COMP2396
Loading...
Searching...
No Matches
question1.java
Go to the documentation of this file.
1import javax.swing.*;
2import java.awt.*;
3import java.awt.event.*;
4
5public class question1 {
6 // put drawing panel at this scope since
7 // listeners need modify it
8 MyDrawPanel drawPanel = new MyDrawPanel();
9 // also the coordinates of the rectangle
10 int x = 95;
11 int y = 70;
12 // and size
13 int size_x = 100;
14 int size_y = 100;
15 // put frame here because we want to repaint frame as a whole in listeners
16 JFrame frame = new JFrame();
17 public static void main(String[] args) {
18 question1 gui = new question1();
19 gui.go();
20 }
21 public void go() {
22 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23 // putting all buttons together
24 JPanel panel = new JPanel();
25 frame.add(panel, BorderLayout.NORTH);
26 // all buttons
27 JButton left = new JButton("left");
28 JButton right = new JButton("right");
29 JButton smaller = new JButton("smaller");
30 JButton bigger = new JButton("Bigger");
31 panel.add(left);
32 panel.add(right);
33 panel.add(smaller);
34 panel.add(bigger);
35 // adding listeners for buttons
36 left.addActionListener(new LeftListener());
37 right.addActionListener(new RightListener());
38 smaller.addActionListener(new SmallerListener());
39 bigger.addActionListener(new BiggerListener());
40 // drawing panel
41 frame.add(drawPanel);
42 // preparing to launch
43 frame.setSize(300, 300);
44 frame.setVisible(true);
45 }
46 class LeftListener implements ActionListener {
47 public void actionPerformed(ActionEvent event) {
48 Timer timer = new Timer(0, new ActionListener() {
49 int count = 0;
50 public void actionPerformed(ActionEvent e) {
51 if (count < 20) {
52 x--;
53 drawPanel.repaint();
54 count++;
55 } else {
56 ((Timer) e.getSource()).stop();
57 }
58 }
59 });
60 timer.start();
61 }
62 }
63 class RightListener implements ActionListener {
64 public void actionPerformed(ActionEvent event) {
65 Timer timer = new Timer(0, new ActionListener() {
66 int count = 0;
67 public void actionPerformed(ActionEvent e) {
68 if (count < 20) {
69 x++;
70 drawPanel.repaint();
71 count++;
72 } else {
73 ((Timer) e.getSource()).stop();
74 }
75 }
76 });
77 timer.start();
78 }
79 }
80 class SmallerListener implements ActionListener {
81 public void actionPerformed(ActionEvent event) {
82 Timer timer = new Timer(0, new ActionListener() {
83 int count = 0;
84 public void actionPerformed(ActionEvent e) {
85 if (count < 20) {
86 size_x -= 1;
87 size_y -= 1;
88 drawPanel.repaint();
89 count++;
90 } else {
91 ((Timer) e.getSource()).stop();
92 }
93 }
94 });
95 timer.start();
96 }
97 }
98 class BiggerListener implements ActionListener {
99 public void actionPerformed(ActionEvent event) {
100 Timer timer = new Timer(0, new ActionListener() {
101 int count = 0;
102 public void actionPerformed(ActionEvent e) {
103 if (count < 20) {
104 size_x += 1;
105 size_y += 1;
106 drawPanel.repaint();
107 count++;
108 } else {
109 ((Timer) e.getSource()).stop();
110 }
111 }
112 });
113 timer.start();
114 }
115 }
116 class MyDrawPanel extends JPanel {
117 public void paintComponent(Graphics g) {
118 int red = 100;
119 int green = 100;
120 int blue = 100;
121 g.clearRect(0, 0, this.getWidth(), this.getHeight());
122 g.setColor(new Color(red, green, blue));
123 g.fillRect(x, y, size_x, size_y);
124 }
125 }
126}
void go()
static void main(String[] args)