public class Cercle extends JPanel { public Cercle(){ super(); } @Override public void paintComponent(Graphics g){ g.setColor(Color.RED); g.fillOval(0, 0, getWidth(), getHeight()); } }
JPanel panel = new JPanel(); panel.setLayout(null); Cercle c = new Cercle(); c.setBounds(x, y, width, height); panel.add(c);
public class AnimateurCercle extends Thread { /** Le cercle à déplacer */ private Cercle cercle; public AnimateurCercle(Cercle cercle){ this.cercle=cercle; } /** * Cette méthode définit le traitement à effectuer * Ici, tu définis donc le déplacement du cercle passé en paramètre */ @Override public void run() { while(true){ //En gros, c'est ce que fait ta méthode go() } } }
JPanel panel = new JPanel(); panel.setLayout(null); Cercle c1 = new Cercle(); AnimateurCercle a1 = new AnimateurCercle(c1); c1.setBounds(0, 0, 60, 60); Cercle c2 = new Cercle(); AnimateurCercle a2 = new AnimateurCercle(c2); c2.setBounds(0, 0, 60, 60); panel.add(c1); panel.add(c2); a1.start(); a2.start();
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionpublic class Elfe extends Individus { private Utilitaire u = new Utilitaire(); public Elfe(int x, int y, String nom) { super(); this.posX = x; this.posY = y; this.setType("ELFE"); this.vol = u.randomChiffre(0, 100); this.setPatience(u.randomChiffre(0, 100)); this.setAgressivite(u.randomChiffre(0, 100)); this.setNegociation(u.randomChiffre(0, 100)); this.setCapacite(u.randomChiffre(0, 100)); this.couleur = Color.WHITE; this.setName(nom); } @Override public void paintComponent(Graphics g) { // g.setColor(Color.YELLOW); // g.fillRect(10, 10, 700, 700); g.setColor(couleur); g.fillOval(0, 0, getWidth(), getHeight()); } }
public class ThreadAnimation extends Thread { private Individus i; public ThreadAnimation(Individus i) { this.i = i; } @Override public void run() { int x = i.getPosX(); int y = i.getPosY(); while(true) { System.out.println("\n"+x); if(x < 1)backX = false; // j'ai préféré mettre les coordonnées de la fenêtre en dur if(x > 700)backX = true; if(y < 1)backY = false; if(y > 650)backY = true; if(!backX)i.setPosX(++x); else i.setPosX(--x); if(!backY) i.setPosY(++y); else i.setPosY(--y); // le repaint servirait à redessinner un rectangle sur la //surface pour afficher l'objet à sa nouvelle position. Mais ça ne marche pas i.repaint(); try { Thread.sleep(3); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public void create() { monde.setSize(700, 650); monde.setPreferredSize(new Dimension(700, 650)); monde.setBackground(Color.GRAY); monde.setLocationRelativeTo(null); monde.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // initialisation du panneau affichant les stats fenetre2(); for(int j=0; j<100; j++) { //génération aléatoire des coordonnées de départ du cercle int x = u.randomChiffre(0, 670); int y = u.randomChiffre(0, 600); Elfe e = new Elfe(x, y, "ELFE"+j); elfes.add(e); ThreadAnimation th = new ThreadAnimation(e); e.setBounds(x, y, 10, 10); contenu.setLayout(null); contenu.setBackground(Color.CYAN); contenu.add(e); th.start(); } monde.add(contenu, BorderLayout.CENTER); monde.pack(); monde.setVisible(true); } // Fin create()
System.out.println("\n"+x);
//suppression de l'objet avant changement de coordonnées contenu.remove(i); //avec les nouvelles coordonnées contenu.add(i);