thunderbird54 | non pas de clear voic le code...
il s'articule autour de 3 classes, la pincipale, le fond (bouton,menu, gestionnaire de clik) et repere (tracage)
classe Fentre :
Code :
- import javax.swing.*;
- import java.awt.*;
- public class Fenetre extends JFrame {
-
- static Fond pan;
- public static void main(String args[])
- {
- Fenetre Fen= new Fenetre();
- pan.initialise();
- }
-
- public Fenetre()
- {
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setTitle("Projet JAVA" );
- setSize(1000,500);
- Container contentPane = getContentPane();
-
- pan = new Fond();
-
- contentPane.add(pan);
- setVisible(true);
-
- }
-
-
-
-
- }
|
Classe Fond : (Attention c long)
Code :
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Fond extends JPanel implements ActionListener
- {
-
- private JButton btnQuitter,B1,B2,B3,B4,B5,B6,Repere;
- private JLabel Param1,Param2,Param3,Param4;
- private JTextField xmin,xmax,ymin,ymax,Coord_X,Coord_Y,Coord_XF1,Coord_YF1,Coord_XF2,Coord_YF2,longueur;
- private Repere R1;
- private JPanel P_Centre;
- private int Action;
- private int nbr=0,xl,yl,xl1,yl1;
-
-
-
-
-
- public Fond()
- {
- setLayout(new BorderLayout());
-
- Haut();
- Gauche();
- Bas();
-
-
- P_Centre=new JPanel();
- P_Centre.addMouseListener(new MouseClickHandler());
- P_Centre.addMouseMotionListener(new Mouse_Dep());
- P_Centre.addComponentListener(new gestion_resize());
-
- add(P_Centre,BorderLayout.CENTER);
- }
-
- public void initialise()
- {
- R1=new Repere(Double.parseDouble(xmin.getText()),Double.parseDouble(xmax.getText()),Double.parseDouble(ymin.getText()),Double.parseDouble(ymax.getText()),P_Centre);
- }
-
-
- public void Haut()
- {
-
- JMenuBar PMenu = new JMenuBar();
-
- JMenu M1 = new JMenu("Fichier" );
- JMenuItem M1_I1= new JMenuItem("Enregistrer" );
- M1_I1.addActionListener(this);
- JMenuItem M1_I2 = new JMenuItem("Quitter" );
- M1_I2.addActionListener(this);
-
- JMenu M2 = new JMenu("Dessiner" );
- JMenu M4 = new JMenu("Aide" );
-
- JMenuItem M2_I1= new JMenuItem("Point" );
- JMenuItem M2_I2= new JMenuItem("Ligne" );
- JMenuItem M2_I3= new JMenuItem("Carre" );
- JMenuItem M2_I4= new JMenuItem("Rectangle" );
- JMenuItem M2_I5= new JMenuItem("Cercle" );
- JMenuItem M2_I6= new JMenuItem("Elipse" );
-
- M1.add(M1_I1);
- M1.add(M1_I2);
-
- M2.add(M2_I1);
- M2.add(M2_I2);
- M2.add(M2_I3);
- M2.add(M2_I4);
- M2.add(M2_I5);
- M2.add(M2_I6);
-
- PMenu.add(M1);
- PMenu.add(M2);
- PMenu.add(M4);
-
- add(PMenu,BorderLayout.NORTH);
- }
-
-
- public void Gauche()
- {
-
- JPanel P = new JPanel();
- P.setLayout(new GridLayout(21,1));
-
- B1 = new JButton ("Point" );
- B1.addActionListener(this);
- P.add(B1);
-
- B2 = new JButton ("Ligne" );
- B2.addActionListener(this);
- P.add(B2);
-
- B3 = new JButton ("Carre" );
- B3.addActionListener(this);
- P.add(B3);
-
- B4 = new JButton ("Rectangle" );
- B4.addActionListener(this);
- P.add(B4);
-
- B5 = new JButton ("Cercle" );
- B5.addActionListener(this);
- P.add(B5);
-
- B6 = new JButton ("Elipse" );
- B6.addActionListener(this);
- P.add(B6);
-
- P.add(new JLabel("" ));
- P.add(new JLabel("Souris " ));
-
- Coord_X = new JTextField(4);
- Coord_X.setEditable(false);
- P.add(Coord_X);
-
-
- Coord_Y = new JTextField(4);
- Coord_Y.setEditable(false);
- P.add(Coord_Y);
-
- P.add(new JLabel("" ));
- P.add(new JLabel("Point 1" ));
-
- Coord_XF1 = new JTextField(4);
- Coord_XF1.setEditable(false);
- P.add(Coord_XF1);
-
-
- Coord_YF1 = new JTextField(4);
- Coord_YF1.setEditable(false);
- P.add(Coord_YF1);
-
- P.add(new JLabel("" ));
- P.add(new JLabel("Point 2" ));
-
- Coord_XF2 = new JTextField(4);
- Coord_XF2.setEditable(false);
- P.add(Coord_XF2);
-
-
- Coord_YF2 = new JTextField(4);
- Coord_YF2.setEditable(false);
- P.add(Coord_YF2);
-
-
- P.add(new JLabel("" ));
- P.add(new JLabel("Longueur" ));
-
- longueur = new JTextField(4);
- longueur.setEditable(false);
- P.add(longueur);
-
-
- add(P,BorderLayout.WEST);
-
- }
-
-
-
- public void Bas()
- {
-
- JPanel P_SUD = new JPanel();
- //P_SUD.setLayout(new GridLayout(2,12));
- Param1 = new JLabel("xmin : " );
- P_SUD.add(Param1);
- xmin = new JTextField(3);
- P_SUD.add(xmin);
-
- Param2 = new JLabel("xmax : " );
- P_SUD.add(Param2);
- xmax = new JTextField(3);
- P_SUD.add(xmax);
-
- Param3 = new JLabel("ymin : " );
- P_SUD.add(Param3);
- ymin = new JTextField(3);
- P_SUD.add(ymin);
-
- Param4 = new JLabel("ymax : " );
- P_SUD.add(Param4);
- ymax = new JTextField(3);
- P_SUD.add(ymax);
-
- Repere = new JButton ("Changer Repere" );
- Repere.addActionListener(this);
- P_SUD.add(Repere);
-
- xmin.setText("-10" );
- xmax.setText("10" );
- ymin.setText("-5" );
- ymax.setText("5" );
-
-
- add(P_SUD,BorderLayout.SOUTH);
-
- }
-
-
-
-
-
-
-
-
- // GESTION EVENEMENTS
-
-
- public void actionPerformed(ActionEvent e)
- {
- String Choix = e.getActionCommand();
-
- Coord_XF1.setText("" );
- Coord_YF1.setText("" );
- Coord_XF2.setText("" );
- Coord_YF2.setText("" );
-
- if (Choix.equals("Quitter" ))
- {
- System.exit(0);
- }
- else if (Choix.equals("Point" ))
- {
- Action = 1;
- //Coord_XF1.setBackground("orange" );
- }
- else if (Choix.equals("Ligne" ))
- {
- Action = 2;
- }
- else if (Choix.equals("Carre" ))
- {
- Action = 3;
- }
- else if (Choix.equals("Rectangle" ))
- {
- Action = 4;
- }
- else if (Choix.equals("Cercle" ))
- {
- Action = 5;
- }
- else if (Choix.equals("Elipse" ))
- {
- Action = 6;
- }
-
- else if (Choix.equals("Changer Repere" ))
- {
-
- // P_Centre=R1.effacerEcran();
- R1=new Repere(Double.parseDouble(xmin.getText()),Double.parseDouble(xmax.getText()),Double.parseDouble(ymin.getText()),Double.parseDouble(ymax.getText()),P_Centre);
-
- }
-
- }
-
-
-
-
-
- private class MouseClickHandler extends MouseAdapter{
-
-
- public void mouseClicked(MouseEvent e)
- {
- nbr++;
-
- if (nbr==1 && Action!=0)
- {
- xl=e.getX();
- yl=e.getY();
- Coord_XF1.setText(Double.toString((int)(100*R1.get_CoordX(e.getX()))/100.00));
- Coord_YF1.setText(String.valueOf((int)(100*R1.get_CoordY(e.getY()))/100.00));
- }
- else if (nbr==2 && Action!=0 && Action!=1)
- { xl1=e.getX();
- yl1=e.getY();
- Coord_XF2.setText(Double.toString((int)(100*R1.get_CoordX(e.getX()))/100.00));
- Coord_YF2.setText(String.valueOf((int)(100*R1.get_CoordY(e.getY()))/100.00));
-
- }
-
-
- System.out.println(Action);
- if (nbr==2 || Action==1)
- {
-
- switch(Action)
- {
- case 1:
- {
- R1.Point_IJ(e.getX(),e.getY());
- break;
- }
-
- case 2:
- {
- R1.ligne_IJ(xl,yl,xl1,yl1);
- break;
- }
-
- case 3 :
- {
- R1.Carre_IJ(xl,yl,xl1);
- break;
- }
-
- case 4:
- {
- R1.Rectangle_IJ(xl,yl,xl1,yl1);
- break;
- }
-
- case 5:
- {
- R1.Cercle_IJ(xl,yl,xl1,yl1);
- break;
- }
-
- case 6:
- {
- R1.Elipse_IJ(xl,yl,xl1,yl1);
- break;
- }
-
-
- }
- nbr=0;
-
- }
-
- }
-
- public void mousePressed(MouseEvent e) {}
-
- public void mouseReleased(MouseEvent e) {}
-
- public void mouseEntered(MouseEvent e) {}
-
- public void mouseExited(MouseEvent e)
- {
- Coord_X.setText("Out" );
- Coord_Y.setText("Out" );
- }
-
-
- }
-
-
- private class Mouse_Dep extends MouseMotionAdapter
- {
-
- public void mouseMoved(MouseEvent e)
- {
- try
- {
- Coord_X.setText("x = "+Double.toString((int)(100*R1.get_CoordX(e.getX()))/100.00));
- Coord_Y.setText("y = "+String.valueOf((int)(100*R1.get_CoordY(e.getY()))/100.00));
- }
- catch (Exception r)
- {
- }
- if (Action == 3 && nbr==1)
- longueur.setText(String.valueOf((int)(100*R1.get_CoordX(Math.abs(xl-e.getX())))/100.00));
-
- }
-
- public void mouseDragged(MouseEvent e) {}
- }
-
-
-
-
-
- public class gestion_resize extends ComponentAdapter
-
-
- {
- public void componentHidden(ComponentEvent e) {}
- public void componentMoved(ComponentEvent e) {}
- public void componentResized(ComponentEvent e)
- {
- R1=new Repere(Double.parseDouble(xmin.getText()),Double.parseDouble(xmax.getText()),Double.parseDouble(ymin.getText()),Double.parseDouble(ymax.getText()),P_Centre);
- }
- public void componentShown(ComponentEvent e) {}
-
-
- }
-
-
- }
|
et pour finir la class Repere :
Code :
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Repere extends JPanel
- { private double xmin,xmax,ymin,ymax,w=382,h=196;
- private Dimension dim;
- private JPanel Pan;
- private Graphics g;
-
- public Repere(double xmin,double xmax,double ymin,double ymax,JPanel P)
- {
- double i,j;
-
- Pan=P;
- g=Pan.getGraphics();
-
-
- dim=Pan.getSize();
- if (dim.width!=0) w = dim.width-10;
- if (dim.height!=0) h = dim.height-10;
-
- this.xmin=xmin;
- this.xmax=xmax;
- this.ymin=ymin;
- this.ymax=ymax;
-
- ligne(xmin,0,xmax,0);
- ligne(0,ymax,0,ymin);
-
- for(i=0;i<=(Math.abs(xmax)+Math.abs(xmin));i++)
- {
- ligne(xmin+i,0.1,xmin+i,-.1);
- }
-
- for(j=0;j<=(Math.abs(ymax)+Math.abs(ymin));j++)
- {
- ligne(0.1,ymin+j,-0.1,ymin+j);
- }
- }
-
-
- public void ligne(double x,double y,double x1,double y1)
- {
- g.drawLine(getI(x),getJ(y),getI(x1),getJ(y1));
- }
-
- public void ligne_IJ(int x,int y,int x1,int y1)
- {
- g.drawLine(x,y,x1,y1);
- }
-
-
-
-
-
- public void Point_IJ(int x,int y)
- {
- g.drawLine(x,y,x,y);
- }
-
- public void Point(int x,int y)
- {
- g.drawLine(getI(x),getJ(y),getI(x),getJ(y));
- }
-
-
-
-
- public void Carre_IJ(int x,int y,int l)
- {
-
- g.drawRect(x,y,l,l);
-
-
- }
-
- public void Cercle_IJ(int x,int y,int x1,int y1)
- {
- g.drawOval(x,y,x1-x,x1-x);
-
- }
-
- public void Elipse_IJ(int x,int y,int x1,int y1)
- {
- g.drawOval(x,y,x1-x,y1-y);
-
- }
-
-
-
- public void Rectangle_IJ(int x,int y,int x1,int y1)
- {
- if (x<x1 && y1>=y)
- g.drawRect(x,y,Math.abs(x1-x),Math.abs(y1-y));
-
- if (x<x1 && y1<y)
- g.drawRect(x,y1,Math.abs(x1-x),Math.abs(y1-y));
-
- if (x>=x1 && y1<y)
- g.drawRect(x1,y1,Math.abs(x1-x),Math.abs(y1-y));
-
- if (x>=x1 && y1>=y)
- g.drawRect(x1,y,Math.abs(x1-x),Math.abs(y1-y));
-
- }
-
-
- public int getI(double x)
- {
- return ((int)(w*((x-xmin)/(xmax-xmin))))+5;
- }
-
-
- public int getJ(double y)
- {
- return ((int)(h*((ymax-y)/(ymax-ymin))))+5;
- }
-
- public double get_CoordX(int i)
- {
- return (i*(xmax-xmin)/w+xmin);
- }
-
-
- public double get_CoordY(int j)
- {
- return (ymax-j*(ymax-ymin)/h);
- }
-
-
- }
|
voila cherchez l'erreur! Message édité par thunderbird54 le 24-11-2002 à 01:19:24
|