[Swing] Dessin progressif d'un cercle : améliorations de mon code
Bonjour, j'ai écrit une classe JFrame permettant, lorsque l'on appuie sur un bouton, d'animer le dessin d'un cercle plein rose sur fond jaune. Toutefois, j'ai besoin de votre aide sur quelques points.
Code:
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 org.isgreat.loloof64.j2se.oval_progressive;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class OvalProgressive extends JFrame {
public static void main(String[] args) {
new OvalProgressive().setVisible(true);
}
public OvalProgressive() {
super("Oval Progressive application");
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(CENTER_RELATIVE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel southPanel = new JPanel();
southPanel.add(startButton);
add(southPanel, BorderLayout.SOUTH);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
new TimeSimulator().start();
}
});
computeOvalOffsets();
}
@Override
public void paint(Graphics graphics) {
super.paint(graphics);
graphics.setColor(BACKGROUND);
graphics.fillRect(0, 0, WIDTH, HEIGHT);
graphics.setColor(OVAL_COLOR);
graphics.fillArc(ovalOffsetX, ovalOffsetY, OVAL_DIAMETER, OVAL_DIAMETER,
START_ANGLE, (int) (-timePart*360) );
}
private void computeOvalOffsets(){
ovalOffsetX = ( getWidth() - OVAL_DIAMETER ) / 2;
ovalOffsetY = ( getHeight() - OVAL_DIAMETER ) / 2;
}
private void buttonStateBecomeStart() {
startButton.setText("Start");
startButton.setEnabled(true);
}
private void buttonStateBecomeWaiting() {
startButton.setText("Please Wait...");
startButton.setEnabled(false);
}
private class TimeSimulator extends Thread {
@Override
public void run() {
buttonStateBecomeWaiting();
while(timePart < 1.0f){
try {
Thread.sleep(DELAY_MILLIS);
} catch (InterruptedException e) {
e.printStackTrace();
}
timePart += TIME_ATOMIC_INCREMENT;
repaint();
}
buttonStateBecomeStart();
}
}
private static final int WIDTH = 200;
private static final int HEIGHT = 230;
private static final int OVAL_DIAMETER = 130;
private static final int START_ANGLE = 0;
private static int ovalOffsetX;
private static int ovalOffsetY;
private static float timePart = 0;
private static final Component CENTER_RELATIVE = null;
private static final Color BACKGROUND = Color.YELLOW;
private static final Color OVAL_COLOR = Color.PINK;
private JButton startButton = new JButton("Start");
private static final long DELAY_MILLIS = 100;
private static final float TIME_ATOMIC_INCREMENT = 0.025f;
private static final long serialVersionUID = 1L;
} |
Je voudrais hériter de vos conseils sur les points suivants :
1) Le bouton disparait pendant l'animation du cercle
2) Le flickering omniprésent
3) Le bouton se situe dans un rectangle blanc à la fin de l'animation
4) Pas moyen d'effacer le cercle affiché, à la fin de l'animation. La seule solution à laquelle j'ai pensée était buggué. Elle consistait à ajouter la section suivante à la fin de la méthode run() de la classe privée TimeSimulator, juste avant l'appel buttonStateBecomeStart(); :
Code:
1 2 3 4
|
Graphics graphics = getGraphics();
graphics.fillRect(0, 0, WIDTH, HEIGHT);
OvalProgressive.super.paint(graphics); |
J'ai du mal à trouver les bonnes méthodes
Bonjour et merci beaucoup de m'avoir répondu :)
Etant donné que je faisais dans le dessin personnalisé d'un composant, je me trompe toujours dans le choix des méthodes où je définis mes codes.
J'ai en effet hésité entre paint(Graphics) et paintComponent(Graphics) : mais je je suis pas encore sûr que paintComponent(Graphics) changera grand chose.
Je pense que j'aurais carrément dû utiliser un JPanel et rédéfinir la méthode paint(Graphics) (ce m'est venu à l'esprit au moment d'écrire ce message) : je pense qu'à force de programmer pour Android, je perds me repères en J2SE.
En tous cas merci beaucoup :)