Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing simple rectangles on a Jframe in java

I am extending JFrame like this:

public GameFrame() {
    this.setBounds(30, 30, 500, 500);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    initializeSquares();
}

private void initializeSquares(){
    for(int i = 0; i < 5; i++){
        this.getContentPane().add(new Square(i*10, i*10, 100, 100));
    }
    this.setVisible(true);
}

However, only one square is being drawn on the screen, does anybody know why?

also My Square class looks like this:

private int x;
private int y;
private int width;
private int height;
public Square(int x, int y, int width, int height){
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawRect(x, y, width, height);
}
like image 287
wfbarksdale Avatar asked Mar 15 '12 03:03

wfbarksdale


People also ask

How do you draw a rectangle in a JFrame?

In Java, to draw a rectangle (outlines) onto the current graphics context, we can use the following methods provided by the Graphics/Graphics2D class: drawRect(int x, int y, int width, int height) draw3DRect(int x, int y, int width, int height, boolean raised) draw(Rectangle2D)

How do you create a rectangle box in Java?

Similarly, we will draw a rectangle on Java applet by two ways . By using the drawRect(int x, int y, int width, int height) or by drawing four lines joining the edges . Examples: We will draw a rectangle of height 200 and width 200 and At a position 100,100 on the applet.

How do you draw and fill a rectangle in Java?

To draw a solid (filled) rectangle, fillRect () method is used. This method also takes four parameters similar to the drawRect () method. A rounded outlined rectangle can be drawn by using the drawRoundRect () method. This method takes the six parameters.


1 Answers

The JFrame's contentPane uses BorderLayout by default. When you add a Square to it, it gets added by default BorderLayout.CENTER and covers up any previously added Squares. You will want to read up on all the layout managers available to Swing GUI's.

For e.g., start here: Laying Out Components within a Container

But having said this, I would do things differently. I would create just one JPanel and make it able to paint multiple squares. For example something like so:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class GameFrame extends JFrame {
   public GameFrame() {
      super("Game Frame");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Squares squares = new Squares();
      getContentPane().add(squares);
      for (int i = 0; i < 15; i++) {
         squares.addSquare(i * 10, i * 10, 100, 100);
      }

      pack();
      setLocationRelativeTo(null);
      setVisible(true);

   }

   public static void main(String[] args) {
      new GameFrame();
   }

}

class Squares extends JPanel {
   private static final int PREF_W = 500;
   private static final int PREF_H = PREF_W;
   private List<Rectangle> squares = new ArrayList<Rectangle>();

   public void addSquare(int x, int y, int width, int height) {
      Rectangle rect = new Rectangle(x, y, width, height);
      squares.add(rect);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      for (Rectangle rect : squares) {
         g2.draw(rect);
      }
   }

}
like image 100
Hovercraft Full Of Eels Avatar answered Oct 12 '22 03:10

Hovercraft Full Of Eels