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
|
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import javax.swing.AbstractAction;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class MainFrame extends JDialog implements MouseListener
{
private JPanel _pnlContent = null;
private DrawPanel _pnlDraw = new DrawPanel();
private int _lastKeyPressed = -1;
private boolean _maintainPressedActivated = false;
private Point _lastMouseClick = null;
private Point _lastMouseDoubleClick = null;
private HashMap _soundMap = new HashMap();
public MainFrame(int width, int height) {
this._pnlContent = (JPanel)this.getContentPane();
this._pnlContent.setLayout(new BorderLayout());
this._pnlContent.setPreferredSize(new Dimension(width, height));
this.setResizable(false);
this._pnlContent.add(this._pnlDraw, BorderLayout.CENTER);
this.addWindowListener(new WindowAdapter() {
/**
* @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
*/
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
// Il en faut une à l'initialisation pour que registerKey puisse être appelé
// n'importe quand ensuite
registerKeyPressed(KeyEvent.VK_ESCAPE);
this.pack();
this._pnlDraw.init(width, height);
this._pnlDraw.addMouseListener(this);
this._pnlDraw.addMouseMotionListener( this );
this.setLocationRelativeTo(null);
this.setVisible(true);
MousePos = new Point();
}
public void registerKeyPressed(final int key)
{
String keyName = KeyEvent.getKeyText(key);
InputMap inputMap = this.getRootPane().getInputMap(JComponent.WHEN_FOCUSED);
inputMap.put(KeyStroke.getKeyStroke(key, 0), keyName);
this.getRootPane().getActionMap().put(keyName, new AbstractAction(keyName) {
public void actionPerformed(ActionEvent e) {
if (key == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
setKeyPressed(key, false);
}
});
}
public void registerKeyPressedToReleased(final int key)
{
String keyNamePressed = KeyEvent.getKeyText(key) + " pressed";
InputMap inputMap = this.getRootPane().getInputMap(JComponent.WHEN_FOCUSED);
inputMap.put(KeyStroke.getKeyStroke(key, 0, false), keyNamePressed);
this.getRootPane().getActionMap().put(keyNamePressed, new AbstractAction(keyNamePressed) {
public void actionPerformed(ActionEvent e) {
setKeyPressed(key, true);
}
});
String keyNameReleased = KeyEvent.getKeyText(key) + " released";
inputMap = this.getRootPane().getInputMap(JComponent.WHEN_FOCUSED);
inputMap.put(KeyStroke.getKeyStroke(key, 0, true), keyNameReleased);
this.getRootPane().getActionMap().put(keyNameReleased, new AbstractAction(keyNameReleased) {
public void actionPerformed(ActionEvent e) {
setKeyPressed(-1, false);
}
});
}
synchronized private void setKeyPressed(int key, boolean maintainPressedActivated) {
this._lastKeyPressed = key;
this._maintainPressedActivated = maintainPressedActivated;
}
synchronized public int getLastKeyPressed() {
int lastKeyPressed = this._lastKeyPressed;
if (!this._maintainPressedActivated) {
this._lastKeyPressed = -1;
}
return lastKeyPressed;
}
synchronized public Point getLastMouseClick() {
Point point = this._lastMouseClick;
this._lastMouseClick = null;
return point;
}
synchronized public Point getLastMouseDoubleClick() {
Point point = this._lastMouseDoubleClick;
// Il y a certainement eu avant un click simple, s'il existe, on le supprime
if ((this._lastMouseClick != null) && (this._lastMouseClick.equals(point))) {
this._lastMouseClick = null;
}
this._lastMouseDoubleClick = null;
return point;
}
synchronized public void registerSound(String filename) {
if (!_soundMap.containsKey(filename)) {
// Vérification de la présence du fichier son
URL url = ClassLoader.getSystemResource(filename);
if (url == null) {
System.out.println("ERREUR : fichier " + filename + " introuvable !");
return;
}
File file = new File(url.getFile());
if (!file.exists()) {
System.out.println("ERREUR : fichier " + file + " introuvable !");
return;
}
// ajout dans la map des sons
_soundMap.put(filename, Applet.newAudioClip(url));
}
}
public void playSound(final String filename) {
Thread thread = new Thread(new Runnable() {
public void run() {
if (!_soundMap.containsKey(filename)) {
System.out.println("ERREUR : le fichier son n'a pas été préalablement enregistré avec la méthode registerSound");
return;
}
((AudioClip)_soundMap.get(filename)).play();
}
});
thread.start();
}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
this._lastMouseDoubleClick = e.getPoint();
} else {
this._lastMouseClick = e.getPoint();
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
MousePos.x = e.getX();
MousePos.y = e.getY();
}
public void mouseDragged(MouseEvent e) {
}
public static void main(String[] args) {
final MainFrame frame = new MainFrame(600, 600);
frame._pnlDraw.setBackground(Color.BLACK);
frame._pnlDraw.fillArc(300, 300, 50, 50, 45, 90);
frame._pnlDraw.clearRect(300, 300, 50, 50);
frame.repaint();
frame._pnlDraw.setClip(300, 300, 50, 50);
int P = 5;
for (int i = 300; i < 500; i += P) {
frame._pnlDraw.sleep(100);
frame._pnlDraw.addClip(i - P, i - P, 50, 50);
frame._pnlDraw.clearRect(i - P, i - P, 50, 50);
frame._pnlDraw.addClip(i, i, 50, 50);
frame._pnlDraw.fillArc(i, i, 50, 50, 45, 90);
frame._pnlDraw.repaint();
frame._pnlDraw.setClip(i, i, 50, 50);
}
}
} |
Partager