A newly created graphic is displayed only after changing the frame size.

This is a continuation from this post.

I have a set of graphic images with arbitrary size and drawing on a JPanel component. I have a button that adds a new draw object to the same JPanel, but does not appear until I resized the window. I added the EDT information mentioned in this post , and also named the repaint () method for the component. I am not using ArrayList as suggested by Hovercraft , but I will. My brain needs to understand things slowly when I go.

Thanks.

The code is in two classes.

import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; public class ZombieDance extends JComponent { JFrame canvas = new JFrame(); JPanel actionPanel = new JPanel(); JButton newZombie = new JButton("Add new Zombie"); ZombieDance(){ //create a couple default zombies buildGUI(); Random rand = new Random(); int i,x,y,w,h; //add zombies to my canvas for (i=1;i<8;i++) { float r = rand.nextFloat(); float g = rand.nextFloat(); float b = rand.nextFloat(); x = rand.nextInt(50); y = rand.nextInt(50); w = rand.nextInt(50); h = rand.nextInt(50); canvas.add(new Zombie(x,y,w,h,r,g,b)); } } //prep the canvas void buildGUI(){ actionPanel.add(newZombie); canvas.add(actionPanel); canvas.setLayout(new GridLayout(3,3)); canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canvas.setSize(400,400); canvas.setBackground(Color.WHITE); canvas.setVisible(true); newZombie.addActionListener(new NewZombieClickHandler()); } public class NewZombieClickHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ Random rand = new Random(); int x,y,w,h; float r = rand.nextFloat(); float g = rand.nextFloat(); float b = rand.nextFloat(); x = rand.nextInt(50); y = rand.nextInt(50); w = rand.nextInt(50); h = rand.nextInt(50); canvas.add(new Zombie(x,y,w,h,r,g,b)); canvas.repaint(); } } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new ZombieDance(); } }); } 

}

Second class

 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JPanel; public class Zombie extends JPanel{ private int x,y,w,h; private float r,g,b; Zombie(int argx, int argy, int argw, int argh, float argr, float argg, float argb){ x = argx; y = argy; w = argw; h = argh; r = argr; g = argg; b = argb; } public Dimension getPreferredSize() { return new Dimension(20,20); } protected void paintComponent(Graphics gr) { super.paintComponent(gr); //g.drawString("Drawing canvas...",10,20); gr.setColor(new Color(r,g,b)); gr.fillRect(x,y,h,w); } } 
+4
source share
2 answers

I have a button that adds a new draw object to the same JPanel but doesn’t appear until I resized the window

When you add a component to a visible GUI, the code should be:

 canvas.add(...); canvas.validate(); //canvas.repaint(); // sometimes needed 

(editor: modified for review)

+7
source

I tried myself and I found that the paintChildren () method solved the problem.

-1
source

All Articles