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();
}
}
}
}
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