Bonjour,
je debute avec Swing, et je suis en train de creer un jeu puissance 4.
Mon probleme est que pour actualiser l'affichage graphique je dois minimiser la fenetre puis la remaximiser. Sinon ca ne s'affiche pas.
Sur Eclipse, j'ai le probleme suivant
Merci d'avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at uk.ac.cam.nscc2.Connect4.Game.changeGrid(Game.java:71) at uk.ac.cam.nscc2.Connect4.GamePanel.actionPerformed(GamePanel.java:106) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253) at java.awt.Component.processMouseEvent(Component.java:6108) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:5873) at java.awt.Container.processEvent(Container.java:2105) at java.awt.Component.dispatchEventImpl(Component.java:4469) at java.awt.Container.dispatchEventImpl(Container.java:2163) at java.awt.Component.dispatchEvent(Component.java:4295) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055) at java.awt.Container.dispatchEventImpl(Container.java:2149) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4295) at java.awt.EventQueue.dispatchEvent(EventQueue.java:604) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
Je sais que showgame ici n'a pas été initialisé, mais je ne vois pas comment faire d'autant que dans les examples d'interface swing que j'ai vu, il ne l'etait pas non plus?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package uk.ac.cam.nscc2.Connect4; import javax.swing.JPanel; public class Game { private Integer[][] grid= { {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,1,0,0,0}, {0,0,1,2,0,0,0}, {0,1,2,2,0,0,0}, {1,2,2,2,0,0,0} } ; private GamePanel showgame; private Player player1; private Player player2; public Game() { } public static String getPlayerName() { // TODO Auto-generated method stub return null; } public void play() { showgame.repaint(); // L'ERREUR EST LA } public Integer[][] getGrid() { return grid ; } public GamePanel getGame() { return showgame; } public Player getPlayer1() { return player1; } public Player getPlayer2() { return player2; } public GamePanel getGraphics() { return showgame; } public void setGraphics(GamePanel showgame) { this.showgame = showgame; } public void changeGrid(int j) { for (int i = 0 ; (i<6) ; i++) { if (grid[i][j-1]==0) { grid[i][j-1]=1; // Changer pour autre joueur break; } } showgame.repaint(); }
et l'autre classe
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package uk.ac.cam.nscc2.Connect4; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; public class GamePanel extends JPanel implements ActionListener{ private Game game ; JButton button1; JButton button2; JButton button3 ; JButton button4; JButton button5; JButton button6; JButton button7; public GamePanel(Game game) { super(); this.game = game; //showgame = new GameGraphics(game); //game.setGraphics(showgame); FlowLayout layout = new FlowLayout(); //GridBagLayout layout = new GridBagLayout(); setLayout(layout); //GridBagConstraints c = new GridBagConstraints(); button1 = new JButton("Here1"); //c.gridx =0; //c.gridy=0; add(button1); button1.addActionListener(this); button2 = new JButton("Here2"); //c.gridx=1; //c.gridy=0; add(button2); button2.addActionListener(this); button3 = new JButton("Here3"); // c.gridx=2; add(button3); button3.addActionListener(this); button4 = new JButton("Here4"); button4.addActionListener(this); // c.gridx=3; add(button4); button5 = new JButton("Here5"); button5.addActionListener(this); // c.gridx=4; add(button5); button6 = new JButton("Here6"); // c.gridx=5; add(button6); button6.addActionListener(this); button7 = new JButton("Here7"); //c.gridx=6; button7.addActionListener(this); add(button7); //c.gridwidth = GridBagConstraints.REMAINDER ; //c.gridheight = GridBagConstraints.REMAINDER; //add(showgame,c); } protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.blue); g.fill3DRect(15, 70, 560, 480, true); //Size of board g.setColor(Color.black); //Each cell will be 80*80 for (int i= 0 ; i<6 ; i++) { for (int j = 0 ; j<7 ; j++) { if ((game.getGrid())[i][j]==1) g.setColor(Color.red); if ((game.getGrid())[i][j]==2) g.setColor(Color.YELLOW); if ((game.getGrid())[i][j]==0) g.setColor(Color.white); g.fillOval(15 + 80*j, 70 + 80*i ,80, 80) ; } } } @Override public void actionPerformed(ActionEvent arg0) { Object source = arg0.getSource(); if (source==button1) game.changeGrid(4); if (source==button2) game.changeGrid(2); if (source==button3) game.changeGrid(3); if (source== button4)game.changeGrid(4); if (source== button5) game.changeGrid(5); if (source==button6) game.changeGrid(6); if (source==button7) game.changeGrid(7); } }
Partager