Correction TP-Bulles

1/1
100%

Correction TP-Bulles

import javax.swing.\*;

import javax.swing.event.\*;

import java.awt.\*;

import java.awt.event.\*;

class Bulles extends JFrame implements ActionListener {

JButton bd, ban, bar;

JPanel pc;

MonThread th;

Bulles (){

JPanel p1 = new JPanel ();

bd = new JButton ("Dessiner");

ban = new JButton ("Animer");

bar = new JButton ("Arrêter");

p1.add(bd); p1.add(ban); p1.add(bar);

add (p1, "South");

pc = new JPanel ();

pc.setSize(500, 500);

Publicité

pc.setBackground(Color.black);

this.getContentPane().add (pc, "Center");

this.setSize(520, 600);

setVisible (true);

this.setDefaultCloseOperation (this.EXIT\_ON\_CLOSE);

bd.addActionListener(this);

ban.addActionListener(this);

bar.addActionListener(this);

}

public void actionPerformed (ActionEvent e){

JButton bsr = (JButton) e.getSource();

// Dessiner

if (bsr == bd){

Graphics g = pc.getGraphics();

// Couleur aléatoire

int r = (int)(Math.random()\*255);

int v = (int)(Math.random()\*255);

int b = (int)(Math.random()\*255);

Publicité

g.setColor(new Color (r, v, b));

g.drawOval(240, 240, 20, 20);

}

else if (bsr == ban){ // Animer

th = new MonThread(pc);

th.start();

}

else if (bsr == bar){

if (th !=null) th.~~stop~~();

}

}

}

class MonThread extends Thread {

JPanel p ;

MonThread (JPanel p){

this.p=p;

}

public void run (){

Publicité

int xx = 240; // Position début

int yy = 240;

int x, y;

Graphics g = p.getGraphics();

while (true){

// Effacer

g.setColor(Color.BLACK);

g.drawOval(xx, yy, 20, 20);

// Dessiner avec une couleur aléatoire et une position aléatoire

int r = (int)(Math.random()\*255);

int v = (int)(Math.random()\*255);

int b = (int)(Math.random()\*255);

g.setColor(new Color (r, v, b));

x = (int) (Math.random()\*490);

y = (int) (Math.random()\*490);

g.drawOval(x, y, 20, 20);

xx = x; //Nouvelle position prend ancienne position

yy = y;

Publicité

try {

sleep (500);

}

catch (InterruptedException e){

}

}

}

}

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

new Competition ();

}

}

Correction TP-Bulles

Programming, Java, Animation · lab

Voir tous les documents en programmation

Correction TP-Bulles

import javax.swing.\*;

import javax.swing.event.\*;

import java.awt.\*;

import java.awt.event.\*;

class Bulles extends JFrame implements ActionListener {

JButton bd, ban, bar;

JPanel pc;

MonThread th;

Bulles (){

JPanel p1 = new JPanel ();

bd = new JButton ("Dessiner");

ban = new JButton ("Animer");

bar = new JButton ("Arrêter");

p1.add(bd); p1.add(ban); p1.add(bar);

add (p1, "South");

pc = new JPanel ();

pc.setSize(500, 500);

Publicité

pc.setBackground(Color.black);

this.getContentPane().add (pc, "Center");

this.setSize(520, 600);

setVisible (true);

this.setDefaultCloseOperation (this.EXIT\_ON\_CLOSE);

bd.addActionListener(this);

ban.addActionListener(this);

bar.addActionListener(this);

}

public void actionPerformed (ActionEvent e){

JButton bsr = (JButton) e.getSource();

// Dessiner

if (bsr == bd){

Graphics g = pc.getGraphics();

// Couleur aléatoire

int r = (int)(Math.random()\*255);

int v = (int)(Math.random()\*255);

int b = (int)(Math.random()\*255);

Publicité

g.setColor(new Color (r, v, b));

g.drawOval(240, 240, 20, 20);

}

else if (bsr == ban){ // Animer

th = new MonThread(pc);

th.start();

}

else if (bsr == bar){

if (th !=null) th.~~stop~~();

}

}

}

class MonThread extends Thread {

JPanel p ;

MonThread (JPanel p){

this.p=p;

}

public void run (){

Publicité

int xx = 240; // Position début

int yy = 240;

int x, y;

Graphics g = p.getGraphics();

while (true){

// Effacer

g.setColor(Color.BLACK);

g.drawOval(xx, yy, 20, 20);

// Dessiner avec une couleur aléatoire et une position aléatoire

int r = (int)(Math.random()\*255);

int v = (int)(Math.random()\*255);

int b = (int)(Math.random()\*255);

g.setColor(new Color (r, v, b));

x = (int) (Math.random()\*490);

y = (int) (Math.random()\*490);

g.drawOval(x, y, 20, 20);

xx = x; //Nouvelle position prend ancienne position

yy = y;

Publicité

try {

sleep (500);

}

catch (InterruptedException e){

}

}

}

}

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

new Competition ();

}

}