How to draw a rectangle in Java using drawRectintintintint f
How to draw a rectangle in Java using drawRect(int,int,int,int) from its CENTER POINT, so that I can use drawRect() by saying drawRect(centerX,centerY,width,height)
Solution
For this, we\'ll create a Swing program,
public class RectanglesDrawingExample extends JFrame {
      public RectanglesDrawingExample() {
super(\"Rectangles Drawing Demo\");
getContentPane().setBackground(Color.WHITE);
setSize(480, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
void drawRectangles(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
public class RectanglesDrawingExample extends JFrame {
      public RectanglesDrawingExample() {
super(\"Rectangles Drawing Demo\");
getContentPane().setBackground(Color.WHITE);
setSize(480, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
void drawRectangles(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
 g2d.drawRect(30, 50, 420, 120);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new RectanglesDrawingExample().setVisible(true);
}
});
}
}


