Folks,
I have been struggling for some time with a problem. I think I have it half solved, but as a beginner at Java (and hindered by decades of programming in older languages), I can't get it solved. Any help would be appreciated, and pardon if I don't follow protocol well.
I have a JPanel that populates a JTabbedPane. It seems to work fine. Right now I've just got it painting full of random pixels. A couple other tabs are present, and the panel is only on the first tab, so success. I need to redraw that image as a simulation progresses. I also want to redraw it when the JFrame is resized. Right now I am just looking for the random pixels to change during a resize.
I've got the Resize plugged in correctly, I think. The text I asked to be echoed does print. But I can't get the panel to update. I worry about creating 1000 instances of the JPanel, or worse the entire tab. I will attach the most relevant code below. I've tried many variations not shown here, with repaint, revalidate, etc. Just can't figure it out. Thank you for any insights!
Randy
//*********************** public final class M_Move extends JPanel { static Random generator; static JComponent panel1; public M_Move() { super(new GridLayout(1, 1)); JTabbedPane tabbedPane = new JTabbedPane(); ImageIcon icon1 = createImageIcon("http://www.javaprogrammingforums.com/images/map.gif"); ImageIcon icon2 = createImageIcon("http://www.javaprogrammingforums.com/images/input.gif"); ImageIcon icon3 = createImageIcon("http://www.javaprogrammingforums.com/images/output.gif"); // JComponent panel1 = makeTextPanel("Spatial"); // JComponent panel1 = new MakeBufferedImage(640, 480); panel1 = new MakeBufferedImage(640, 480); tabbedPane.addTab("Spatial", icon1, panel1, "Map agent movements"); tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); panel1.setPreferredSize(new Dimension(640, 480)); < snip ... panels 2 and 3 are set up, and work> //Add the tabbed pane to this panel. add(tabbedPane); } protected JComponent makeTextPanel(String text) { < snipped > } protected static ImageIcon createImageIcon(String path) { < snipped > } private static void createAndShowGUI() { //Create and set up the window. //JFrame frame = new JFrame("Multi Move"); JFrame frame = new Main(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. addComponentsToPane(frame.getContentPane()); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. generator = new Random(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { //Turn off metal's use of bold fonts // UIManager.put("swing.boldMetal", Boolean.FALSE); createAndShowGUI(); } }); } //************************** package m_move; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import javax.swing.JPanel; public final class MakeBufferedImage extends JPanel { private BufferedImage canvas; public MakeBufferedImage(int width, int height) { canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int y=0; y<height; y++) { for (int x=0; x<width; x++) { int r = M_Move.generator.nextInt(256); int g = M_Move.generator.nextInt(256); int b = M_Move.generator.nextInt(256); int rgb = new Color(r,g,b).getRGB(); canvas.setRGB(x, y, rgb); } } } @Override public Dimension getPreferredSize() { return new Dimension(canvas.getWidth(), canvas.getHeight()); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.drawImage(canvas, null, null); } public void redrawer() { System.out.println("IN REDRAWER!!!!!!!!!!!!!!!!!!!!!!!!!!!"); canvas = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB); for (int y=0; y<480; y++) { for (int x=0; x<640; x++) { int r = M_Move.generator.nextInt(256); int g = M_Move.generator.nextInt(256); int b = M_Move.generator.nextInt(256); int rgb = new Color(r,g,b).getRGB(); canvas.setRGB(x, y, rgb); } } } } // ******************************************** package m_move; // //import java.awt.Frame; import java.awt.Frame; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.KeyEvent; import javax.swing.JComponent; import javax.swing.JFrame; public class Main extends JFrame implements ComponentListener { // private BufferedImage canvas; public Main() { addComponentListener(this); } @Override public void componentHidden(ComponentEvent e) { System.out.println("componentHidden"); } @Override public void componentMoved(ComponentEvent e) { System.out.println("componentMoved"); } @Override public void componentResized(ComponentEvent e) { // M_Move.resizer(); // maker.Redrawer(); M_Move.panel1 = new MakeBufferedImage(640, 480); // panel1.revalidate(); // MakeBufferedImage t = new MakeBufferedImage(640,480); // panel1.redrawer(); System.out.println(" RESIZER!!! --- Resized "); System.out.println("componentResized"); System.out.println(getExtendedState()); if (getExtendedState() == Frame.ICONIFIED) { System.out.println("RESIZED TO ICONIFIED"); } else if (getExtendedState() == Frame.NORMAL) { System.out.println("RESIZED TO NORMAL"); } else { System.out.println("RESIZED TO MAXIMIZED"); } } @Override public void componentShown(ComponentEvent e) { // throw new UnsupportedOperationException("Not supported yet."); } }