forked from cstrahan/aduni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameTest.java
More file actions
86 lines (62 loc) · 1.76 KB
/
FrameTest.java
File metadata and controls
86 lines (62 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
/**
* Main class for game. Does window stuff and display
*/
class MyFrame extends JFrame{
public MyFrame(){
setTitle("MyFrame");
setSize(200,200); // size in pixels
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel{
Color current = Color.red;
// Handler for Mouse presses and clicks
// Extends MouseAdapator
// (which implements MouseListener with empty methods)
// We use an inner class so we can access to data on JPanel
class MouseHandler extends MouseAdapter{
// call on mouse button down
public void mousePressed(MouseEvent ev){
int x = ev.getX();
int y = ev.getY();
System.out.println("Pressed at " + x + "," + y);
}
public void mouseClicked(MouseEvent ev){
int x = ev.getX();
int y = ev.getY();
System.out.println("Clicked at " + x + "," + y);
if(current.equals(Color.red))
current = Color.blue;
else
current = Color.red;
repaint();
}
}
MyPanel(){
addMouseListener(new MouseHandler());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
// System.out.println("paintComponent");
g.setColor(current);
g.drawString("Hello World",50,50);
}
}
public class FrameTest{
// OK let's have our main() create a frame.
public static void main(String[] args){
MyFrame myframe = new MyFrame();
MyPanel mypanel = new MyPanel();
// Random stuff we just have to do (the book explains, kind of)
Container contentPane = myframe.getContentPane();
// add panel
contentPane.add(mypanel);
myframe.show();
}
}
/*
*/