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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.awt.Graphics;
public class Panneau extends JPanel implements MouseMotionListener,MouseListener
{
private Image[][] tableauDeImage;
private boolean mode_dessin;
private int position;
private ArrayList<Integer> tab_position;
private ArrayList<String> alphabet;
private Point old_point;
private Graphics g;
private String res="";
public Panneau()
{
super();
setPreferredSize(new Dimension(500, 500));
tab_position = new ArrayList<Integer>();
old_point = new Point();
tableauDeImage = new Image[7][4]; //tableau de 7 colonne et 4 ligne
for(int i = 0;i<7;i++)
{
for(int j = 0;j<4;j++)
{
tableauDeImage[i][j] =(new ImageIcon(Panneau.class.getResource("images/image"+i+j+".png")).getImage());
}
}
mode_dessin=false;
this.addMouseListener(this);
this.addMouseMotionListener(this);mode_dessin=false;
}
private void chargerDico()
{
alphabet = new ArrayList<String>(26);
alphabet.add(0,"A"); //A
alphabet.add(1,"B"); //B
alphabet.add(2,"C"); //C
alphabet.add(3,"D"); //D
alphabet.add(4,"E"); //E
alphabet.add(5,"F"); //F
alphabet.add(6,"G"); //G
alphabet.add(7,"H"); //H
alphabet.add(8,"I"); //I
alphabet.add(9,"J"); //J
alphabet.add(10,"K"); //K
alphabet.add(11,"L"); //L
alphabet.add(12,"M"); //M
alphabet.add(13,"N"); //N
alphabet.add(14,"O"); //O
alphabet.add(15,"P"); //P
alphabet.add(16,"Q"); //Q
alphabet.add(17,"R"); //R
alphabet.add(18,"S"); //S
alphabet.add(19,"T"); //T
alphabet.add(20,"U"); //U
alphabet.add(21,"V"); //V
alphabet.add(22,"W"); //W
alphabet.add(23,"X"); //X
alphabet.add(24,"Y"); //Y
alphabet.add(25,"Z"); //Z
}
public void mouseMoved(MouseEvent event)
{
}
public void mouseDragged(MouseEvent event)
{
if( ! mode_dessin )
{
old_point = event.getPoint();
mode_dessin = true;
}
else
{
int position=0;
// position de la souris en x et y
int x = (int)(event.getPoint().getX() + this.getLocationOnScreen().getX());
int y = (int)(event.getPoint().getY() + this.getLocationOnScreen().getY());
position= this.getPositionAvecCoordonneesSouris(x,y);
//this.getGd().setColor( Color.RED ); ->>> pose probleme
old_point = event.getPoint();
// pose probleme this.drawLine()
//this.drawLine( (int) old_point.getX(), (int) old_point.getY(), event.getX(), event.getY() );
//System.out.println("coordonnees x:"+x+" y:"+y);
if( tab_position.size() <= 0 || tab_position.get( tab_position.size() - 1 ) != position )
{
tab_position.add( position );
}
this.repaint(); // on rafraichit le panneau
}
}
public void mouseReleased(MouseEvent event)
{
mode_dessin = false;
tab_position.size();
String tmp="";
System.out.println("Positions : "+tab_position); // Affiche le tableau de positions
for(int i=0; i < tab_position.size(); i++)
{
tmp=Integer.toString(tab_position.get(i));
res+=tmp;
}
System.out.println("resultat : "+res); // Affiche resultat
tmp=""; res=""; tab_position.clear();
}
public void mouseClicked(MouseEvent event)
{
}
public void mouseEntered(MouseEvent event)
{
}
public void mouseExited(MouseEvent event)
{
}
public void mousePressed(MouseEvent event)
{
}
public Graphics getGd()
{
return g;
}
public int getPositionAvecCoordonneesSouris(int x,int y)
{
int caseX,caseY;
int largeur =(int) ((getWidth())/7);
int hauteur =(int) ((getHeight())/4);
caseX = x *7/ largeur;
caseY = y *4/ hauteur;
if ( (caseX>=0) && (caseX<7) && (caseY>=0) && (caseY<4) )
return caseX + caseY * 7;
else return -1;
}
public void paintComponent(Graphics g)
{
dessinerFond(g);
dessinerBordure(g);
dessinerInterieurDesCases(g);
}
private void dessinerFond(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
}
private void dessinerBordure(Graphics g)
{
g.setColor(Color.BLACK);
// Les 7 barres noires verticales
for(int i=1;i<7;i++)
g.drawLine((int)((getWidth()*i)/7),0, (int)((getWidth()*i)/7),getHeight());
// Les 4 barres noires horizontales
for(int i=1;i<4;i++)
g.drawLine(0,(int)((getHeight()*i)/4), getWidth(),(int)((getHeight()*i)/4));
}
private void dessinerInterieurDesCases(Graphics g)
{
int largeur =(int) ((getWidth())/7);
int hauteur =(int) ((getHeight())/4);
for(int i = 0;i<7;i++)
{
for(int j = 0;j<4;j++)
{
Image tmp = scale(tableauDeImage[i][j],largeur-1,hauteur-1);
g.drawImage(tmp, (i *largeur)+1,(j*hauteur)+1,largeur-1, hauteur-1,this);
//On met des +1 et -1 pour ne pas débordé sur les bordures
}
}
}
//Redimensionne une image
public static Image scale(Image source, int width, int height)
{
/* On crée une nouvelle image aux bonnes dimensions. */
BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
/* On dessine sur le Graphics de l'image bufferisée. */
Graphics2D g = buf.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(source, 0, 0, width, height, null);
g.dispose();
/* On retourne l'image bufferisée, qui est une image. */
return buf;
}
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setContentPane(new Panneau());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
} |
Partager