0
I’m starting to work with the package swing and manipulation of events and I came up with an idea to test some program that would create rectangles of random sizes and colors. Code creation and execution all came out as planned, except for the fact that the frame already initializes with a drawn rectangle.
I tried to set the background of the panel as opaque and white on startup but the problem persists.
OBS: The idea of the program is that the squares are drawn on top of each other.
public class RandomSquares {
NovoPainel painel;
JFrame frame;
public static void main(String[] args) {
RandomSquares rs = new RandomSquares();
rs.construirGUI();
}
public void construirGUI() {
frame = new JFrame("Squares");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450,450);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
painel = new NovoPainel();
JButton botao = new JButton("CRIAR");
botao.addActionListener(new NovoPainel());
frame.getContentPane().add(painel);
frame.getContentPane().add(BorderLayout.SOUTH, botao);
frame.setVisible(true);
}
public class NovoPainel extends JPanel implements java.awt.event.ActionListener {
public void paintComponent(Graphics g) {
int red = (int) (Math.random() * 256);
int green = (int) (Math.random() * 256);
int blue = (int) (Math.random() * 256);
int posicaoX = (int) (Math.random() * this.getWidth());
int posicaoY = (int) (Math.random() * this.getHeight());
int largura = (int) (Math.random() * this.getWidth() + 1);
int altura = (int) (Math.random() * this.getHeight() + 1);
g.setColor(new Color(red, green, blue));
g.fillRect(posicaoX, posicaoY, largura, altura);
}
public void actionPerformed(java.awt.event.ActionEvent ev) {
painel.repaint();
}
}
}
I don’t understand, what’s wrong with the code? It’s not clear
– user28595
I want to start the application with the blank drawing area, then each time the user clicks the button, the system will call repaint() that will draw the rectangles. But the drawing area starts already drawn.
– ADayan
The paintcomponent method will always be called, any changes on the screen it is redesigned, the way it is, until minimizing will create squares, you need to create a mechanism that avoids this.
– user28595
You can place rectangles in a list by adding one whenever the user clicks on a button. In paintComponent() you paint the white background first, then use a foreach to scroll through the List of rectangles painting each. :)
– Douglas
@Douglas isn’t just that, he needs a mechanism to control creation as well. I would use bufferedimage, keep making loop in the paintcomponent I think very bad, when you have a class for images that controls this without weighing the application with loops.
– user28595
Um, as I recall Bufferedimage gives you a Graphics2d that you can use to paint the rectangle directly in the Bufferedimage, so a new rectangle would be painted on it whenever the button was clicked. You would keep Bufferedimage in an instance variable and paint it in paintComponent() without using a loop. If that’s it, it looks a lot better to me than my suggestion.
– Douglas
Well, I found a way to solve the problem, but there’s an annoying bug in this code that is "mirroring" the southern boot in the northern position. I see a way to solve this bug.
– user28595
@That’s what Douglas is. And take the responsibility of the paintcomponent to pick up these points, as he is doing, delegate the repaint and the creation to a method outside.
– user28595