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