Change color of drawing when clicked


Joined
Nov 28, 2021
Messages
2
Reaction score
0
I have multiple shapes , and i wan't when i click on one, only it changes color.
this code is not doing what i want, when i click on one shape, all the other shapes change color.
I didn't know how to modify this to do what i want.
Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;

public class TestGraphic {

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

    public TestGraphic() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame frame = new Frame();
                frame.add(new Composants());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                frame.addWindowListener((WindowListener) new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent windowEvent) {
                        System.exit(0);
                    }
                });

            }
        });
    }

    public class Composants extends Container {

        private Color color = Color.BLACK;
        private List<Color> avaliableColors = new ArrayList<>(16);
        private Shape face;

        public Composants() {
            setLayout(new BorderLayout());

            Area area = new Area();
            area.add(new Area(new Ellipse2D.Double(108, 110, 200, 200)));
            area.add(new Area(new Ellipse2D.Double(160, 150, 20, 20)));
            area.add(new Area(new Ellipse2D.Double(240, 150, 20, 20)));
            area.add(new Area(new Rectangle2D.Double(160, 220, 100, 40)));
            face = area;

            avaliableColors.add(Color.BLACK);
            avaliableColors.add(Color.BLUE);
            avaliableColors.add(Color.CYAN);
            avaliableColors.add(Color.DARK_GRAY);
            avaliableColors.add(Color.GRAY);
            avaliableColors.add(Color.GREEN);
            avaliableColors.add(Color.LIGHT_GRAY);
            avaliableColors.add(Color.MAGENTA);
            avaliableColors.add(Color.ORANGE);
            avaliableColors.add(Color.PINK);
            avaliableColors.add(Color.RED);
            avaliableColors.add(Color.WHITE);
            avaliableColors.add(Color.YELLOW);

            JButton btn = new JButton("Change color");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    changeColor();
                }
            });
            add(btn, BorderLayout.SOUTH);

            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (face.contains(e.getPoint())) {
                        changeColor();
                    }
                }
            });
        }

        protected void changeColor() {
            // Randomise the colors
            Collections.shuffle(avaliableColors);
            color = avaliableColors.get(0);
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 400);
        }

        public void close() {
            addComponentListener((ComponentListener) new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent windowEvent) {
                    System.exit(0);
                }
            });
        }

        @Override
        public void paint(Graphics gui) {
            super.paint(gui);
            Graphics2D g2d = (Graphics2D) gui.create();
            g2d.setColor(color);
            g2d.drawOval(108, 110, 200, 200);
            g2d.drawOval(160, 150, 20, 20);
            g2d.drawOval(240, 150, 20, 20);
            g2d.drawRect(160, 220, 100, 40);
            g2d.dispose();
        }

    }
}
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
174
Reaction score
19
Hello, there, and welcome! I'd recommend creating a class that stores a shape to draw and the color in which to draw it. Then, store a list of those objects in Composants and iterate through the list for painting and mouse click checks. I've done that in the following. I tried to change as little of your code as possible. Note well that I had to change the order of the shapes being drawn so that the largest shape is last; this allows smaller shapes to check for a click before the large one that contains them.

Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Point;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;

public class TestGraphic {

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

    public TestGraphic() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame frame = new Frame();
                frame.add(new Composants());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                frame.addWindowListener((WindowListener) new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent windowEvent) {
                        System.exit(0);
                    }
                });

            }
        });
    }

    public class Composant {
        private Area area;
        private Color color = Color.BLACK;
        
        public Composant(Area area){
            this.area = area;
        }
        public void changeColor(Color color){
            this.color = color;
        }
        public void paint(Graphics2D g){
            g.setColor(color);
            g.draw(area);
        }
        public boolean contains(Point p){
            return area.contains(p);
        }
    }
    public class Composants extends Container {
        private List<Color> avaliableColors = new ArrayList<>(16);
        private List<Composant> composants = new ArrayList<>(4);

        public Composants() {
            setLayout(new BorderLayout());

            composants.add(new Composant(new Area(new Ellipse2D.Double(160, 150, 20, 20))));
            composants.add(new Composant(new Area(new Ellipse2D.Double(240, 150, 20, 20))));
            composants.add(new Composant(new Area(new Rectangle2D.Double(160, 220, 100, 40))));
            composants.add(new Composant(new Area(new Ellipse2D.Double(108, 110, 200, 200))));

            avaliableColors.add(Color.BLACK);
            avaliableColors.add(Color.BLUE);
            avaliableColors.add(Color.CYAN);
            avaliableColors.add(Color.DARK_GRAY);
            avaliableColors.add(Color.GRAY);
            avaliableColors.add(Color.GREEN);
            avaliableColors.add(Color.LIGHT_GRAY);
            avaliableColors.add(Color.MAGENTA);
            avaliableColors.add(Color.ORANGE);
            avaliableColors.add(Color.PINK);
            avaliableColors.add(Color.RED);
            avaliableColors.add(Color.WHITE);
            avaliableColors.add(Color.YELLOW);

            JButton btn = new JButton("Change color");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    changeColor();
                }
            });
            add(btn, BorderLayout.SOUTH);

            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    for (Composant c : composants){
                        if (c.contains(e.getPoint())) {
                            changeColor(c);
                            break;
                        }
                    }
                }
            });
        }
        
        protected void changeColor(){
            Collections.shuffle(avaliableColors);
            Color col = avaliableColors.get(0);
            for (Composant c : composants){
                c.changeColor(col);
            }
            repaint();
        }

        protected void changeColor(Composant c) {
            // Randomise the colors
            Collections.shuffle(avaliableColors);
            c.changeColor(avaliableColors.get(0));
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 400);
        }

        public void close() {
            addComponentListener((ComponentListener) new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent windowEvent) {
                    System.exit(0);
                }
            });
        }

        @Override
        public void paint(Graphics gui) {
            super.paint(gui);
            Graphics2D g2d = (Graphics2D) gui.create();
            for (Composant c : composants){
                c.paint(g2d);
            }
            g2d.dispose();
        }

    }
}
 
