gpt4 book ai didi

java - 无法在 paintComponent 中设置颜色

转载 作者:行者123 更新时间:2023-11-30 08:24:34 24 4
gpt4 key购买 nike

我正在编写一个“绘画” 模拟程序(它从HashSet 中绘制矩形和线条),但我在为点和线条着色时遇到了问题。这是我的代码:

package paint;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.HashSet;

import javax.swing.JComponent;

public class Draw extends JComponent {
private HashSet<Point2D> points = new HashSet<>();
private HashSet<Line2D> lines = new HashSet<>();
private final int WIDTH = 5, HEIGHT = 5;
private Point2D prevPoint, nextPoint;

public Draw() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
if (e.getButton() == MouseEvent.BUTTON1) {
prevPoint = e.getPoint();
points.add(prevPoint);
repaint();
}
}

});

addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
nextPoint = e.getPoint();
Line2D line = new Line2D.Double(prevPoint, nextPoint);
lines.add(line);
prevPoint = nextPoint;
repaint();
}
});
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Point2D p : points) {
int x = (int)p.getX() - (WIDTH / 2);
int y = (int)p.getY() - (HEIGHT / 2);
g2d.setColor(getColor());
g2d.fillRect(x, y, WIDTH, HEIGHT);
g2d.drawRect(x, y, WIDTH, HEIGHT);
}

for (Line2D l : lines) {
g2d.setColor(getColor());
g2d.setStroke(new BasicStroke(WIDTH));
g2d.draw(l);
}
}

public Color getColor() {
Tools t = new Tools();
return t.getColor();
}

}


package paint;

import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Tools extends JPanel {
private Color color;
private JButton button;
private ButtonColor bc;

public Tools() {
makeButton("black", Color.BLACK);
makeButton("green", Color.GREEN);
}

public void makeButton(String name, final Color color) {
bc = new ButtonColor(name, color);
add(new JButton(bc));
}

private class ButtonColor extends AbstractAction {

public ButtonColor(String name, Color color) {
putValue(NAME, name);
putValue(SHORT_DESCRIPTION, "Choose " + name.toLowerCase() + " color");
putValue("color", color);
}

@Override
public void actionPerformed(ActionEvent e) {
color = (Color) getValue("color");
System.out.println(color);
}

}

public Color getColor() {
System.out.println(color);
return color;
}

}

现在,当我按下黑色或绿色按钮时控制台打印 java.awt.Color[r=0,g=0,b=0]java.awt.Color[r =0,g=255,b=0]。但是当我尝试画线时,它打印出 null

我如何改变方法 Draw.getColor() 来改变颜色?

最佳答案

每次调用 paintComponent 时,您都会使用最后设置的颜色。

您需要做的是将颜色与当前 Activity 相关联,以便在调用 paintComponent 时,您可以查找正在绘制的形状的颜色。

您可以创建一个自定义类,其中包含 Line 和颜色值,并将它们添加到您的列表中。当您绘制 Line 时,您也会查找要使用的颜色

你需要为你的积分做同样的事情

关于java - 无法在 paintComponent 中设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22872421/

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