gpt4 book ai didi

java - 在 JComponent 中存储形状

转载 作者:行者123 更新时间:2023-12-01 21:48:05 24 4
gpt4 key购买 nike

我不确定如何使用一个形状类型的数组列表来存储多个不同的形状。这是主要内容。

public class A1 {
public static boolean rDraw = false;
public static boolean eDraw = false;
public static boolean lDraw = false;
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton rect = new JButton("Rectangle");
rect.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e){
if(e.getSource()==rect){
rDraw = true;
eDraw = false;
lDraw = false;
}
}
});
JButton ellipse = new JButton("Ellipse");
ellipse.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e){
if(e.getSource()==ellipse){
rDraw = false;
eDraw = true;
lDraw = false;
}
}
});
JButton edge = new JButton("Edge");
edge.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e){
if(e.getSource() == edge){
rDraw = false;
eDraw = false;
lDraw = true;
}
}
});
JButton label = new JButton("Label");
JTextField labelField = new JTextField(20);
JPanel panel = new JPanel();
panel.add(rect);
panel.add(ellipse);
panel.add(edge);
panel.add(label);
panel.add(labelField);
frame.add(panel,BorderLayout.NORTH);
JComponent canvas = new Canvas();
frame.add(canvas,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setTitle("Graph Draw");
frame.setVisible(true);
}
}

在我的 Canvas 中,我不确定如何使用drawShape?以及在paintComponent 中放入什么来存储形状。

public class Canvas extends JComponent {
private Point p,p2;
private Rectangle r;
private Ellipse2D ep;
private Line2D l;
ArrayList<Shape> shapes = new ArrayList<Shape>();

public Canvas(){
class MyMouseListener implements MouseListener{
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

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

}
@Override
public void mousePressed(MouseEvent e) {
if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
p = e.getPoint();
r = new Rectangle(p.x, p.y, p.x - p.x, p.y - p.y);
}
else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
p = e.getPoint();
ep = new Ellipse2D.Double(p.x, p.y, p.x - p.x, p.y - p.y);
}
else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
p = e.getPoint();
l = new Line2D.Double(p.x, p.y, p.x - p.x, p.y - p.y);
}
}
@Override
public void mouseReleased(MouseEvent e) {
if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
if (r.width != 0 || r.height != 0)
{
addRectangle(r);
}

r = null;
}
else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
if (ep.getWidth() != 0 || ep.getHeight() != 0){
addEllipse(ep);
}
ep = null;
}
else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
if (l.getP1() != null || l.getP2() != null){
addLine(l);
}
l = null;
}
}
}
class MyMouseMotionListener implements MouseMotionListener{
@Override
public void mouseDragged(MouseEvent e) {
if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
int x = Math.min(p.x, e.getX());
int y = Math.min(p.y, e.getY());
int width = Math.abs(p.x - e.getX());
int height = Math.abs(p.y - e.getY());
r.setBounds(x, y, width, height);
repaint();
}
else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
int x = Math.min(p.x, e.getX());
int y = Math.min(p.y, e.getY());
int width = Math.abs(p.x - e.getX());
int height = Math.abs(p.y - e.getY());
ep.setFrame(x, y, width, height);
repaint();
}
else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
p2 = e.getPoint();
l.setLine(p, p2);
repaint();
}
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub

}
}
MouseListener mListener = new MyMouseListener();
addMouseListener(mListener);
MouseMotionListener mmListener = new MyMouseMotionListener();
addMouseMotionListener(mmListener);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Shape shape:shapes){

}
if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
g2d.draw(r);
}
else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
g2d.draw(ep);
}
else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
g2d.draw(l);
}
}
public void addRectangle(Rectangle rect){
if(r != null){
Rectangle cR = new Rectangle(rect);
shapes.add(cR);
repaint();
}
}
public void addEllipse(Ellipse2D ellipse){
if (ep != null){
Ellipse2D cEp = new Ellipse2D.Double(ellipse.getX(),ellipse.getY(),ellipse.getWidth(),ellipse.getHeight());
shapes.add(cEp);
repaint();
}
}
public void addLine(Line2D line){
if(l != null){
Line2D cL = new Line2D.Double(line.getP1(), line.getP2());
shapes.add(cL);
repaint();
}
}
}

最佳答案

你的paintComponent方法似乎做了太多和太少的事情:太少了,因为它没有绘制形状,太多了,因为它试图用静态 boolean 字段做一些不应该做的事情。

"...and what to put in the paintComponent to store the shapes."

这不应该是paintComponent方法的一部分。此方法用于在组件上绘制项目,仅此而已。它不应该用于程序逻辑。用于存储形状的代码应该位于鼠标监听器类型代码中,而不是位于paintComponent 中。

我认为在paintComponent中你所需要的只是这样:

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Shape shape : shapes) {
g2d.draw(shape); // !! added
}
}

作为一些旁注:

  • 您的 A1 类中有太多静态代码(包括静态字段),其中大部分都应该是实例代码,并且 main 方法中的大部分代码应该被删除并放入构造函数中。 main 方法应该非常小。
  • 您需要将 Canvas 类重命名为不同的名称,因为这会与核心 Java 类 java.awt.Canvas 产生潜在的名称冲突,并且可能会让其他人感到困惑,甚至编译器也应该这样做您将来可以将此类移至另一个包中。
  • 您可以将 MouseListener 和 MouseMotionListener 代码合并到一个扩展 MouseAdapter 的类中。这将简化您的代码,并允许您避免发布空方法停止。
  • 由于您的绘图选择是互斥的,因此我将使用 JRadioButtons 或 JToggleButtons 全部保存在单个 ButtonGroup 中,以便用户可以轻松查看当前处于 Activity 状态的选择,并且选择一个按钮会关闭对所有按钮的选择其他人。

关于java - 在 JComponent 中存储形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35672740/

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