gpt4 book ai didi

java - 绘制形状时,形状属性未在类之间正确传递 - Java Swing

转载 作者:行者123 更新时间:2023-12-02 13:04:50 24 4
gpt4 key购买 nike

我对 java 相当陌生(编码大约 8 个月),在 JPanel 上绘制形状时遇到一些问题。我已经可以通过单击并拖动鼠标来绘制形状。我遇到的问题是:

  • 当我尝试将颜色传递给正在绘制形状的类时,颜色不会更新并保持默认颜色(黑色)。

  • 当我尝试更改形状的 pointSize(使用 setStroke 方法)时,我正确绘制的第一个形状会更新 pointSize,但我绘制的下一个形状具有默认的 pointSize。 (看起来好像每次绘制新形状时 pointSize 都会重置)

长话短说;我认为实例化新对象的方式以及将值传递给其他类的方式有问题。我一直在从其他人的问题中寻找答案,但我就是不知道出了什么问题。

这是我的代码:

我的 inputHandler 类:

        package colors;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MouseInputListener;


public class InputHandler implements ActionListener, MouseInputListener, ChangeListener
{
private DrawPanel dp;
public int pos = 0;
public String mode = "";
public String shapeColor = "";
private double startx;
private double starty;

ButtonPanel bp;
myRectangle rectangle = new myRectangle();
myLine line = new myLine();
myEllipse ellipse = new myEllipse();

JLabel label;



public InputHandler(DrawPanel dpanel, ButtonPanel bpanel)
{
this.dp = dpanel;
this.bp = bpanel;
dp.addMouseListener(this);
dp.addMouseMotionListener(this);
}


public void actionPerformed (ActionEvent e)
{

switch (buttonPressed(e)) //handles the pressed button
{

case "Paint":
mode = "paint";
myRectangle.paint = true;
myEllipse.paint = true;
break;

case "Add Line":
mode = "place line";
break;

case "Add Ellipse":
mode = "place ellipse";
break;

case "Add Rectangle":
mode = "place rectangle";
break;

case "Delete":
for(int i = dp.shapesList.size()-1; i>=0; i--)
{
dp.shapesList.remove(i);
}
System.out.println("Canvas cleared.");
dp.repaint();
break;

case "Image":
mode = "image";
dp.repaint();
break;

case "Text":
mode = "text";
dp.repaint();
break;



//colors
case "red":
shapeColor = "red";
rectangle.setColor(Color.red);
ellipse.setColor(Color.red);
line.setColor(Color.red);
System.out.println("red");
break;

case "blue":
shapeColor = "blue";
rectangle.color = Color.blue;
ellipse.color = Color.blue;
line.color = Color.blue;
System.out.println("blue");
break;

case "green":
shapeColor = "green";
rectangle.color = Color.green;
ellipse.color = Color.green;
line.color = Color.green;
System.out.println("green");
break;

case "yellow":
shapeColor = "yellow";
rectangle.color = Color.yellow;
ellipse.color = Color.yellow;
line.color = Color.yellow;
System.out.println("yellow");
break;

case "black":
shapeColor = "black";
rectangle.color = Color.black;
ellipse.color = Color.black;
line.color = Color.black;
System.out.println("black");
break;

case "purple":
shapeColor = "purple";
rectangle.color = Color.magenta;
ellipse.color = Color.magenta;
line.color = Color.magenta;
System.out.println("magenta");
break;

default:
break;
}
}



public String buttonPressed (ActionEvent e)
{
String buttonPressed = e.getActionCommand();
return buttonPressed;
}




@Override
public void mouseClicked(MouseEvent me)
{


if(mode.equals("paint"))
{
double x = me.getX();
double y = me.getY();


for(int i = 0; i<dp.shapesList.size(); i++)
{
if(dp.shapesList.get(i).contains(x, y))
{

}
}
}

if(mode.equals("text"))
{
System.out.println("test");
double x = me.getX();
double y = me.getY();

}
}







@Override
public void mouseEntered(MouseEvent me)
{


}

@Override
public void mouseExited(MouseEvent me)
{
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent me)
{
double x = me.getX();
double y = me.getY();
startx = me.getX();
starty = me.getY();

if(mode.equals("place line"))
{
line = new myLine(startx,starty,x,y);
dp.shapesList.add(line);
dp.repaint();
}

if(mode.equals("place rectangle"))
{
rectangle = new myRectangle(startx,starty,x,y);
dp.shapesList.add(rectangle);
dp.repaint();
}

if(mode.equals("place ellipse"))
{
ellipse = new myEllipse(startx,starty,x,y);
dp.shapesList.add(ellipse);
dp.repaint();
}



}

@Override
public void mouseReleased(MouseEvent me)
{
startx = 0;
starty = 0;

}

@Override
public void mouseDragged(MouseEvent me)
{
double x = me.getX();
double y = me.getY();


if(mode.equals("place line"))
{
line = new myLine(startx,starty,x,y);
dp.shapesList.remove(dp.shapesList.size()-1);
dp.shapesList.add(line);
dp.repaint();
}

if(mode.equals("place rectangle"))
{
rectangle = new myRectangle(startx,starty,x,y);
dp.shapesList.remove(dp.shapesList.size()-1);
dp.shapesList.add(rectangle);
dp.repaint();

}

if(mode.equals("place ellipse"))
{
ellipse = new myEllipse(startx,starty,x,y);
dp.shapesList.remove(dp.shapesList.size()-1);
dp.shapesList.add(ellipse);
dp.repaint();

}


}

@Override
public void mouseMoved(MouseEvent me)
{

}


@Override
public void stateChanged(ChangeEvent ce)
{

if(ce.getSource() == bp.pointSize)
{
rectangle.setStroke(bp.pointSize.getValue());
ellipse.setStroke(bp.pointSize.getValue());
line.setStroke(bp.pointSize.getValue());
}
}


}

