2012-03-08 12 views
5

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); 
      } 
     }); 
    } 
} 
+0

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

+0

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

+0

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

risposta

7

come l'avete trovato, un BufferedImage è un modo efficace per memorizzare nella cache contenuti complessi per il rendering efficiente; CellTest è un esempio. Un renderizzatore di pesi mosca, indicato con here, è un altro approccio. Infine, ho rielaborato il tuo esempio istruttivo in un modo che potrebbe facilitare la sperimentazione.

Layer Demo

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.Random; 
import javax.swing.JFrame; 
import javax.swing.JLayeredPane; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

/** @see https://stackoverflow.com/q/9625495/230513 */ 
public class LayerDemo extends JFrame { 

    private static final Dimension d = new Dimension(320, 240); 

    public LayerDemo() { 
     JLayeredPane layers = new JLayeredPane(); 
     layers.setPreferredSize(d); 

     layers.add(new LayerPanel(1 * d.height/8), 100); 
     layers.add(new LayerPanel(2 * d.height/8), 101); 
     layers.add(new LayerPanel(3 * d.height/8), 102); 

     this.add(layers, BorderLayout.CENTER); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.pack(); 
     this.setLocationByPlatform(true); 
    } 

    private static class LayerPanel extends JPanel { 

     private static final Random r = new Random(); 
     private int n; 
     private Color color = new Color(r.nextInt()); 

     public LayerPanel(int n) { 
      this.n = n; 
      this.setOpaque(false); 
      this.setBounds(n, n, d.width/2, d.height/2); 
      this.addMouseListener(new MouseHandler(this)); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g; 
      g2d.setRenderingHint(
       RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 
      g2d.setColor(color); 
      g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16); 
      g2d.setColor(Color.black); 
      g2d.drawString(String.valueOf(n), 5, getHeight() - 5); 
     } 

     private void update() { 
      color = new Color(r.nextInt()); 
      repaint(); 
     } 
    } 

    private static class MouseHandler extends MouseAdapter { 

     LayerPanel panel; 

     MouseHandler(LayerPanel panel) { 
      this.panel = panel; 
     } 

     @Override 
     public void mouseClicked(MouseEvent e) { 
      panel.update(); 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       (new LayerDemo()).setVisible(true); 
      } 
     }); 
    } 
}