forked from c0g4ih4l4n/AI_OAQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.java
145 lines (118 loc) · 4.15 KB
/
Canvas.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package testSQuares2;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
/**
* Create a JPanel on which we draw and listen for keyboard and mouse events.
*
* @author www.gametutorial.net
*/
public abstract class Canvas extends JPanel implements KeyListener, MouseListener {
// Keyboard states - Here are stored states for keyboard keys - is it down or not.
private static boolean[] keyboardState = new boolean[525];
// Mouse states - Here are stored states for mouse keys - is it down or not.
private static boolean[] mouseState = new boolean[3];
public Canvas()
{
// We use double buffer to draw on the screen.
this.setDoubleBuffered(true);
this.setFocusable(true);
this.setBackground(Color.black);
// If you will draw your own mouse cursor or if you just want that mouse cursor disapear,
// insert "true" into if condition and mouse cursor will be removed.
if(false)
{
BufferedImage blankCursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(blankCursorImg, new Point(0, 0), null);
this.setCursor(blankCursor);
}
// Adds the keyboard listener to JPanel to receive key events from this component.
this.addKeyListener(this);
// Adds the mouse listener to JPanel to receive mouse events from this component.
this.addMouseListener(this);
}
// This method is overridden in Framework.java and is used for drawing to the screen.
public abstract void Draw(Graphics2D g2d);
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
super.paintComponent(g2d);
Draw(g2d);
}
// Keyboard
/**
* Is keyboard key "key" down?
*
* @param key Number of key for which you want to check the state.
* @return true if the key is down, false if the key is not down.
*/
public static boolean keyboardKeyState(int key)
{
return keyboardState[key];
}
// Methods of the keyboard listener.
@Override
public void keyPressed(KeyEvent e)
{
keyboardState[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e)
{
keyboardState[e.getKeyCode()] = false;
keyReleasedFramework(e);
}
@Override
public void keyTyped(KeyEvent e) { }
public abstract void keyReleasedFramework(KeyEvent e);
// Mouse
/**
* Is mouse button "button" down?
* Parameter "button" can be "MouseEvent.BUTTON1" - Indicates mouse button #1
* or "MouseEvent.BUTTON2" - Indicates mouse button #2 ...
*
* @param button Number of mouse button for which you want to check the state.
* @return true if the button is down, false if the button is not down.
*/
public static boolean mouseButtonState(int button)
{
return mouseState[button - 1];
}
// Sets mouse key status.
private void mouseKeyStatus(MouseEvent e, boolean status)
{
if(e.getButton() == MouseEvent.BUTTON1)
mouseState[0] = status;
else if(e.getButton() == MouseEvent.BUTTON2)
mouseState[1] = status;
else if(e.getButton() == MouseEvent.BUTTON3)
mouseState[2] = status;
}
// Methods of the mouse listener.
@Override
public void mousePressed(MouseEvent e)
{
mouseKeyStatus(e, true);
}
@Override
public void mouseReleased(MouseEvent e)
{
mouseKeyStatus(e, false);
}
@Override
public void mouseClicked(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
}