无法在paintComponent中设置颜色

时间:2014-04-04 20:28:12

标签: java paint paintcomponent

我正在编写" paint" 模拟程序(它从HashSets中绘制矩形和线条),我遇到了问题彩色点和线。这是我的代码:

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()以改变颜色?

1 个答案:

答案 0 :(得分:3)

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

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

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

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