用Jbutton改变绘图的颜色

时间:2014-11-02 20:42:48

标签: java swing

我写了一个绘制数字的程序。我在组件之间的通信有问题。经过几个小时的不同方式,我放弃了。

我的程序分为两个包: 第一个名为面板的包:App.java,ButtonPanel.java,ColorPanel.java,DrawingPanel.java,MainFrame.java

第二个包称为shapes: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]

然而,当我按下Circle按钮和红色按钮时,它给了我NullPointerException。看起来cl不会传递给Circle.java类中的updatedColor变量

 updatedColor = buttonPanel.getNewColor();

当我sysout该语句时,它给我null。请问有人帮我吗?

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

您正在致电

updatedColor = buttonPanel.getNewColor();

但是,变量buttonPanel是Circle类中的私有变量,您没有初始化它。

这就是为什么你有NullPointerException的原因。创建圆时,您还应该向其传递一个对初始化buttonPanel的按钮面板的引用。