Question

I'm writing a "paint" analog program (it draws rectangles and lines from HashSets), and I have a problem to color dots and lines. Here's my code:

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;
}

}

Now, when I pressed black or green button console printed java.awt.Color[r=0,g=0,b=0] or java.awt.Color[r=0,g=255,b=0]. But when I tried to draw lines it printed null.

How could I change method Draw.getColor() to make changing color possible?

Was it helpful?

Solution

Each time paintComponent is called, you are using what ever color was set last.

What you need to do is associate the color with the current activity, so that when paintComponent is called, you can look up the color for the shape you are drawing.

You could create a custom class which contained the Line and color values and add those to your List. When you paint the Line, you would look up the color to use as well

You would need to do the same thing for your points

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top