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
| import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.KeyboardFocusManager;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class DemoDeplacement extends JPanel {
public final static int DEPLACEMENT = 4;
public final static int SIZE = 8;
private static final boolean OPPOSITE_NOT_ALLOWED = true;
public static void main(String[] args) {
SwingUtilities.invokeLater( ()->demo( new DemoDeplacement(OPPOSITE_NOT_ALLOWED) ) );
//illegal start of expression, 'void' not allowed
}
private static void demo( final DemoDeplacement demoDeplacement ) {
final JFrame frame = new JFrame( "Démo" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 300, 300 );
frame.setLocationRelativeTo( null );
frame.getContentPane().add( demoDeplacement );
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(
(e) -> { return demoDeplacement.getKeyStateHandler().dispatch(e); } );
//illegal start of expression x 3, cannot find symbol -> e, illegal start of type
//cannot return a value from method whose result type is void, cannot find symbol -> e
demoDeplacement.addComponentListener( new ComponentAdapter() {
@Override
public void componentResized( ComponentEvent e ) {
demoDeplacement.replacer();
}
});
frame.addWindowListener(new WindowAdapter() {
private Timer timer;
@Override
public void windowOpened(WindowEvent e) {
timer = new Timer();
//constructor timer cannot be applied to given type, required: int,ActionListener
//found : no arguments
timer.schedule(new TimerTask() {
//cannot find symbol -> method schedule (<anonymous TimerTask,int,int)
@Override
public void run() {
demoDeplacement.deplacer();
}
}, 0, 40);
}
@Override
public void windowClosed(WindowEvent e) {
timer.cancel();
//cannot find symbol -> method cancel()
}
});
frame.setVisible(true);
}
private int x=0;
private int y=0;
private final KeyStateHandler handler;
public DemoDeplacement(boolean oppositeNotAllowed) {
setBackground( Color.GREEN.darker() );
handler = new KeyStateHandler(oppositeNotAllowed);
}
public KeyStateHandler getKeyStateHandler() {
//Exporting non-public type through public API (souligné en jaune)
return handler;
}
@Override
public void paint( final Graphics g ) {
super.paint( g );
final Graphics2D graphics = (Graphics2D)g.create();
try {
graphics.setColor( Color.ORANGE );
graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
graphics.fillOval( x, y, SIZE, SIZE );
}
finally {
graphics.dispose();
}
}
public void deplacer() {
SwingUtilities.invokeLater( ()-> deplacer( handler.getDeplacementX(), handler.getDeplacementY() ) );
//illegal start of expression x2 ,'void' type not allowed here
}
public void deplacer( final int dx, final int dy ) {
final int oldx = x;
x = deplace( x, dx, getWidth() - SIZE );
final int oldy = y;
y = deplace( y, dy, getHeight() - SIZE );
if ( oldx != x || oldy != y ) {
repaint();
}
}
private int deplace( int c, final int dc, final int maxc ) {
if ( dc < 0 && c > 0 ) {
c += dc;
if ( c < 0 ) {
c = 0;
}
}
else if ( dc>0 && c<maxc ) {
c += dc;
if ( c > maxc ) {
c = maxc;
}
}
return c;
}
public void replacer() {
final int oldx = x;
x = replace( x, getWidth() - SIZE );
final int oldy = y;
y = replace( y, getHeight() - SIZE );
if ( oldx != x || oldy != y ) {
repaint();
}
}
private int replace( int c, final int maxc ) {
if ( c < 0 ) {
c = 0;
}
else if ( c > maxc ) {
c = maxc;
}
return c;
}
private static class KeyStateHandler {
public enum Direction {
DOWN,
UP,
LEFT,
RIGHT,
NONE;
}
private final Direction[] keyX;
private final Direction[] keyY;
private final boolean oppositeNotAllowed;
public KeyStateHandler(boolean oppositeNotAllowed) {
this.oppositeNotAllowed=oppositeNotAllowed;
keyX = new Direction[2];
Arrays.fill(keyX, Direction.NONE);
keyY = new Direction[2];
Arrays.fill(keyY, Direction.NONE);
}
public boolean dispatch(KeyEvent e) {
boolean consumed;
switch (e.getID()) {
case KeyEvent.KEY_PRESSED:
consumed = pressed(e.getKeyCode());
break;
case KeyEvent.KEY_RELEASED:
consumed = released(e.getKeyCode());
break;
default:
consumed = false;
break;
}
return consumed;
}
private boolean pressed(int keyCode) {
Direction[] dirStack;
Direction dir;
switch (keyCode) {
case KeyEvent.VK_DOWN:
dir = Direction.DOWN;
dirStack = keyY;
break;
case KeyEvent.VK_UP:
dir = Direction.UP;
dirStack = keyY;
break;
case KeyEvent.VK_LEFT:
dir = Direction.LEFT;
dirStack = keyX;
break;
case KeyEvent.VK_RIGHT:
dir = Direction.RIGHT;
dirStack = keyX;
break;
default:
dir = null;
dirStack = null;
break;
}
if ( dirStack!=null ) {
if ( dirStack[0]!=dir ) {
if ( dirStack[0]==Direction.NONE ) {
dirStack[0]=dir;
}
else {
dirStack[1]=dir;
}
}
return true;
}
return false;
}
private boolean released(int keyCode) {
Direction[] dirStack;
Direction dir;
switch (keyCode) {
case KeyEvent.VK_DOWN:
dir = Direction.DOWN;
dirStack = keyY;
break;
case KeyEvent.VK_UP:
dir = Direction.UP;
dirStack = keyY;
break;
case KeyEvent.VK_LEFT:
dir = Direction.LEFT;
dirStack = keyX;
break;
case KeyEvent.VK_RIGHT:
dir = Direction.RIGHT;
dirStack = keyX;
break;
default:
dir = null;
dirStack = null;
break;
}
if ( dirStack!=null ) {
if ( dirStack[0]==dir ) {
dirStack[0]=dirStack[1];
dirStack[1]=Direction.NONE;
}
else if ( dirStack[1]==dir ) {
dirStack[1]=Direction.NONE;
}
return true;
}
return false;
}
public int getDeplacementX() {
int dx;
if ( oppositeNotAllowed && keyX[1]!=Direction.NONE ) {
dx=0;
}
else {
switch( keyX[0] ) {
case LEFT:
dx=-DEPLACEMENT;
break;
case RIGHT:
dx=DEPLACEMENT;
break;
default:
dx=0;
break;
}
}
return dx;
}
public int getDeplacementY() {
int dy;
if ( oppositeNotAllowed && keyY[1]!=Direction.NONE ) {
dy=0;
}
else {
switch( keyY[0] ) {
case UP:
dy=-DEPLACEMENT;
break;
case DOWN:
dy=DEPLACEMENT;
break;
default:
dy=0;
break;
}
}
return dy;
}
}
} |