How to make a Rect in Java?

danny :

I have been scanning the internet for half an hour and the Rects people suggest do not work so I wanted to know what you guys would suggest.

Crevious :

The simple code in here which is made by a 2D grid

 import java.awt.Graphics;

 import javax.swing.JComponent;
 import javax.swing.JFrame;

 class MyCanvas extends JComponent {

 public void paint(Graphics g) {
 g.drawRect (10, 10, 200, 200);  
 }
  }

  public class DrawRect {
 public static void main(String[] a) {
   JFrame window = new JFrame();


window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 window.setBounds(30, 30, 300, 300);
  window.getContentPane().add(new MyCanvas());
   window.setVisible(true);
   }
  }

Output :

enter image description here

Otherwise use the drawRect() method of a Graphics object. This method looks like:

drawRect(int  x, int  y, 
    int  width, int  height)

It draws the outline of a rectangle using the current pen color. The left and right edges of the rectangle are at x and x + width respectively. The top and bottom edges of the rectangle are at y and y + height respectively.

This method is also used to draw a square. This applet draws a rectangle around the entire drawing area, then puts another rectangle in the center.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=276498&siteId=1