In a part of my app (a game), I draw a playfield which consists of a
grid of "cells".
The resolution of the playfield can be made twice as big by pressing
on the "x2" button.
But... when the playfield (GraphicalViewPanel) is painted, the cells,
which are themselves
panels added to the playfield, are not repainted UNTIL the playfield
is resized !!
I already have figured out using println's that the paintComponent()
method of the "cells"
is only called after resizing the frame.
The cells (here of type JPanel but a subclass of JPanel in the real
app where they contain
data) are saved in the Map celPanels.
The method fillCellPanels fills this map according to the private
members nrOfRows and nrOfCols.
The method addCellPanelsToViewPanel adds all cells in this map to the
playfield.
But WHY must i resize the main application frame after pressing "x2"
to see the effect???
PLEEZ HELP, this problem bothers me already 2 months or so.......
Greetz Klaas
The app consists of 2 classes which are listed below... They're not
too big :-)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestFrame extends JFrame {
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.getContentPane().setLayout(new BorderLayout());
final GraphicViewPanel gvp = new GraphicViewPanel();
frame.getContentPane().add(gvp, BorderLayout.CENTER);
JPanel knoppenPanel = new JPanel();
JButton groterButton = new JButton("x2");
groterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
gvp.doubleResolution();
gvp.repaint(); //not enough, playfield must be resized
}
});
knoppenPanel.add(groterButton);
frame.getContentPane().add(knoppenPanel, BorderLayout.SOUTH);
frame.show();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class GraphicViewPanel extends JPanel {
private int nrOfRows = 3;
private int nrOfCols = 3;
private int offsetRows = 0;
private int offsetCols = 0;
private Map celPanels = new HashMap();
private MouseListener mouseListener;
public GraphicViewPanel() {
setBackground(new Color(0, 200, 30));
setMinimumSize(new Dimension(200, 0));
fillCellPanels();
addCellPanelsToViewPanel();
} //constructor
public void fillCellPanels() {
System.out.println("fillCellPanels");
celPanels.clear(); //en maak de map met verwijzingen naar de
panels leeg
for (int row = offsetRows; row < offsetRows + nrOfRows; row++) {
for (int col = offsetCols; col < offsetCols + nrOfCols; col++) {
Point key = new Point(col, row);
JPanel celPanel = new JPanel();
//vul celpanel met levende cellen
celPanels.put(key, celPanel);
}
}
}
public void addCellPanelsToViewPanel() {
System.out.println("addCellPanelsToViewPanel");
this.removeAll();
this.setLayout(new GridLayout(nrOfRows, nrOfCols, 1, 1));
JPanel celPanel;
for (Iterator iterator = celPanels.entrySet().iterator();
iterator.hasNext(); ) {
//aanpassen
celPanel = (JPanel)((Map.Entry)iterator.next()).getValue();
//System.out.println("Celpanel wordt toegevoegd");
this.add(celPanel);
}
}
public void doubleResolution() {
nrOfRows = nrOfRows*2;
nrOfCols = nrOfCols*2;
fillCellPanels();
addCellPanelsToViewPanel();
}
public void paintComponent(Graphics g) {
/**@todo: Override this javax.swing.JComponent method*/
super.paintComponent( g);
System.out.println("GraphicViewPanel.paintComponent()");
}
} //GraphicViewPanel
> But WHY must i resize the main application frame after pressing "x2"
> to see the effect???
> PLEEZ HELP, this problem bothers me already 2 months or so.......
> Greetz Klaas
Whenever you add or hide components, or resize (in code) their container,
you should usually call validate() on their container to get them
re-layed-out. This might solve your problem...
--
--
Jason Teagle
ja...@teagster.co.uk
-----------------------------------------------------------
A list of programming resources I use:
ML: www.windev.org, www.codecipher.com, www.beginthread.com
MB: www.codeguru.com, www.codeproject.com
NG: comp.lang.java.*
OI: www.php.net
-----------------------------------------------------------
revalidate() is the more appropriate call on Swing components. Figuring
out the validate() / invalidate() combinations required to display a
particular mutation to the GUI structure is a nightmare for new AWT
users, and Swing simplifies this quite a bit by providing the revalidate
method which performs all the required magic internally.
BK
> revalidate() is the more appropriate call on Swing components. Figuring
> out the validate() / invalidate() combinations required to display a
> particular mutation to the GUI structure is a nightmare for new AWT
> users, and Swing simplifies this quite a bit by providing the revalidate
> method which performs all the required magic internally.
OK, I'll bear this in mind.
Thanx anyway !
Klaas
Yes, that's why - as I said, whenever you hide (or show) items it needs
revalidating.