我一直致力于Java Paint应用程序的练习;但是,我现在陷入困境的部分是如何在不改变我之前绘制的任何颜色的情况下改变笔的颜色?我被建议创建另一个ArrayList并将其合并到我的paintComponent中,但现在我很困惑并且不确定该怎么做。谁能帮我?我没有包含我的测试人员类,但它已经创建了按钮,这正是我的代码所做的。
package drawing;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Drawing extends JPanel {
private final ArrayList<Point> points = new ArrayList<>();
private boolean drawingInProgress;
private Color shapeColor = Color.BLACK;
public void setShapeColor(Color color)
{
this.shapeColor = color;
}
public Drawing(){
setBackground(Color.white);
drawingInProgress = false;
addMouseListener(
new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent ev)
{
if(!drawingInProgress)
{
drawingInProgress = true;
} else {
drawingInProgress = false;
}
}
}
);
addMouseMotionListener(
new MouseMotionAdapter(){
@Override
public void mouseMoved(MouseEvent event)
{
if (drawingInProgress){
points.add(event.getPoint());
repaint();
} else {
}
}
}
);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//g.setColor(shapeColor); What I had before that was wrong.
for (Point point: points)
g.fillOval(point.x, point.y, 8, 8);
}
public void red() {
shapeColor = Color.RED;
repaint();
}
public void blue() {
shapeColor = Color.BLUE;
repaint();
}
public void green() {
shapeColor = Color.GREEN;
repaint();
}
}
答案 0 :(得分:1)
您可以创建伪“形状”,它不仅包含自己绘制所需的信息,还包含颜色(以及任何其他属性)
public interface PaintShape {
public Rectangle getBounds();
public Color getColor();
public void paint(Graphics2D g2d);
}
然后你可以创造出你想要的形状......
public abstract class AbstractPaintShape implements PaintShape {
private final Rectangle bounds;
private final Color color;
public AbstractPaintShape(Rectangle bounds, Color color) {
this.bounds = bounds;
this.color = color;
}
@Override
public Rectangle getBounds() {
return bounds;
}
@Override
public Color getColor() {
return color;
}
}
public class OvalPaintShape extends AbstractPaintShape {
private Ellipse2D oval;
public OvalPaintShape(Rectangle bounds, Color color) {
super(bounds, color);
oval = new Ellipse2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight());
}
@Override
public void paint(Graphics2D g2d) {
g2d.setColor(getColor());
g2d.fill(oval);
}
}
或类似的东西
您应该仔细查看2D Graphics和Working with Geometry以获取更多想法
答案 1 :(得分:0)
一种方法是创建一个类似MyShape
的类,其中包含一系列点(如您已有)和颜色。
对于整个绘画,您将获得此类MyShape
个对象的列表。
现在,每当用户开始绘制一条线(即鼠标按下)时,您将使用当前颜色创建一个新的MyShape
对象,并收集该对象的点列表中的所有鼠标移动,直到鼠标 - 起来。
在repaint
中,您将在形状列表中绘制所有MyShape,每个MyShape都有自己的颜色。
答案 2 :(得分:0)
在这种情况下,您存储点并重新绘制点列表,您必须使用点信息保存每个Point
的颜色,然后在绘制时询问每个点它是什么颜色。也许创建一个ColoredPoint
的课程Color
。
for (ColoredPoint cp : points) {
g.setColor(cp.getColor());
...
答案 3 :(得分:0)
你可以重构它以将形状分别存储到绘制它们的位置。
public class Shape {
private final ArrayList<Point> points = new ArrayList<>();
public final Color color;
public Shape() {
this(Color.BLACK);
}
public Shape(final Color color) {
this.color = color;
}
public void addPoint(final Point point) {
this.points.add(point);
}
public List<Point> getPoints() {
return points;
}
}
您的绘图课程将具有
public class Drawing extends JPanel {
private final List<Shape> shapes = new ArrayList<>():
// ...
// a mouse listener that creates shapes and gives them points
// ...
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Shape shape: shapes
g.setColor(shape.color);
for (Point point: shape.getPoints()) {
g.fillOval(point.x, point.y, 8, 8);
}
}
}