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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class Frame extends JFrame {
static Timer timer;
ImageIcon ii = new ImageIcon(this.getClass().getResource("Essaye-lapin2.gif"));
JLabel imageLabel = new JLabel(ii);
protected int x=10;
protected int y=10;
private static final long serialVersionUID = 4983727800375272097L;
public static JLabel label;
public Frame() {
ActionListener deplacement = new ActionListener() {
public void actionPerformed(ActionEvent e) {
imageLabel.setBounds(getX(),getY(),ii.getIconWidth(),ii.getIconHeight());
repaint();//peut être inutile??
x=x+100;
y=y+100;
}
};
timer = new Timer(1000,deplacement);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1367, 703);
setLocationRelativeTo(null);
BufferedImage img = null;
try {
img = ImageIO.read(new File("Background0.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
setContentPane(new MyCanvas(img));
}
public static void main(String[] args) {
new Frame().setVisible(true);
}
private static class MyCanvas extends JComponent implements MouseListener {
//ici on défini toutes les zones clicables de l'écran 1
private static final long serialVersionUID = 1L;
// position de la première zone de limage que l'on veut (ici elle se trouve au pixel x=0, y=0 et s'étend sur width=10 et height=10)
private static final Rectangle ZONE_IMAGE_1 = new Rectangle(21, 10, 281, 50);
// position de la première zone de limage que l'on veut (ici elle se trouve au pixel x=100, y=100 et s'étend sur width=30 et height=20)
private static final Rectangle ZONE_IMAGE_2 = new Rectangle(320, 10, 265, 50);
private static final Rectangle ZONE_IMAGE_3 = new Rectangle(602, 10, 290, 50);
// position de la première zone de limage que l'on veut (ici elle se trouve au pixel x=100, y=100 et s'étend sur width=30 et height=20)
private static final Rectangle ZONE_IMAGE_4 = new Rectangle(135, 200, 170, 140);
private static final Rectangle ZONE_IMAGE_5 = new Rectangle(135, 352, 170, 140);
private static final Rectangle ZONE_IMAGE_6 = new Rectangle(135, 506, 170, 140);
private static final Rectangle ZONE_IMAGE_10 = new Rectangle(368, 200, 61, 45);
private static final Rectangle ZONE_IMAGE_11 = new Rectangle(368, 247, 61, 45);
private static final Rectangle ZONE_IMAGE_12 = new Rectangle(368, 294, 61, 44);
private static final Rectangle ZONE_IMAGE_13 = new Rectangle(368, 340, 60, 44);
private static final Rectangle ZONE_IMAGE_14 = new Rectangle(368, 386, 60, 44);
private static final Rectangle ZONE_IMAGE_15 = new Rectangle(368, 432, 60, 44);
private static final Rectangle ZONE_IMAGE_16 = new Rectangle(368, 478, 60, 46);
private static final Rectangle ZONE_IMAGE_17 = new Rectangle(368, 526, 60, 45);
private static final Rectangle ZONE_IMAGE_18 = new Rectangle(368, 573, 60, 54);
private BufferedImage buff= null;
JLabel imageLabel;
JLabel imageLabel1;
public MyCanvas(BufferedImage img) {
setLayout(null);
this.addMouseListener(this);
this.buff = img;
imageLabel = new JLabel();
ImageIcon ii = new ImageIcon(this.getClass().getResource("Essaye-lapin2.gif"));
imageLabel.setIcon(ii);
imageLabel.setBounds(10,10,ii.getIconWidth(),ii.getIconHeight());
imageLabel.setVisible(false);
add(imageLabel);
imageLabel1 = new JLabel();
ImageIcon ii1 = new ImageIcon(this.getClass().getResource("Essaye-lapin3.gif"));
imageLabel1.setIcon(ii1);
imageLabel1.setBounds(10,10,ii1.getIconWidth(),ii1.getIconHeight());
imageLabel1.setVisible(false);
add(imageLabel1);
//imageLabel = new JLabel(new ImageIcon(this.getClass().getResource("gifAnime.gif")));
}
public void paintComponent(Graphics g) {//zone images
// dessine l'image
g.drawImage(buff, 0, 0, buff.getWidth(), buff.getHeight(), this);
// juste histoire de voir ou se trouvent les zones clicables
g.setColor(Color.GREEN);
g.drawRect(ZONE_IMAGE_1.x, ZONE_IMAGE_1.y, ZONE_IMAGE_1.width, ZONE_IMAGE_1.height);
g.drawRect(ZONE_IMAGE_2.x, ZONE_IMAGE_2.y, ZONE_IMAGE_2.width, ZONE_IMAGE_2.height);
g.drawRect(ZONE_IMAGE_3.x, ZONE_IMAGE_3.y, ZONE_IMAGE_3.width, ZONE_IMAGE_3.height);
g.drawRect(ZONE_IMAGE_4.x, ZONE_IMAGE_4.y, ZONE_IMAGE_4.width, ZONE_IMAGE_4.height);
g.drawRect(ZONE_IMAGE_5.x, ZONE_IMAGE_5.y, ZONE_IMAGE_5.width, ZONE_IMAGE_5.height);
g.drawRect(ZONE_IMAGE_6.x, ZONE_IMAGE_6.y, ZONE_IMAGE_6.width, ZONE_IMAGE_6.height);
g.drawRect(ZONE_IMAGE_10.x, ZONE_IMAGE_10.y, ZONE_IMAGE_10.width, ZONE_IMAGE_10.height);
g.drawRect(ZONE_IMAGE_11.x, ZONE_IMAGE_11.y, ZONE_IMAGE_11.width, ZONE_IMAGE_11.height);
g.drawRect(ZONE_IMAGE_12.x, ZONE_IMAGE_12.y, ZONE_IMAGE_12.width, ZONE_IMAGE_12.height);
g.drawRect(ZONE_IMAGE_13.x, ZONE_IMAGE_13.y, ZONE_IMAGE_13.width, ZONE_IMAGE_13.height);
g.drawRect(ZONE_IMAGE_14.x, ZONE_IMAGE_14.y, ZONE_IMAGE_14.width, ZONE_IMAGE_14.height);
g.drawRect(ZONE_IMAGE_15.x, ZONE_IMAGE_15.y, ZONE_IMAGE_15.width, ZONE_IMAGE_15.height);
g.drawRect(ZONE_IMAGE_16.x, ZONE_IMAGE_16.y, ZONE_IMAGE_16.width, ZONE_IMAGE_16.height);
g.drawRect(ZONE_IMAGE_17.x, ZONE_IMAGE_17.y, ZONE_IMAGE_17.width, ZONE_IMAGE_17.height);
g.drawRect(ZONE_IMAGE_18.x, ZONE_IMAGE_18.y, ZONE_IMAGE_18.width, ZONE_IMAGE_18.height);
}
private Boolean testLocation(Point mouse, Rectangle area, String text) {//ici on a juste le test pour savoir si les zones de clics répondes
// test si la souris est dans les data de l'image
if(area.contains(mouse)) {
System.out.println(text + " - image");
return true;
}
else {
System.out.println(text + " - !image");
return false;
}
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e)
{ //récupération de la position de la souris
Point p = e.getPoint();
if (testLocation(p, ZONE_IMAGE_1, "mouseClicked - data 1"))
{
new Frame2().setVisible(true);// j'appelle ma nouvelle fenêtre
}
testLocation(p, ZONE_IMAGE_2, "mouseClicked - data 2");
if (testLocation( p, ZONE_IMAGE_2, "mouseClicked - data 2")){
new Frame3().setVisible(true);// j'appelle ma nouvelle fenêtre
}
testLocation(p, ZONE_IMAGE_3, "mouseClicked - data 3");
if(testLocation(p, ZONE_IMAGE_3, "mouseClicked - data 3")){
new Frame4().setVisible(true);// j'appelle ma nouvelle fenêtre
}
testLocation(p, ZONE_IMAGE_4, "mouseClicked - data 4");//clapier1
testLocation(p, ZONE_IMAGE_5, "mouseClicked - data 5");//clapier2
testLocation(p, ZONE_IMAGE_6, "mouseClicked - data 6");//clapier3
testLocation(p, ZONE_IMAGE_10, "mouseClicked - data 10");//case 1
if (testLocation( p, ZONE_IMAGE_10, "mouseClicked - data 10")){
{
if (testLocation( p, ZONE_IMAGE_10, "mouseClicked - data 10")){
int x =10;
int y =10;
imageLabel.setVisible(true);
timer.start();
}
if (testLocation(p, ZONE_IMAGE_11, "mouseClicked - data 11")) {
}
testLocation(p, ZONE_IMAGE_12, "mouseClicked - data 12");//case 3
testLocation(p, ZONE_IMAGE_13, "mouseClicked - data 13");//case 4
testLocation(p, ZONE_IMAGE_14, "mouseClicked - data 14");//case 5
testLocation(p, ZONE_IMAGE_15, "mouseClicked - data 15");//case 6
testLocation(p, ZONE_IMAGE_16, "mouseClicked - data 16");//case 7
testLocation(p, ZONE_IMAGE_17, "mouseClicked - data 17");//case 8
testLocation(p, ZONE_IMAGE_18, "mouseClicked - data 18");//case 9
}
}
}
public void mouseReleased(MouseEvent e) {}//peut servir pour plus tard
public void mouseEntered(MouseEvent e) { }//peut servir pour plus tard
public void mouseExited(MouseEvent e) { }//peut servir pour plus tard
}
} |
Partager