2014-11-02 69 views
0

我寫了一個繪製圖形的程序。我在組件之間進行通信時遇到問題。幾個小時後,我放棄了不同的方式。 稱爲第一包裝片:用Jbutton改變圖形的顏色

我的程序2包間分爲App.java,ButtonPanel.java,ColorPanel.java,DrawingPanel.java,MainFrame.java

稱爲第二個包的形狀:Circle.java, Line.java,Square.java等數字。還有一個叫做Shape.java的抽象類

我希望基本上可以按下叫做btnRed的按鈕,並且可以用紅色繪製圖形。在這種情況下,我想繪製圓圈。

enter image description here

Circle.class

package shapes; 

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 

import panels.ButtonPanel; 


public class Circle extends Shape { 

    private int x1, y1, x2, y2; 
    private Color updatedColor; 
    private ButtonPanel buttonPanel; 

    public Circle(int x1, int y1, int x2, int y2) { 
     this.x1 = x1; 
     this.y1 = y1; 
     this.x2 = x2; 
     this.y2 = y2; 
    } 

    @Override 
    public void draw(Graphics2D g2) { 

     int r = (int)Math.sqrt(Math.pow((x2-x1),2) + Math.pow((y2-y1),2)); 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

     updatedColor = buttonPanel.getNewColor(); 

     g2.setColor(updatedColor); 
     g2.drawOval(x1 - r, y1 - r, r*2, r*2); 
    } 

    @Override 
    public void update(int x, int y) { 
     x2 = x; 
     y2 = y; 
    } 
} 

我有ButtonPanel.java類的所有按鈕和按鍵的聽衆。

package panels; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.border.Border; 

import shapes.Circle; 

public class ButtonPanel extends JPanel implements ActionListener{ 

    private JButton btnLine; 
    private JButton btnCircle; 
    private JButton btnTriangle; 
    private JButton btnSquare; 
    private JButton btnHexagon; 
    private JButton btnUndo; 
    private JButton btnReset; 
    private JButton btnSave; 
    private JButton btnLoad; 
    private JButton btnPentagon; 
    private JButton btnWhite; 
    private JButton btnYellow; 
    private JButton btnOrange; 
    private JButton btnPink; 
    private JButton btnRed; 
    private JButton btnGreen; 
    private JButton btnBlue; 
    private JButton btnMagenta; 
    private JButton btnCyan; 
    private JButton btnLightGrey; 
    private JButton btnDarkGrey; 
    private JButton btnBlack; 
    private Color cl; 

    //i want to hook up this with my drawing panel 
    private DrawingPanel drawingPanel; 
    private Circle circle; 


    public ArrayList<JButton> shapeArrayList = new ArrayList<JButton>(); 

