Sto scrivendo un'applicazione che ha un JLayeredPane (chiamalo layer) contenente due JPanel in livelli diversi. Sovrascrivo il metodo paintComponent di JPanel nella parte inferiore (chiamiamolo grid_panel) in modo che dipinga una griglia e il metodo paintComponent di quello in alto (chiamiamolo circuit_panel) in modo che dipinga un circuito.JLayeredPane and painting
Ecco una sintesi della struttura:
layers -
|-circuit_panel (on top)
|-grid_panel (at bottom)
Voglio il grid_panel di rimanere statica, cioè, di non fare alcun riverniciare (ad eccezione di quello iniziale), in quanto non cambia.
Il problema è, ogni volta che chiamo circuit_panel.repaint(), grid_panel viene ridipinto anche! Questo è decisamente non efficiente.
Penso che questo sia dovuto al comportamento di pittura entusiasta di JLayeredPane. C'è un modo per disabilitare questa funzione in JLayeredPane?
Nel caso siate interessati a vedere l'effetto di cui sopra, ho scritto un piccolo programma demo:
public class Test2 extends JFrame {
public Test2() {
JLayeredPane layers = new JLayeredPane();
layers.setPreferredSize(new Dimension(600, 400));
MyPanel1 myPanel1 = new MyPanel1();
MyPanel2 myPanel2 = new MyPanel2();
myPanel1.setSize(600, 400);
myPanel2.setSize(600, 400);
myPanel1.setOpaque(false);
myPanel2.setOpaque(false);
myPanel2.addMouseListener(new MyMouseListener(myPanel2));
layers.add(myPanel1, new Integer(100)); // At bottom
layers.add(myPanel2, new Integer(101)); // On top
this.getContentPane().add(layers, BorderLayout.CENTER);
this.setSize(600, 400);
}
class MyPanel1 extends JPanel {
Color getRandomColor() {
int r = (int) (256 * Math.random());
int g = (int) (256 * Math.random());
int b = (int) (256 * Math.random());
return new Color(r, g, b);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getRandomColor());
g2d.fillRoundRect(30, 30, 60, 60, 5, 5);
}
}
class MyPanel2 extends JPanel {
Color getRandomColor() {
int r = (int) (256 * Math.random());
int g = (int) (256 * Math.random());
int b = (int) (256 * Math.random());
return new Color(r, g, b);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getRandomColor());
g2d.fillRoundRect(45, 45, 75, 75, 5, 5);
}
}
class MyMouseListener extends MouseAdapter {
JPanel panel;
MyMouseListener(JPanel panel) {
this.panel = panel;
}
@Override
public void mouseClicked(MouseEvent e) {
panel.repaint();
}
}
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
(new Test2()).setVisible(true);
}
});
}
}
Sono abbastanza sicuro che non sarà in grado di ottenere intorno a questa a parte forse il doppio buffering i livelli se costoso per disegnare. Tutto si riduce a RepaintManager che tiene traccia delle regioni "sporche", perché i livelli condividono lo stesso spazio dello schermo e un ridisegno di uno farà scattare un ridisegno di un altro ... Buona fortuna. – Adam
Quindi c'è un modo per un JPanel di mettere in cache ciò che dovrebbe dipingere (ovviamente ciò che è dipinto dovrebbe essere statico), e lasciare il repaint() semplicemente dipingere la cache, invece di fare tutti questi calcoli ancora e ancora? – stackoverflower
hehehe anche su ridimensionamento, con tremolio tremolante +1, ci deve essere un altro problema, ma sei in grado di usare [JLayer (da Java7)] (http://stackoverflow.com/a/9272534/714968) o 'JXLayer (per Java6) ' – mKorbel