2011-09-20 61 views
3

我試圖向現有的JPanel添加/繪製單個Graphics對象。我生成了10個隨機大小的初始圖形對象並放置在面板中,但希望一次添加其他繪製對象,隨機大小和初始位置10.試圖在JPanel中添加一個動態定位的圖像按鈕單擊

目前,AddNewDrawItem類不會呈現新的圖形對象。

感謝您的意見。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 

public class Painter{ 

private DrawingPanel dp = new DrawingPanel(); 

    //constructor 
    public Painter(){ 
     buildGUI(); 
    } 

    private void buildGUI(){ 
     JFrame frame = new JFrame(); 
     frame.setLayout(new BorderLayout()); 
     frame.setTitle("Paint drawing demonstration"); 
     JPanel headerPanel = new JPanel(); 
     headerPanel.add(new JLabel("The drawing panel is below")); 
     JButton addNew = new JButton("Add New Graphic"); 
     addNew.addActionListener(new addNewClickHandler()); 
     headerPanel.add(addNew);    
     frame.add(BorderLayout.NORTH,headerPanel); 
     frame.add(BorderLayout.SOUTH,this.dp); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    class DrawingPanel extends JPanel { 

     public void paintComponent(Graphics g) { 
      super.paintComponent(g);  
      this.setBackground(Color.white); 

      int x, posx, posy, width, height; 

      for(x=0;x<=10;x++){ 
       //even number differentiation 
       if(x % 2 == 0){ 
        g.setColor(Color.red); 
       }else{ 
        g.setColor(Color.blue); 
       } 

       Random rand = new Random(); 
       posx = rand.nextInt(300); 
       posy = rand.nextInt(300); 
       width = rand.nextInt(40); 
       height = rand.nextInt(40); 

       //System.out.println("the ran x pos is: " + posx); 
       g.fillRect(posx, posy, width, height); 
      }//end for 
     }//end paintComponent 

     public Dimension getPreferredSize() { 
      return new Dimension(400,400); 
     } 
    }// end DrawingPanel 

    private class addNewClickHandler implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      System.out.print("in addNew_click_handler click handler");//trace debug 
      AddNewDrawItem newItem = new AddNewDrawItem(); 
      newItem.repaint(); 
      System.out.print("after repaint() in addNew_click_handler click handler");//trace debug 
      } 
     } 

    class AddNewDrawItem extends JPanel { 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g);  
      this.setBackground(Color.white); 
      int posx, posy, width, height; 

      Random rand = new Random(); 
      posx = rand.nextInt(300); 
      posy = rand.nextInt(300); 
      width = rand.nextInt(40); 
      height = rand.nextInt(40); 
      g.setColor(Color.cyan); 
      g.fillRect(posx, posy, width, height); 

     }//end paintComponent 
    }//end AddNewDrawItem 

    public static void main(String args[]){ 
     new Painter(); 
    } 

    }//end class Painter 

回答

5

你有一些問題,你的代碼,其中之一是,你有程序邏輯在你的paintComponent方法:代碼隨機變化正在這個方法中顯示的數值,這意味着你的顯示器將改變當它重新繪製時,無論你想不想。要看到這一點,請嘗試調整GUI的大小,然後在繪製的紅色和藍色矩形中看到一些迷幻的變化。

現在針對您當前的問題,解決方案與上述問題的解決方案類似。我建議......

  • 您創建一個ArrayList<Rectangle2D>
  • 您在類的構造函數創建隨機矩形,使他們創造了一次,然後將它們放置在上面的ArrayList中。
  • 您可以在JPanel的paintComponent方法中遍歷此ArrayList,隨時隨地繪製它們。這樣的paintComponent什麼也不做,但塗料這是理所應當的,
  • 您創建一個MouseAdapter派生的對象,將其作爲的MouseListener和的MouseMotionListener添加到您的DrawingPanel
  • 你使用上面的聽衆創造一個新的Rectangle2D
  • 對象,完成後將其添加到ArrayList中,並在DrawingPanel上調用重繪,以便通過按鈕的操作偵聽器激活鼠標適配器。

我會在那裏停下來,但我想你會明白,如果你不這樣做,請問你可能有什麼問題。

+0

+1雖然我會建議界面,列表'。 – trashgod

+0

謝謝,我想我明白了氣墊船的說法。我會給更新一個鏡頭,並在完成後發佈重做代碼。 – jamesTheProgrammer