LaChoupinette Profil : Jeune recrue | Bonjour !
J' ai un petit probleme avec une JApplet
Code :
- import org.netbeans.lib.awtextra.*;
- import java.awt.*;
- //import java.applet.*;
- import java.awt.image.*;
- import javax.swing.*;
- import java.awt.event.*;
- import javax.swing.event.*;
- public class Carte extends JApplet /*implements Runnable*/implements MouseListener{
- private Thread tr;
- private Image base;
- private Image affiche;
- private static int h = 500;
- private static int w = 400;
- private static int longueurcarte = 1365;
- private static int largeurcarte = 1952;
- ImageIcon point ;
- private boolean modif;
- private int actuelx, actuely, actuelw, actuelh;
- private int count=0;
- private JPanel jP1,jP2,jP3;
- private JLabel jL1,jL2,jL3,jL4,jL5,jL6,jL7;
- private JButton jB1,jB2;
- private JLabel [] tab;
- private int cooX[];
- private int cooY[];
- public void init()
- {
- System.out.println("dans init()" );
- tab = new JLabel [2];
- tab[0]= new JLabel("a" );
- tab[1]= new JLabel("b" );
- base = getImage(getDocumentBase(),"Creteil.jpg" );
- jP1 = new JPanel();
- jP2 = new JPanel();
- jP3 = new JPanel();
- try{
- ImageIcon ouest = new ImageIcon("arrow.big.left.jpg" );
- ImageIcon est = new ImageIcon("arrow.big.right.jpg" );
- ImageIcon nord = new ImageIcon("arrow.big.up.jpg" );
- ImageIcon sud = new ImageIcon("arrow.big.down.jpg" );
- point = new ImageIcon("rouge.jpg" );
- jL1 = new JLabel(nord);
- jL2 = new JLabel(est);
- jL3 = new JLabel(sud);
- jL4 = new JLabel(ouest);
- jL7 = new JLabel(point);
- }
- catch(Exception e)
- {
- jL1 = new JLabel("O" );
- jL2 = new JLabel("N" );
- jL3 = new JLabel("E" );
- jL4 = new JLabel("S" );
- }
- jL1.setEnabled(true);
- jL2.setEnabled(true);
- jL3.setEnabled(true);
- jL4.setEnabled(true);
- jL5 = new JLabel("Résultat de la recherche" );
- jL6 = new JLabel("ici" );
- // jL7 = new JLabel("là" );
- jB1 = new JButton("Zoom +" );
- jB2 = new JButton("Zoom -" );
- Container c = getContentPane();
- jP1.setBackground(Color.blue);
- jP2.setBackground(Color.white);
- c.setLayout(new AbsoluteLayout());
- //jP1.setSize(500,50);
- c.add(jP1,new AbsoluteConstraints(0,0,500,50));
- c.add(jP2,new AbsoluteConstraints(0,50,500,400));
- c.add(jP3,new AbsoluteConstraints(0,450,500,100));
- jP1.add(jB1);
- jP1.add(jB2);
- jP2.setLayout(new AbsoluteLayout());
- //System.out.println("miliue de init()" );
- jP2.add(jL1,new AbsoluteConstraints(236,5,28,28));
- jP2.add(jL2,new AbsoluteConstraints(466,186,28,28));
- jP2.add(jL3,new AbsoluteConstraints(236,366,28,28));
- jP2.add(jL4,new AbsoluteConstraints(5,186,28,28));
- jP2.add(jL6,new AbsoluteConstraints(100,100,20,20));
- jP2.add(jL7,new AbsoluteConstraints(250,250,10,10));
- jP3.add(jL5);
- jP3.setBackground(Color.pink);
- zoom(1,1,largeurcarte,longueurcarte);
- /*
- affiche = base;
-
- tr = new Thread();
- tr.start();*/
- System.out.println("sort de init()" );
- jL1.addMouseListener(this);
- jL2.addMouseListener(this);
- jL3.addMouseListener(this);
- jL4.addMouseListener(this);
- jB1.addMouseListener(this);
- jB2.addMouseListener(this);
- dessinePoint();
- }
- public void mouseClicked(MouseEvent e){
- if(e.getSource() == jL1){
- deplace(1);
- }
- if(e.getSource() == jL2){
- deplace(4);
- }
- if(e.getSource() == jL3){
- deplace(3);
- }
- if(e.getSource() == jL4){
- deplace(2);
- }
- }
- public void mousePressed(MouseEvent e){
- Double t1, t2;
- if(e.getSource() == jB1)
- {
-
- if(count<10)
- {
- count++;
- System.out.println("zoom +" );
- t1 = new Double(actuelw*0.8);
- t2 = new Double(actuelh*0.8);
- zoom(actuelx,actuely,t2.intValue(),t1.intValue());
- }
- }
- if(e.getSource() == jB2)
- {
- if(count>0)
- {
- count--;
- System.out.println("zoom -" );
- t1 = new Double(actuelw/0.8);
- t2 = new Double(actuelh/0.8);
- zoom(actuelx,actuely,t2.intValue(),t1.intValue());
- }
- }
- }
- public void mouseReleased(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
- public void mouseEntered(MouseEvent e){
- if(e.getSource() == jL1){
- deplace(1);
- }
- if(e.getSource() == jL2){
- deplace(4);
- }
- if(e.getSource() == jL3){
- deplace(3);
- }
- if(e.getSource() == jL4){
- deplace(2);
- }
- }
- public void run()
- {
- while(true)
- {
- if(modif)
- {
- //dessinePoint();
- modif=false;
- }
- }
- }
- public void onafaitdesmodif()
- {
- modif=true;
- }
- public void zoom(int x, int y, int height, int width)
- {
- ImageFilter cif;
- ImageProducer improd;
- System.out.println("dans zoom (1)" );
- improd = base.getSource();
- System.out.println("dans zoom (2)" );
- cif = new CropImageFilter(x, y, height, width);
- affiche = createImage(new FilteredImageSource(improd, cif));
- System.out.println("dans zoom (3)" );
- actuelx=x;
- actuely=y;
- actuelh = height;
- actuelw = width;
- System.out.println("dans zoom (4)" );
- dessinePoint();
- paint(this.getGraphics());
- }
- public void deplace(int direction)
- {
- int aw=actuelw/10;
- int ah=actuelh/10;
- if((direction==1) && (actuely>ah)) //hau
- {
- zoom(actuelx, actuely -ah, actuelh, actuelw);
- }
- else if((direction==3) && (actuely<longueurcarte-ah)) //bas
- {
- zoom(actuelx, actuely +ah, actuelh, actuelw);
- }
- else if((direction==2) && (actuelx>aw)) //gauche
- {
- zoom(actuelx -aw , actuely, actuelh, actuelw);
- }
- else if((direction==4) && (actuelx<largeurcarte-actuelw)) //droite
- {
- zoom(actuelx + aw, actuely, actuelh, actuelw);
- }
- }
- public void repeindre(JComponent j)
- {
- j.repaint();
- }
- public void paint(Graphics g)
- {
- Graphics g2=jP2.getGraphics();
- jP1.paint(jP1.getGraphics());
- jP2.paint(g2);
- repeindre(jL6);
- for(int i=0;i<tab.length;i++)
- {
- try
- {
- tab[i].setVisible(true);
- repeindre(tab[i]);
- }
- catch(Exception e){}
- }
- jL7.repaint();
- jP3.paint(jP3.getGraphics());
- g2.drawImage(affiche, 38, 38, h-76, w-76, this);
- for(int i=0;i<tab.length;i++)
- {
- try
- {
- tab[i].setVisible(true);
- repeindre(tab[i]);
- }
- catch(Exception e){}
- }
- }
- private void dessineLocation(int x, int y, int numero)
- {
- int t=5;
- //System.out.println((x>actuelx) + " "+ (x< actuelx+actuelh) +"&&" + (y>actuely)+" &&"+ (y< actuely+actuelw));
- if((x>actuelx) && (x< actuelx+actuelh) && (y>actuely) && (y< actuely+actuelw))
- {
- //le point est dans la carte
- int calculx, calculy;
- calculx = x*h/actuelw + actuelx;
- calculy = y*w/actuelh + actuely;
- System.out.println(x + " --- " + h + " --- " + actuelw + " --- " + calculx);
- //tab[numero]= new JLabel("a" );
- //tab[numero].setVisible(true);
- //jP2.remove(tab[numero]);
- System.out.println("avant placage point: "+numero);
- jP2.add(tab[numero],new AbsoluteConstraints(calculx,calculy,10,10));
- System.out.println("apres placage point: "+numero);
- //tab[numero].validate();
- //tab[numero].setVisible(true);
- tab[numero].paintImmediatly();
- }
- else
- {
- }
- }
- public void dessinePoint()
- {
- int nb=2;
- // récupération des X et Y
- cooX= new int[nb];
- cooY= new int[nb];
- // tab = new JLabel [nb];
- cooX[0]=200;cooY[0]=200;
- cooX[1]=150;cooY[1]=300;
- // pour chaque, utilisation de dessineLocation
- for(int i=0; i<nb;i++)
- {
- System.out.println(i);
- dessineLocation(cooX[i],cooY[i],i);
- }
- // repeindre
- }
- }
|
Le problème est le suivant : au début, tout s'affiche bien. Mais ensuite, à chaque paint(), l'image se remet pardessus les JLabel tab... Quelqu'un pourrait me dire comment faire svp?
|