Having trouble with pen transparency in paint program

55 Views Asked by At

I'm relatively new to Java, but I've made a simple paint program that allows the user to draw, change the color and size of the pen, and clear the drawing area.

The user selects a color from the JColorChooser object 'c' and this is handed off to the Color variable 'selected' through c.getColor(). I want to be able to change the transparency of the selected color, but I don't know if there is a way to convert from Color to RGB so that I can use the setColor(r, g, b, a) method. My code is below. I really appreciate any help!

Color selected;

public class ColorTool extends JPanel {
    public ColorTool() {
        super(new BorderLayout());
        b = new JLabel("Pen Color", JLabel.CENTER);
        b.setForeground(Color.black);

        c = new JColorChooser(b.getForeground());
        c.getSelectionModel().addChangeListener(new ColorListener());

        add(c, BorderLayout.PAGE_END);
    }
}

public DrawPanel() {
        setDoubleBuffered(false);
        addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {

                    for (int i = 0; i <= 100; i++) {
                        if (PS == i) {
                            oldX = (int)(e.getX() - Math.ceil(PS/2)); 
                            oldY = (int)(e.getY() - Math.ceil(PS/2));
                            if (graphics != null) {
                                graphics.setColor(selected);
                                graphics.fillOval(oldX, oldY, PS, PS);
                            }
                            repaint();
                            oldX = e.getX();
                            oldY = e.getY();
                        }
                    }
                }
            });

        addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    BasicStroke size = new BasicStroke(PS, 
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
                    currentX = e.getX();
                    currentY = e.getY();
                    if (graphics != null) {
                        graphics.setColor(selected);
                        graphics.setStroke(size);
                        graphics.drawLine(oldX, oldY, currentX, currentY);
                    }
                    repaint();
                    oldX = currentX;
                    oldY = currentY;
                }
            });

    }

public class ColorListener implements ChangeListener {
    public void stateChanged(ChangeEvent e) {
        selected = c.getColor();
    }
}
1

There are 1 best solutions below

0
On

You can access that by calling the method getComponents on the instance of Color.

From the documentation:

public float[] getComponents(float[] compArray)

Returns a float array containing the color and alpha components of the Color, in the ColorSpace of the Color. If compArray is null, an array with length equal to the number of components in the associated ColorSpace plus one is created for the return value. Otherwise, compArray must have at least this length and it is filled in with the components and returned.

Parameters: compArray - an array that this method fills with the color and alpha components of this Color in its ColorSpace and returns

Returns: the color and alpha components in a float array.