    public ButtonPanel() { 

     //instanitiate buttons 
     Dimension dim = getPreferredSize(); 
     btnLine = new JButton("LINE"); 
     btnCircle = new JButton("CIRCLE"); 
     btnTriangle = new JButton("TRIANGLE"); 
     btnSquare = new JButton("SQUARE"); 

     btnWhite = new JButton("WHITE"); 
     btnWhite.setBackground(Color.WHITE); 


     btnYellow = new JButton("YELLOW"); 
     btnYellow.setBackground(Color.YELLOW); 

     btnOrange = new JButton("ORANGE"); 
     btnOrange.setBackground(Color.ORANGE); 

     btnPink = new JButton("PINK"); 
     btnPink.setBackground(Color.PINK); 

     btnRed = new JButton("RED"); 
     btnRed.setBackground(Color.RED); 


     btnGreen = new JButton("GREEN"); 
     btnGreen.setBackground(Color.GREEN); 

     btnBlue = new JButton("BLUE"); 
     btnBlue.setBackground(Color.BLUE); 

     btnMagenta = new JButton("MAGENTA"); 
     btnMagenta.setBackground(Color.MAGENTA); 

     btnCyan = new JButton("CYAN"); 
     btnCyan.setBackground(Color.CYAN); 

     btnLightGrey = new JButton("LIGHT GREY"); 
     btnLightGrey.setBackground(Color.LIGHT_GRAY); 


     btnDarkGrey = new JButton("GREY"); 
     btnDarkGrey.setBackground(Color.DARK_GRAY); 

     btnBlack = new JButton("BLACK"); 
     btnBlack.setBackground(Color.BLACK); 

     btnPentagon = new JButton("PENTAGON"); 
     btnUndo = new JButton("UNDO"); 
     btnReset = new JButton("RESET"); 
     btnSave = new JButton("SAVE"); 
     btnLoad = new JButton("LOAD"); 

     //set width fot the panel button 
     dim.width = 200; 
     setPreferredSize(dim); 

     Border innerBorder = BorderFactory.createTitledBorder("Draw buttons"); 
     Border outerBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5); 
     setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder)); 

     //set new Lyaout for ButtonPanel 
     setLayout(new GridBagLayout()); 

     GridBagConstraints gc = new GridBagConstraints(); 

     gc.gridx = 0; //set position from left to right 
     gc.gridy = 0; //set position from up to down 
     gc.weightx = 1; //weight controls how irmuch space take relative to the other space 
     gc.weighty = 1; 

     // tell will it take all the space in the components or not 
     gc.fill = GridBagConstraints.BOTH; 

     add(btnLine, gc); //add buttons to the layout 

     gc.gridx = 0; 
     gc.gridy = 1; 
     add(btnCircle, gc); 

     gc.gridx = 0; 
     gc.gridy = 2; 
     add(btnSquare, gc); 

     gc.gridx = 0; 
     gc.gridy = 3; 
     add(btnTriangle, gc); 

     gc.gridx = 0; 
     gc.gridy = 4; 
     add(btnSquare, gc); 

     gc.gridx = 0; 
     gc.gridy = 5; 
     add(btnPentagon, gc); 

     //colors/////////////////////////////////////////////////////// 
     gc.gridx = 0; 
     gc.gridy = 6; 
     add(btnWhite, gc); 

     gc.gridx = 0; 
     gc.gridy = 7; 
     add(btnYellow, gc); 

     gc.gridx = 0; 
     gc.gridy = 8; 
     add(btnOrange, gc); 

     gc.gridx = 0; 
     gc.gridy = 9; 
     add(btnPink, gc); 

     gc.gridx = 0; 
     gc.gridy = 10; 
     add(btnRed, gc); 

     gc.gridx = 0; 
     gc.gridy = 11; 
     add(btnGreen, gc); 

     gc.gridx = 0; 
     gc.gridy = 12; 
     add(btnBlue, gc); 

     gc.gridx = 0; 
     gc.gridy = 13; 
     add(btnMagenta, gc); 

     gc.gridx = 0; 
     gc.gridy = 14; 
     add(btnCyan, gc); 

     gc.gridx = 0; 
     gc.gridy = 15; 
     add(btnLightGrey, gc); 

     gc.gridx = 0; 
     gc.gridy = 16; 
     add(btnDarkGrey, gc); 

     gc.gridx = 0; 
     gc.gridy = 17; 
     add(btnBlack, gc); 


     //operations buttons 
     gc.gridx = 0; 
     gc.gridy = 18; 
     add(btnUndo, gc); 

     gc.gridx = 0; 
     gc.gridy = 19; 
     add(btnReset, gc); 

     gc.gridx = 0; 
     gc.gridy = 20; 
     add(btnSave, gc); 

     gc.gridx = 0; 
     gc.gridy = 21; 
     add(btnLoad, gc); 

     /* 
     * 
     * add action listener to the buttons 
     */ 
     btnLine.addActionListener(this); 
     btnCircle.addActionListener(this); 
     btnTriangle.addActionListener(this); 
     btnSquare.addActionListener(this); 
     btnPentagon.addActionListener(this); 

     btnWhite.addActionListener(this); 
     btnYellow.addActionListener(this); 
     btnOrange.addActionListener(this); 
     btnPink.addActionListener(this); 
     btnRed.addActionListener(this); 
     btnGreen.addActionListener(this); 
     btnBlue.addActionListener(this); 
     btnMagenta.addActionListener(this); 
     btnCyan.addActionListener(this); 
     btnLightGrey.addActionListener(this); 
     btnDarkGrey.addActionListener(this); 
     btnBlack.addActionListener(this); 

     btnUndo.addActionListener(this); 
     btnReset.addActionListener(this); 
     btnSave.addActionListener(this); 
     btnLoad.addActionListener(this); 

    } //end of constructor  


    //overridden method from actionlistener interface 
    @Override 
    public void actionPerformed(ActionEvent e) { 

     JButton btnFire = (JButton) e.getSource(); 

     if(btnFire == btnLine) { 
      drawingPanel.setCurrentShape(0); 
     } 
     else if(btnFire == btnCircle) { 
      drawingPanel.setCurrentShape(1); 
     } 
     else if(btnFire == btnTriangle) { 
      drawingPanel.setCurrentShape(2); 
     } 
     else if(btnFire == btnSquare) { 
      drawingPanel.setCurrentShape(3); 
     } 
     else if(btnFire == btnPentagon) { 
      drawingPanel.setCurrentShape(4); 
     } 

     else if(btnFire == btnUndo) { 
      drawingPanel.setCurrentShape(5); 
     } 
     else if(btnFire == btnReset) { 
      drawingPanel.setCurrentShape(6); 
     } 
     else if(btnFire == btnSave) { 
      drawingPanel.setCurrentShape(7); 
     } 
     else if(btnFire == btnLoad) { 
      drawingPanel.setCurrentShape(8); 
     } 
     else if(btnFire == btnRed) { 
      cl = Color.red; 
      System.out.println("cl is: " + cl); 

     } 
    } //end of actionperformed method 

    public void setDrawingPanel(DrawingPanel drawingPanel) { 
     this.drawingPanel = drawingPanel; 
    } 

    public void setButtonPanel(ButtonPanel buttonPanel) { 
     this.drawingPanel = drawingPanel; 
    } 

    public void setNewColor(Color color) { 
     this.cl = color; 
    } 

    public Color getNewColor() { 
     return cl; 
    } 

} 

