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
| public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new UserInterface());
}
}
public class UserInterface implements Runnable {
private JFrame frame;
private DrawingBoard drawingBoard;
@Override
public void run() {
// TODO Auto-generated method stub
frame = new JFrame("");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
createComponents(frame.getContentPane());
addListener();
frame.pack();
frame.setVisible(true);
}
public void createComponents(Container container) {
drawingBoard = new DrawingBoard();
container.add(drawingBoard);
}
public void addListener() {
frame.addKeyListener(new KeyboardListener(drawingBoard));
}
}
public class DrawingBoard extends JPanel {
public Dimension getPreferredSize() {
return new Dimension(250, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
public class KeyboardListener extends DrawingBoard implements KeyListener {
private DrawingBoard drawingBoard;
public KeyboardListener(DrawingBoard drawingBoard) {
this.drawingBoard = drawingBoard;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Bonjour", 50, 50);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
// Comment j'appelle la methode paintComponent ici ?
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
} |
Partager