Skip to content
Advertisement

How to make Color change when mouse clicked in java?

I’m trying to make my own Java GUI project. I want to make the line’s colors change when mouse pressed, but this doesn’t work. I used ‘for’loop and array for Colors but this doesn’t run. So I’d like to ask you help me to solve it! Also, I wonder why loop needs for drawing lines on panel.

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GraphicEx extends JFrame {
    private MyPanel panel = new MyPanel();

    public GraphicEx(){
    setTitle("Java Mondrian");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setContentPane(panel);
    setSize(400,220);
    setVisible(true);
    }

    class MyPanel extends JPanel{

        private Vector <Point> vStart = new Vector <Point>();
        private Vector <Point> vEnd = new Vector <Point>();
        Color [] c = {Color.BLUE, Color.RED, Color.YELLOW, Color.BLACK};
        private int a;
        MyPanel(){
        setBackground(Color.WHITE);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
                Point startP = e.getPoint();
                vStart.add(startP);
                for(int i=0; i<c.length; i++) {
                    if (i== (c.length-1)){
                        i=0;
                    }
                    a = i;
                    }
            }

I made this for Color Change, But this not work.

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                Point endP = e.getPoint();
                vEnd.add(endP);
                repaint();
            }



        });
    }       

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            //component Color & Size
            g.setColor(Color.BLACK);
            g.drawRect(10,10,50,50);
            g.setColor(Color.BLUE);
            g.fillRect(60, 60, 100, 100);
            g.setColor(Color.RED);
            g.fillRect(50,50,20,20);
            g.setColor(Color.YELLOW);
            g.fillRect(130,50,50,50);
            g.setColor(Color.RED);
            g.drawRect(170,10,50,50);
            g.setColor(Color.BLACK);
            g.fillRect(210,50,80,50);
            g.setColor(Color.YELLOW);
            g.drawRect(260,30,40,170);
            g.setColor(Color.RED);
            g.fillRect(240,130,170,40);
            g.setColor(new Color(0,0,0));
            g.setFont(new Font("Arial",Font.ITALIC, 30));
            g.drawString("Mondrian.2020", 100, 174);

            g.setColor(new Color(0,210,200));
            g.setFont(new Font("Arial",Font.BOLD,20));
            g.drawString("Draw your own Picture", 70, 100);

            g.setColor(new Color(0,0,0));
            g.drawLine(20,20,350,20);
            g.drawLine(35,0,35,180);
            g.drawLine(20,160,350,160);
            g.drawLine(330, 0, 330, 190);
            int [] x = {80, 40, 80, 120};
            int [] y = {40, 120, 200, 120};
            g.drawPolygon(x,y,4);
            g.setColor(Color.BLUE);
            g.fillArc(290, 10, 50, 50, 90, 360);


            for(int i=0; i<vStart.size();i++) { 
                Point s = vStart.elementAt(i);
                Point e = vEnd.elementAt(i);
                g.setColor(c[a]);
                g.drawLine((int)s.getX(), (int)s.getY(), (int)e.getX(), (int)e.getY());
            }
        }

    }

I cannot understand this part too! why I should use loop to draw lines?

    public static void main(String[] args) {
        new GraphicEx();
    }

}

Advertisement

Answer

As there’s only a limited supply of colors, and each line should have it’s own, the modulo operator seems fitting:

        //Make sure both vectors have that index!
        for(int i=0; i< Math.min(vStart.size(), vEnd.size()); i++) { 
            Point s = vStart.elementAt(i);
            Point e = vEnd.elementAt(i);
            g.setColor(c[i % c.length]); //Use a calculated color using modulo length
            g.drawLine((int)s.getX(), (int)s.getY(), (int)e.getX(), (int)e.getY());
        }

Also it’s not good to access elements that are not there (yet). Painting could happen at any time – so there might be start items with no end items.

If this works, you could get rid of the whole calculation of a as well.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement