Question

I'm kinda new to java and have been trying to make a simple paint program, I have gotten everything to work except the color of the paint brush. Rigth now I set the color to blue but I want to make the color of the paint brush the same color as the color selected by the color slider.

Here's the code I got so far

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Paint extends JFrame implements ChangeListener{
    PaintPanel color;
    PaintPanel2 paint;
    JSlider red;
    JSlider green;
    JSlider blue;

    public Paint(){
        super("Paint");
        setSize(300,290);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        color = new PaintPanel();
        paint = new PaintPanel2();
        red = new JSlider(0,255,255);
        green = new JSlider(0,255,0);
        blue = new JSlider(0,255,0);

        red.setMajorTickSpacing(50);
        red.setMinorTickSpacing(10);
        red.setPaintTicks(true);
        red.setPaintLabels(true);
        red.addChangeListener(this);

        green.setMajorTickSpacing(50);
        green.setMinorTickSpacing(10);
        green.setPaintTicks(true);
        green.setPaintLabels(true);
        green.addChangeListener(this);

        blue.setMajorTickSpacing(50);
        blue.setMinorTickSpacing(10);
        blue.setPaintTicks(true);
        blue.setPaintLabels(true);
        blue.addChangeListener(this);

        JLabel redLabel = new JLabel("Red: ");
        JLabel greenLabel = new JLabel("Green: ");
        JLabel blueLabel = new JLabel("Blue: ");
        GridLayout grid = new GridLayout(5,1);
        FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
        setLayout(grid);

        JPanel redPanel = new JPanel();
        redPanel.setLayout(flow);
        redPanel.add(redLabel);
        redPanel.add(red);
        add(redPanel);

        JPanel greenPanel = new JPanel();
        greenPanel.setLayout(flow);
        greenPanel.add(greenLabel);
        greenPanel.add(green);
        add(greenPanel);

        JPanel bluePanel = new JPanel();
        bluePanel.setLayout(flow);
        bluePanel.add(blueLabel);
        bluePanel.add(blue);
        add(bluePanel);       
        add(color);
        add(paint);

        setVisible(true);
    }

    public void stateChanged(ChangeEvent e){
        JSlider source = (JSlider) e.getSource();
        if(source.getValueIsAdjusting() != true){
            Color mainColor = new Color(red.getValue(),
                green.getValue(),
                blue.getValue());
            color.changeColor(mainColor);
            color.repaint();
        }
    }

    public static void main(String[] args){
        Paint p = new Paint();
    }
}

class PaintPanel extends JPanel{
    Color background;

    public PaintPanel(){
        background = Color.red;
    }

    public void paintComponent(Graphics comp){
        Graphics2D comp2D = (Graphics2D) comp;
        comp2D.setColor(background);
        comp2D.fillRect(0,0,getSize().width,getSize().height);
    }

    void changeColor(Color newBackground){
        background = newBackground;
    }
}

class PaintPanel2 extends JPanel{
    Image image;
    Graphics2D comp2D;
    int currentX, currentY, oldX, oldY;
    PaintPanel color;

    public PaintPanel2(){
        color = new PaintPanel();      

        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                oldX = e.getX();
                oldY = e.getY();
            }
        });

        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                currentX = e.getX();
                currentY = e.getY();
                if(comp2D != null)
                comp2D.drawLine(oldX, oldY, currentX, currentY);
                repaint();
                oldX = currentX;
                oldY = currentY;
            }
        });
    }

    public void paintComponent(Graphics comp){
        if(image == null){
            image = createImage(getSize().width, getSize().height);
            comp2D = (Graphics2D)image.getGraphics();
            comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            comp2D.setPaint(Color.white);
            comp2D.fillRect(0, 0, getSize().width, getSize().height);
            comp2D.setPaint(Color.blue);
            repaint();
        }
        comp.drawImage(image, 0, 0, null);
    }
}
Was it helpful?

Solution

The problem was that you weren't setting the chosen color in PaintPanel2. I changed the stateChanged method and the PaintPanel2 as follows, and now it works as I assume you intended:

public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider) e.getSource();
        if (source.getValueIsAdjusting() != true) {
            Color mainColor = new Color(red.getValue(),
                    green.getValue(),
                    blue.getValue());
            color.changeColor(mainColor);
            paint.setPaintColor(mainColor);
            color.repaint();
        }
    }

class PaintPanel2 extends JPanel {

    Image image;
    Graphics2D comp2D;
    int currentX, currentY, oldX, oldY;

    public PaintPanel2() {

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                oldX = e.getX();
                oldY = e.getY();
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                currentX = e.getX();
                currentY = e.getY();
                if (comp2D != null) {
                    comp2D.drawLine(oldX, oldY, currentX, currentY);
                }
                repaint();
                oldX = currentX;
                oldY = currentY;
            }
        });
    }

    public void paintComponent(Graphics comp) {
        if (image == null) {
            image = createImage(getSize().width, getSize().height);
            comp2D = (Graphics2D) image.getGraphics();
            comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            comp2D.setPaint(Color.white);
            comp2D.fillRect(0, 0, getSize().width, getSize().height);
            comp2D.setPaint(Color.blue);
            repaint();
        }
        comp.drawImage(image, 0, 0, null);
    }

    public void setPaintColor(Color color) {
        comp2D.setColor(color);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top