Drawing simple rectangles on jframe in java

I am extending the JFrame as follows:

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 drawn on the screen, does anyone know why?

also the My Square class is as follows:

 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); } 
+6
source share
3 answers

The default JFrame content area is BorderLayout. When you add a square to it, it is added by default to BorderLayout.CENTER and closes all previously added squares. You will want to read all the layout managers available for the Swing GUI.

For example, start here: Aligning components in a container

But, having said that, I would have acted differently. I would create only one JPanel and make it able to draw a few squares. For example, something like this:

 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); } } } 
+12
source

Fo absolute positioning, call setLayout (null). Then the icons will be drawn at the position returned by the getLocation () method, so you can call setLocation () first.

+1
source

Even I had a problem when displaying multiple rectangles in a jframe with the absolute layout layout ie set to null.

 JFrame frame = new JFrame("hello"); frame.setLayout(null); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setSize(600, 600); frame.getContentPane().setBackground(Color.red); frame.getContentPane().add(new Square(10,10,100,100));//My rectangle class is similar to the Square mentioned in the question and it extends JComponent. 

However, I was getting problems because the rectangle did not display. Unless you explicitly set the borders to Square (JComponent), this will not work. Even if Square has the location passed in the constructor, setbounds only fixes the problem. Below is the problem, and this is the problem for absolute jframe layouts.

 Square square = new Square(10,10,100,100); square.setBounds(10,10,100,100); frame.getContentPane().add(square); 
0
source

Source: https://habr.com/ru/post/910766/


All Articles