简体   繁体   English

在JLabel上绘画 - 改变画笔颜色

[英]Painting on JLabel - change brush color

I am working on a tool that requires painting on the Icon of a Label. 我正在研究一种需要在标签图标上绘画的工具。 That works fine so far, but if I change the color of the brush, all the already-painted lines also change color. 到目前为止工作正常,但如果我改变画笔的颜色,所有已画过的线条也会改变颜色。

This is my overridden paintComponent method: 这是我重写的paintComponent方法:

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(brushColor);
        g2.setStroke(brush);
        for (int i = 1; i < point.size(); i++) {
            g2.draw(new Line2D.Float(point.get(i), point.get(i)));
        }
    }

Here's how to change my brush color: 以下是更改笔刷颜色的方法:

    public void changeBrushColor(int red, int green, int blue) {
        this.brushRed = red;
        this.brushGreen = green;
        this.brushBlue = blue;

        brushColor = new Color(red, green, blue);
        this.brush = new BasicStroke(brushWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    }

And this is how I add points to the point-Array: 这就是我向点数组添加点数的方法:

        imageLabel.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent event) {
                updateBrush();
                point.add(event.getPoint());
                imageLabel.updatePointList(point);
                repaint();
            }
        });

        imageLabel.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent event) {
                updateBrush();
                point.add(event.getPoint());
                imageLabel.updatePointList(point);
                repaint();
            }
        });

Well, you misunderstood how drawing in a JLabel or the corresponding Graphics -object works. 好吧,你误解了JLabel绘图或相应的Graphics -object是如何工作的。

There are no "already painted" lines on your JLabel , since the Graphics -object will be erased. JLabel上没有“已绘制”的行,因为Graphics -object将被删除。 The paintComponent() will draw all lines anew. paintComponent()将重新绘制所有行。

In your code you set the color for all lines before drawing. 在您的代码中,您可以在绘制之前为所有线条设置颜色。

What you have to do is to store the line color together with your points and change the color when drawing a single line. 您需要做的是将线条颜色与点一起存储,并在绘制单条线时更改颜色。

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    for (int i = 1; i < point.size(); i++) {
        g2.setColor(colors.get(i));
        g2.setStroke(brushes.get(i));
        g2.draw(new Line2D.Float(point.get(i).x, point.get(i).y));
    }
}

Well here you need 3 lists, one for color, one for brush and one for points. 那么你需要3个列表,一个用于颜色,一个用于刷子,一个用于点。 Maybe you consider to create an object which capsules these values (eg " Linedesc(color, brush, point) ") to only have a list containing them (" point = new ArrayList<LineDesc>() ") 也许您考虑创建一个对这些值进行封装的对象(例如“ Linedesc(color, brush, point) ”)只包含一个包含它们的列表(“ point = new ArrayList<LineDesc>() ”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM