Home > Software engineering >  Change color of drawing by clicking on it AWT JAVA
Change color of drawing by clicking on it AWT JAVA

Time:11-28

I want to add to this code the functionality to change color of drawing by clicking on it, but i don't know if this will code support that

import java.awt.*;
import java.awt.event.*;
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);

        public Composants() {
            setLayout(new BorderLayout());
           
            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) {
                    // Randomise the colors
                    Collections.shuffle(avaliableColors);
                    color = avaliableColors.get(0);
                    repaint();
                }
            });
            add(btn, BorderLayout.SOUTH);
            
        }

        @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();
        }
        
    }
}

What i found on the internet is to create Rectangle class and Circle , but if i could do it just here it will be great, thank you.

What i found on the internet is to create Rectangle class and Circle , but if i could do it just here it will be great, thank you.(duplicate for submitting)

CodePudding user response:

See How to Write a Mouse Listener and Working with Geometry

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

    }
}

CodePudding user response:

You should try to make your question clearer. Are you asking if the code will change the color of the circles and rectangle?

If you run this code, you should get an error on line 76 because you can't cast an 'anonymous java.awt.event.WindowAdapter' to 'java.awt.event.ComponentListener'. If you remove the close() method, the color of the circles and rectangle will change.

  • Related