2013-04-30 77 views
2

我正在使用JButton的Action偵聽器繪製不同的形狀。它工作正常,但如何保持面板上以前繪製的形狀?因爲當另一個按鈕按下以前的形狀已經消失。顯示JPanel上的所有繪製形狀

jButton1.setText("Button1"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

jButton2.setText("Button2"); 
    jButton2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton2ActionPerformed(evt); 
     } 
    }); 


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    // TODO add your handling code here: 
    s = evt.getActionCommand(); 
    repaint(); 

} 
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
    s = evt.getActionCommand(); 
    repaint(); 

} 

.......和方法的paintComponent是

protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 


     System.out.println("====>>> " + s); 
     switch (s) { 

      case "Button1": 
       g.drawRoundRect(20,20,40,40,100,200); 
       break; 

      case "Button2": 
       g.drawRect(0, 0, 200, 200); 
       break; 

      default: 
       g.drawOval(40, 40, 100, 100); 

這裏的String包含按下按鈕字幕。

+0

基本問題是,在每個繪畫循環中,您需要將圖形恢復到您需要的狀態。也就是說,您將需要重新繪製您之前繪製(並希望保留)的任何東西 – MadProgrammer 2013-04-30 07:38:30

回答

5

這些要麼應該做的:

  • 存儲所有的繪圖操作中的列表和油漆,迭代列表和油漆他們。

  • 將圖形繪製到BufferedImage並將圖像顯示在標籤中。例如。可以看出在this answer

+0

您的第一個選擇「將所有繪圖操作存儲在列表和繪畫中,迭代列表並將它們全部繪製。」我該怎麼做,請解釋一下 – iostream007 2013-04-30 08:48:25

+0

試一試,發表一下代碼,然後問一個更具體的問題。我不打算向你提供一個例子。 – 2013-04-30 08:50:26

6

你可以簡單地畫一個緩衝的圖像和顯示圖像。

演示代碼:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.GridLayout; 
import java.awt.image.BufferedImage; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

class TestPaint { 

    private BufferedImage image; 
    private JLabel drawing; 

    private int x = 0; 
    private int y = 0; 

    protected void initUI() { 
     JFrame jFrame = new JFrame(); 
     jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JButton jButton1 = new JButton(); 
     JButton jButton2 = new JButton(); 
     jButton1.setText("Button1"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      @Override 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jButton2.setText("Button2"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      @Override 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(evt); 
      } 
     }); 

     image = new BufferedImage(500, 300, BufferedImage.TYPE_INT_RGB); 
     image.getGraphics().setColor(Color.WHITE); 
     image.getGraphics().fillRect(0, 0, image.getWidth(), image.getHeight()); 
     drawing = new JLabel(new ImageIcon(image)); 
     JPanel bottomPanel = new JPanel(new FlowLayout()); 
     JPanel buttonPanel = new JPanel(new GridLayout(1, 0)); 
     buttonPanel.add(jButton1); 
     buttonPanel.add(jButton2); 
     bottomPanel.add(buttonPanel); 
     jFrame.add(drawing); 
     jFrame.add(bottomPanel, BorderLayout.SOUTH); 
     jFrame.pack(); 
     jFrame.setVisible(true); 
    } 

    private Graphics getImageGraphics() { 
     return image.getGraphics(); 
    } 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     Graphics g = getImageGraphics(); 
     g.setColor(Color.GREEN); 
     g.drawRoundRect(x, y, 40, 40, 100, 200); 
     drawing.repaint(); 
     x += 5; 
     y += 5; 
    } 

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
     Graphics g = getImageGraphics(); 
     g.setColor(Color.BLUE); 
     g.drawRect(x, y, 200, 200); 
     drawing.repaint(); 
     x += 5; 
     y += 5; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new TestPaint().initUI(); 
      } 

     }); 
    } 
}