Lorsque j'essaie d'accéder à la méthode repaint() pour la méthode statique principale du formulaire JPanel, j'obtiens une erreur : "Cannot make a static reference to the non-static method repaint() from the type Component"
. Lorsque j'ai essayé de changer la méthode principale en "public void main"
ou supprimer les statiques - il ne peut pas compiler. Quelle est la bonne approche pour résoudre ce problème ?
public class mull extends JPanel { ...
....
public static void main(String[] args) {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
f.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
&& (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
ringPoints += 100;
allowedToDrawRing = false;
}
if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
&& (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
squarePoints += 100;
allowedToDrawSquare = false;
}
if(!allowedToDrawSquare && !allowedToDrawRing){
// stop timer
// display points
ringTimer.stop();
squareTimer.stop();
showScores = true;
repaint(); // Cannot make a static reference to the non-static method repaint() from the type Component
}
}
});
f.add(p);
f.add(new mull());
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
J'ai remplacé tout mon code de la méthode main et il y a un détail que je ne vois pas comment traiter. Dans ma méthode main, j'ai créé JFrame et je lui ai ajouté des temps (Swing timers). Lorsque j'ai placé tout le code dans le constructeur, cela ne fonctionne pas et cela n'a pas de sens d'appeler le constructeur lui-même à l'intérieur (afin d'ajouter des temps à JFrame). Comment résoudre ce problème ? Constructeur :
public Mull() {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
f.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
&& (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
ringPoints += 100;
allowedToDrawRing = false;
}
if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
&& (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
squarePoints += 100;
allowedToDrawSquare = false;
}
if(!allowedToDrawSquare && !allowedToDrawRing){
// stop timer
// display points
ringTimer.stop();
squareTimer.stop();
showScores = true;
repaint();
}
}
});
f.add(p);
f.add(new Mull());
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setVisible(true);
ActionListener actionListenerRing = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(ringCounter < 3){
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
while( !(
(xSquare + squareSize) < (xRing)
|| (xSquare) > (xRing + ringSize )
|| (ySquare + squareSize) < (yRing)
|| (ySquare) > (yRing + ringSize)
)
){
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
}
setParamsRing(xRing, yRing, ringSize);
ringCounter++;
repaint();
}
}
};
ActionListener actionListenerSquare = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(squareCounter < 3){
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
while( !(
(xSquare + squareSize) < (xRing)
|| (xSquare) > (xRing + ringSize )
|| (ySquare + squareSize) < (yRing)
|| (ySquare) > (yRing + ringSize)
)
){
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
}
setParamsSquare(xSquare, ySquare, squareSize);
squareCounter++;
repaint();
}
}
};
ringTimer = new Timer(700, actionListenerRing);
ringTimer.setInitialDelay(1000);
ringTimer.start();
squareTimer = new Timer(500, actionListenerSquare);
squareTimer.setInitialDelay(1000);
squareTimer.start();
}
Probablement, je devrais déplacer les méthodes de Timers dans une autre classe et ensuite appeler quelque chose comme f.add(new setTimers());
....