當我按下紅色和調試System.out.print它說我有紅色按鈕存儲在CL變量。

cl is: java.awt.Color[r=255,g=0,b=0] 

但是,當我按下圓形按鈕和紅色按鈕,它給了我NullPointerException。它看起來像CL不傳遞給updatedColor變量在Circle.java類

updatedColor = buttonPanel.getNewColor(); 

它給我的空當我SYSOUT的發言。有誰能幫我解決這個問題嗎?

回答

1

繪畫具有破壞性,每次觸發繪畫循環時,都需要重新繪製整個狀態,這意味着,您需要將形狀的顏色與形狀保存在一起。

而不是使用...

updatedColor = buttonPanel.getNewColor(); 

Shape應該有它自己的顏色屬性,它是用來每次塗時間。目前的方法存在的問題是Circle的每個實例都會獲得相同的顏色。

將顏色屬性添加到形狀,並在需要時更改形狀的顏色。不要忘記調用repaint形狀容器上,以更新它

public class Circle extends Shape { 

    private int x1, y1, x2, y2; 
    private Color updatedColor; 
    private ButtonPanel buttonPanel; 

    public Circle(int x1, int y1, int x2, int y2) { 
     this.x1 = x1; 
     this.y1 = y1; 
     this.x2 = x2; 
     this.y2 = y2; 
    } 

    public void setColor(Color color) { 
     updateColor = color; 
    } 

    public Color getColor() { 
     return updateColor; 
    } 

    @Override 
    public void draw(Graphics2D g2) { 

     int r = (int)Math.sqrt(Math.pow((x2-x1),2) + Math.pow((y2-y1),2)); 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

     g2.setColor(getColor()); 
     g2.drawOval(x1 - r, y1 - r, r*2, r*2); 
    } 

    @Override 
    public void update(int x, int y) { 
     x2 = x; 
     y2 = y; 
    } 
} 
0

要調用

updatedColor = buttonPanel.getNewColor(); 

然而,可變buttonPanel是在你的Circle類私有變量,你不初始化。

這就是爲什麼你有一個NullPointerException。當你創建這個圓時,你還應該傳遞一個對初始化爲buttonPanel的按鈕面板的引用。