myRectangle 类(绘制矩形的类):` 包装颜色;

        import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

public class myRectangle implements Drawable {

private double x1, y1, x2, y2;
DrawPanel dp;
public Color color;
public static boolean paint;
public int pointSize;

public myRectangle() {

}

public myRectangle(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}

@Override
public void draw(Graphics2D g) {
double x = getStartX();
double y = getStartY();
double width = getWidth();
double height = getHeight();
Rectangle2D r = new Rectangle2D.Double(x, y, width, height);
g.setColor(color);
g.draw(r);
g.setStroke(new BasicStroke(pointSize));
if(paint)
{
g.fillRect((int)x, (int)y, (int)width, (int)height);
}

System.out.println("rectangle called");
}





public boolean contains(double x, double y) {
return x >= getStartX() && x <= getEndX() && y >= getStartY() && y <= getEndY();
}

public double getWidth() {
return Math.abs(x1 - x2);
}

public double getHeight() {
return Math.abs(y1 - y2);
}

public double getStartX() {
return Math.min(x1, x2);
}

public double getStartY() {
return Math.min(y1, y2);
}

public double getEndX() {
return Math.max(x1, x2);
}

public double getEndY() {
return Math.max(y1, y2);
}

public String getShape()
{
return "Rectangle";
}

public void setStroke(int stroke)
{
pointSize = stroke;
}


public void setColor(Color color)
{
this.color = color;
}
}`

我的DrawPanel类(在JPanel上绘制形状的类):`包颜色;

        import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class DrawPanel extends JPanel
{
public ArrayList<Drawable> shapesList = new ArrayList<Drawable>();
public static int pointSize;



public DrawPanel()
{
super();
}



@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);

// Cast the graphics object to type Graphics2D
Graphics2D g2d = (Graphics2D) g;
// Loop through the shapesList and draw every shape
for(Drawable s : shapesList)
{
s.draw(g2d);
}

}







}`

最后,这是一张图像,可以让您对我的程序有一个基本的了解:image of my program

我知道这是很多代码,但我已经在这些问题上苦苦挣扎了一段时间,我只是无法弄清楚出了什么问题,所以我最终决定发表一篇文章。

预先感谢您的帮助。

-米克

最佳答案

Long story short; I think there is something wrong with the way I instantiate new objects and how I pass values to other classes

是的,您在创建形状对象时并未设置它们的所有参数。

rectangle.setColor(Color.red);  
ellipse.setColor(Color.red);
line.setColor(Color.red);

当您单击颜色按钮时,您可以在上面设置现有对象的颜色。

rectangle = new myRectangle(startx,starty,x,y);
dp.shapesList.add(rectangle);

但是随后您创建了一个新对象并将该对象添加到列表中,但您从未设置颜色。

创建对象时需要设置所有参数。

“红色”按钮应该只在类中设置一个变量,以便在创建形状对象时使用下一个颜色。

查看Custom Painting有关在绘制矩形时设置颜色的工作示例。

此外,您通常会在将形状添加到 shapeList 后调用 repaint(),而不是使用“Paint”按钮。不需要“paint”变量。只要调用draw()方法就可以绘制形状。

关于java - 绘制形状时,形状属性未在类之间正确传递 - Java Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44191264/

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