Programming via Java
Chapter 25. More with Swing
25.1. A customized component
1 import java.awt.BorderLayout; import java.awt.Color;
2 import java.awt.Container; import java.awt.Dimension;
3 import java.awt.Graphics; import java.awt.Point;
4 import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
5 import java.util.ArrayList; import javax.swing.JButton;
6 import javax.swing.JComponent; import javax.swing.JFrame;
7
8 public class DotsComponent extends JComponent implements ActionListener {
9 private ArrayList<Point> centers;
10
11 public DotsComponent() {
12 centers = new ArrayList<Point>();
13 setPreferredSize(new Dimension(250, 250));
14 }
15
16 public void paintComponent(Graphics g) {
17 g.setColor(Color.WHITE);
18 g.fillRect(0, 0, getWidth(), getHeight());
19 g.setColor(new Color(0, 128, 0));
20 for(int i = 0; i < centers.size(); i++) {
21 Point p = centers.get(i);
22 int px = (int) Math.round(p.getX());
23 int py = (int) Math.round(p.getY());
24 g.fillOval(px - 10, py - 10, 20, 20);
25 }
26 }
27
28 public void actionPerformed(ActionEvent e) {
29 int x = (int) (Math.random() * this.getWidth());
30 int y = (int) (Math.random() * this.getHeight());
31 centers.add(new Point(x, y));
32 repaint();
33 }
34
35 public static void main(String[] args) {
36 DotsComponent dots = new DotsComponent();
37 JButton add = new JButton("Add");
38 add.addActionListener(dots);
39
40 JFrame frame = new JFrame("Click Component");
41 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
42 Container contents = frame.getContentPane();
43 contents.add(dots, BorderLayout.CENTER);
44 contents.add(add, BorderLayout.SOUTH);
45 frame.pack();
46 frame.setVisible(true);
47 }
48 }
25.2. Handling mouse interaction
1 import java.awt.Color; import java.awt.Container;
2 import java.awt.Dimension; import java.awt.Graphics;
3 import java.awt.Point; import java.awt.event.MouseEvent;
4 import java.awt.event.MouseListener; import java.util.ArrayList;
5 import javax.swing.JComponent; import javax.swing.JFrame;
6
7 public class ClickComponent extends JComponent implements MouseListener {
8 private ArrayList<Point> centers;
9
10 public ClickComponent() {
11 centers = new ArrayList<Point>();
12 addMouseListener(this);
13 setPreferredSize(new Dimension(250, 250));
14 }
15
16 public void paintComponent(Graphics g) {
17 g.setColor(Color.WHITE);
18 g.fillRect(0, 0, getWidth(), getHeight());
19 g.setColor(new Color(0, 128, 0));
20 for(int i = 0; i < centers.size(); i++) {
21 Point p = centers.get(i);
22 int px = (int) Math.round(p.getX());
23 int py = (int) Math.round(p.getY());
24 g.fillOval(px - 10, py - 10, 20, 20);
25 }
26 }
27
28 public void mousePressed(MouseEvent e) {
29 centers.add(new Point(e.getX(), e.getY()));
30 repaint();
31 }
32 public void mouseReleased(MouseEvent e) { }
33 public void mouseEntered(MouseEvent e) { }
34 public void mouseExited(MouseEvent e) { }
35 public void mouseClicked(MouseEvent e) { }
36
37 public static void main(String[] args) {
38 JFrame frame = new JFrame("Click Component");
39 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40 Container contents = frame.getContentPane();
41 contents.add(new ClickComponent());
42 frame.pack();
43 frame.setVisible(true);
44 }
45 }