gpt4 book ai didi

java - 使用 Jbutton 更改绘图的颜色

转载 作者:行者123 更新时间:2023-12-01 12:20:23 25 4
gpt4 key购买 nike

我写了一个画图的程序。我在组件之间的通信方面遇到问题。经过几个小时的不同方式我放弃了。

我的程序分为 2 个包:第一个包称为面板:App.java、ButtonPanel.java、ColorPanel.java、DrawingPanel.java、MainFrame.java

第二个包叫shapes:Circle.java、Line.java、Square.java等图形。还有一个名为 Shape.java 的抽象类

我希望基本上按下名为 btnRed 的按钮,并且可以用红色绘制图形。在本例中我想画圆。

enter image description here

圆.类

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 没有传递给 Circle.java 类中的 UpdatedColor 变量

 updatedColor = buttonPanel.getNewColor();

当我输出该语句时,它给我 null 。有人可以帮我吗?

最佳答案

绘画是破坏性的,每次触发绘画周期时,您都需要重新绘制整个状态,这意味着,您需要以某种方式存储形状的颜色和形状。

而不是使用...

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;
}
}

关于java - 使用 Jbutton 更改绘图的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26704164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com