Ad

Advertisements

Joined
Nov 28, 2021
Messages
2
Reaction score
0
Hello, there, and welcome! I'd recommend creating a class that stores a shape to draw and the color in which to draw it. Then, store a list of those objects in Composants and iterate through the list for painting and mouse click checks. I've done that in the following. I tried to change as little of your code as possible. Note well that I had to change the order of the shapes being drawn so that the largest shape is last; this allows smaller shapes to check for a click before the large one that contains them.

Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Point;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;

public class TestGraphic {

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

    public TestGraphic() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame frame = new Frame();
                frame.add(new Composants());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                frame.addWindowListener((WindowListener) new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent windowEvent) {
                        System.exit(0);
                    }
                });

            }
        });
    }

    public class Composant {
        private Area area;
        private Color color = Color.BLACK;
       
        public Composant(Area area){
            this.area = area;
        }
        public void changeColor(Color color){
            this.color = color;
        }
        public void paint(Graphics2D g){
            g.setColor(color);
            g.draw(area);
        }
        public boolean contains(Point p){
            return area.contains(p);
        }
    }
    public class Composants extends Container {
        private List<Color> avaliableColors = new ArrayList<>(16);
        private List<Composant> composants = new ArrayList<>(4);

        public Composants() {
            setLayout(new BorderLayout());

            composants.add(new Composant(new Area(new Ellipse2D.Double(160, 150, 20, 20))));
            composants.add(new Composant(new Area(new Ellipse2D.Double(240, 150, 20, 20))));
            composants.add(new Composant(new Area(new Rectangle2D.Double(160, 220, 100, 40))));
            composants.add(new Composant(new Area(new Ellipse2D.Double(108, 110, 200, 200))));

            avaliableColors.add(Color.BLACK);
            avaliableColors.add(Color.BLUE);
            avaliableColors.add(Color.CYAN);
            avaliableColors.add(Color.DARK_GRAY);
            avaliableColors.add(Color.GRAY);
            avaliableColors.add(Color.GREEN);
            avaliableColors.add(Color.LIGHT_GRAY);
            avaliableColors.add(Color.MAGENTA);
            avaliableColors.add(Color.ORANGE);
            avaliableColors.add(Color.PINK);
            avaliableColors.add(Color.RED);
            avaliableColors.add(Color.WHITE);
            avaliableColors.add(Color.YELLOW);

            JButton btn = new JButton("Change color");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    changeColor();
                }
            });
            add(btn, BorderLayout.SOUTH);

            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    for (Composant c : composants){
                        if (c.contains(e.getPoint())) {
                            changeColor(c);
                            break;
                        }
                    }
                }
            });
        }
       
        protected void changeColor(){
            Collections.shuffle(avaliableColors);
            Color col = avaliableColors.get(0);
            for (Composant c : composants){
                c.changeColor(col);
            }
            repaint();
        }

        protected void changeColor(Composant c) {
            // Randomise the colors
            Collections.shuffle(avaliableColors);
            c.changeColor(avaliableColors.get(0));
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 400);
        }

        public void close() {
            addComponentListener((ComponentListener) new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent windowEvent) {
                    System.exit(0);
                }
            });
        }

        @Override
        public void paint(Graphics gui) {
            super.paint(gui);
            Graphics2D g2d = (Graphics2D) gui.create();
            for (Composant c : composants){
                c.paint(g2d);
            }
            g2d.dispose();
        }

    }
}
Thank you for helping, i really learned a lot from your response/code :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top