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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
| import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ExemplePauseSwing1 extends JPanel {
public static int RAY = 10;
private static int DIAM = RAY*2;
private static Font FONT1 = new Font("Arial", Font.BOLD, 25);
private static Font FONT2 = new Font("Arial", Font.BOLD, 10);
private int x;
private int y;
private boolean paused;
private boolean mode;
public ExemplePauseSwing1() {
super(true);
setBackground(Color.BLACK);
x=RAY;
y=RAY;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if ( paused ) {
g.setColor(Color.LIGHT_GRAY);
}
else {
g2d.setPaint(Color.WHITE);
}
g.fillOval(x-RAY, y-RAY, DIAM, DIAM);
if ( paused ) {
g.setColor(Color.YELLOW);
g.setFont(FONT1);
FontMetrics fm = g.getFontMetrics();
g.drawString("PAUSE", (int)((getWidth()-fm.getStringBounds("PAUSE", g).getWidth())/2), (getHeight()+fm.getAscent())/2);
}
else {
g.setColor(Color.BLUE);
g2d.setXORMode(Color.WHITE);
g.setFont(FONT2);
FontMetrics fm = g.getFontMetrics();
g.drawString("Mode " + (mode?2:1), 5, (getHeight()-5-fm.getAscent()+fm.getAscent()));
}
}
public void setPaused(boolean paused) {
if ( this.paused!=paused ) {
this.paused=paused;
repaint();
}
}
public void setMode(boolean mode) {
if ( this.mode!=mode ) {
this.mode=mode;
repaint();
}
}
public void moveBall(int dx, int dy) {
if ( !paused ) {
x+=dx;
y+=dy;
if ( x-RAY<0 ) {
x=RAY;
}
else if ( x+RAY>=getWidth() ) {
x=getWidth()-RAY-1;
}
if ( y-RAY<0 ) {
y=RAY;
}
else if ( y+RAY>=getHeight() ) {
y=getHeight()-RAY-1;
}
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ExemplePauseSwing1 panel = new ExemplePauseSwing1();
frame.add(panel);
class GameLoop implements ActionListener {
private boolean pause;
private boolean up;
private boolean down;
private boolean right;
private boolean left;
private int dx;
private int dy;
private boolean mode;
@Override
public void actionPerformed(ActionEvent e) {
panel.moveBall(dx,dy);
}
public void setPause(boolean pause) {
this.pause=pause;
panel.setPaused(pause);
}
public void togglePause() {
setPause(!pause);
}
public void up(boolean b) {
if ( mode ) {
if ( b ) {
this.down=false;
}
else {
return;
}
}
this.up=b;
updateDy();
}
public void left(boolean b) {
if ( mode ) {
if ( b ) {
this.right=false;
}
else {
return;
}
}
this.left=b;
updateDx();
}
public void right(boolean b) {
if ( mode ) {
if ( b ) {
this.left=false;
}
else {
return;
}
}
this.right=b;
updateDx();
}
public void down(boolean b) {
if ( mode ) {
if ( b ) {
this.up=false;
}
else {
return;
}
}
this.down=b;
updateDy();
}
private void updateDy() {
if ( this.down && this.up ) {
dy=0;
}
else if ( this.down ) {
dy=1;
}
else if ( this.up ) {
dy=-1;
}
else {
dy=0;
}
}
private void updateDx() {
if ( this.right && this.left ) {
dx=0;
}
else if ( this.right ) {
dx=1;
}
else if ( this.left ) {
dx=-1;
}
else {
dx=0;
}
}
public void toggleMode() {
mode=!mode;
panel.setMode(mode);
if ( !mode ) {
up=down=right=left=false;
updateDx();
updateDy();
}
}
public boolean isPause() {
return pause;
}
}
GameLoop gameLoop = new GameLoop();
Timer timer = new Timer(33, gameLoop);
KeyEventDispatcher keyEventDispatcher = new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
boolean dispatch=true;
switch(e.getID()) {
case KeyEvent.KEY_RELEASED:
switch(e.getKeyCode()) {
case KeyEvent.VK_UP:
gameLoop.up(false);
dispatch=false;
break;
case KeyEvent.VK_DOWN:
gameLoop.down(false);
dispatch=false;
break;
case KeyEvent.VK_RIGHT:
gameLoop.right(false);
dispatch=false;
break;
case KeyEvent.VK_LEFT:
gameLoop.left(false);
dispatch=false;
break;
}
break;
case KeyEvent.KEY_PRESSED:
switch(e.getKeyCode()) {
case KeyEvent.VK_UP:
gameLoop.up(true);
dispatch=false;
break;
case KeyEvent.VK_DOWN:
gameLoop.down(true);
dispatch=false;
break;
case KeyEvent.VK_RIGHT:
gameLoop.right(true);
dispatch=false;
break;
case KeyEvent.VK_LEFT:
gameLoop.left(true);
dispatch=false;
break;
}
break;
}
return !dispatch;
}
};
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
boolean pauserelease=true;
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
boolean dispatch=true;
switch(e.getID()) {
case KeyEvent.KEY_RELEASED:
switch(e.getKeyCode()) {
case KeyEvent.VK_SPACE:
pauserelease=true;
dispatch=false;
break;
}
break;
case KeyEvent.KEY_PRESSED:
switch(e.getKeyCode()) {
case KeyEvent.VK_SPACE:
if ( pauserelease ) {
gameLoop.togglePause();
if ( gameLoop.isPause() ) {
timer.stop();
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
}
else {
timer.start();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyEventDispatcher);
}
pauserelease=false;
}
dispatch=false;
break;
case KeyEvent.VK_ESCAPE:
gameLoop.toggleMode();
dispatch=false;
break;
}
break;
}
return !dispatch;
}
});
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyEventDispatcher);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
WindowAdapter windowAdapter = new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
timer.start();
}
@Override
public void windowDeactivated(WindowEvent e) {
gameLoop.setPause(true);
timer.stop();
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
}
@Override
public void windowClosed(WindowEvent e) {
timer.stop();
}
@Override
public void windowIconified(WindowEvent e) {
gameLoop.setPause(true);
timer.stop();
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
}
};
frame.addWindowListener(windowAdapter);
frame.addWindowStateListener(windowAdapter);
frame.setVisible(true);
}
} |
Partager