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
|
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TraceCourbe extends JComponent
{
private HistPositions liste;
private Color couleur;
public TraceCourbe (HistPositions list,Color couleur)
{
super();
liste = list;
this.couleur=couleur;
}
protected void paintComponent (Graphics g)
{
// Appel de la méthode dans la classe mère
super.paintComponent (g);
// On peut tracer les points
g.setColor (couleur);
Position pos = liste.getFirst();
int lati1,longi1,lati2,longi2;
while(pos.getNext()!=null)
{
lati1 = (int)(pos.getVal()[0]);
longi1 =(int)( pos.getVal()[1]);
pos=pos.getNext();
lati2= (int)(pos.getVal()[0]);
longi2 = (int)(pos.getVal()[1]);
g.fillOval(lati1, longi1, 16, 16);
g.drawLine ( (lati1), (longi1), (lati2), (longi2));
g.fillOval(lati2, longi2, 16, 16);
}
}
public static void main (String[] args)
{
// J'ai mis ici un jeu de test, avec juste trois positions
Position pos1 = new Position (100,100);
Position pos2 = new Position (200,200);
Position pos3 = new Position (150,200);
HistPositions ListPos = new HistPositions();
ListPos.insertFirst(pos1);
ListPos.insertFirst(pos2);
ListPos.insertFirst(pos3);
TraceCourbe tr= new TraceCourbe(ListPos, Color.black);
JFrame frame = new JFrame ("TraceCourbe");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (new Dimension (500, 500));
// Insertion du TraceCourbe :
frame.getContentPane().add(tr);
// => ça marche
JPanel pane = new JPanel();
frame.add(pane);
pane.setLocation(0,0);
pane.setSize(new Dimension(300,300));
pane.add (tr);
// =>ça ne marche pas
frame.setVisible (true);
}
} |
Partager