Svp j'aimerai savoir comment raffrechir ma class

  • Auteur de la discussion mitsumo10
  • Date de début

mitsumo10

Nouveau membre
Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class Fenetre extends JFrame  implements ActionListener{
	
private Panneau pan = new Panneau();
private JButton haut = new JButton("haut");
private JButton bas = new JButton("bas");
private JButton droite = new JButton("droite");
private JButton gauche = new JButton("gauche");
private JPanel container = new JPanel();
private JLabel label = new JLabel("GO  ! ! ! ");

private int x = 350;
private int y = 350;


public Fenetre(){
	this.setTitle("People's war version 1.0.0");
	this.setSize(800, 800);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setLocationRelativeTo(null);
	this.setResizable(false);	
	
	this.setContentPane(new Panneau());
	this.setVisible(true);



	container.setBackground(Color.white);
	container.setLayout(new BorderLayout());
	container.add(pan, BorderLayout.CENTER);

	haut.addActionListener(this);
	bas.addActionListener(this);
	droite.addActionListener(this);
	gauche.addActionListener(this);
	JPanel south = new JPanel();
	south.add(gauche);
	south.add(haut);
	south.add(bas);
	south.add(droite);
	container.add(south, BorderLayout.SOUTH);

	Font police = new Font("Tahoma", Font.BOLD, 16 );

	label.setFont(police);

	label.setForeground(Color.blue);

	label.setHorizontalAlignment(JLabel.CENTER);
	container.add(label, BorderLayout.NORTH);
	this.setContentPane(container);
	this.setVisible(true);
	
	
		}
	

	public void actionPerformed(ActionEvent arg0) {
			if(x>700)
			x=x-800;
			
			if(x<0)
			x=x+800;
			
			if(y>700)
			y=y-800;
			
			if(y<0)	
			y=y+800;
		
		if(arg0.getSource() == haut)
			y=y-50;
			label.setText("x=" + this.x + " y=" + this.y);
			
		
		if(arg0.getSource() == bas)
			y=y+50;
			label.setText("x=" + this.x + " y=" + this.y);
			
		
		if(arg0.getSource() == droite)
			x=x+50;
			label.setText("x=" + this.x + " y=" + this.y);
			
			
		if(arg0.getSource() == gauche)
			x=x-50;
			label.setText("x=" + this.x + " y=" + this.y);
			
			
					
	}	
	
public class Panneau extends JPanel {
		
		
	
		public void paintComponent(Graphics g){
			
				
			
				
				
				try {
					Image img = ImageIO.read(new File("map(0,0).jpg"));
					g.drawImage(img, 0, 0, this);

				} catch (IOException e) {
					e.printStackTrace();
				}
			
			
			
			
			try {
				Image img = ImageIO.read(new File("Sacha.jpg"));
				g.drawImage(img, x, y, this);

				

				
			} catch (IOException e) {
				e.printStackTrace();
			}
		
}	
		
}


}
Salut, je voudrais mettre une instruction après " if(arg0.getSource() == bas)
y=y+50;
label.setText("x=" + this.x + " y=" + this.y); "
pour pouvoir rafraichir ou réafficher mon image "Sacha.jpg"
(C'est urgent !!!)
Merci d'avence

 

OmaR

Modérateur
Salut,

Normalement il te suffit d'appeler la méthode repaint() de ton contrôle pour le repeindre.

Par contre :
Java:
if(arg0.getSource() == haut)
			y=y-50;
			label.setText("x=" + this.x + " y=" + this.y);
 
 
		if(arg0.getSource() == bas)
			y=y+50;
			label.setText("x=" + this.x + " y=" + this.y);
 
 
		if(arg0.getSource() == droite)
			x=x+50;
			label.setText("x=" + this.x + " y=" + this.y);
 
 
		if(arg0.getSource() == gauche)
			x=x-50;
			label.setText("x=" + this.x + " y=" + this.y);

Si tu veux faire un if sur plusieurs lignes, il faut mettre le code entre accolades { et }.
Là ton label.setText(...) est exécuté 4 fois
 
Vous devez vous inscrire ou vous connecter pour répondre ici.
Derniers messages publiés
Statistiques globales
Discussions
730 136
Messages
6 718 119
Membres
1 586 397
Dernier membre
Chachabidou
Partager cette page
Haut