Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2344 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  [java] pb repaint() et update()

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[java] pb repaint() et update()

n°1259948
sebounet
Posté le 06-12-2005 à 13:15:41  profilanswer
 

Salut a tous!!
 
Je fais appel à votre aide rapidement, je suis tout nouveau dans la prog java.
Je suis sur un programme qui doit me dessiner des triangles lorsque je clique sur la souris a tel endroit de l ecran.
Le triangle se dessine comme il faut la ou je clique pas de pb, mais seulement lorsque je clique sur un autre endroit le nouveau triangle se dessine  
mais l ancien disparait.
J'ai regardé un peu partout sur google et j ai trouvé que ça se passait au niveau de la methode repaint() qui appelle la methode update qui efface l ecran a chaque fois.
Il faut donc que je redefinisse cette methode update() mais je ne sais pas comment.
 
j ai essayé de la mettre a plusieur endroit dans le programme mais ça marche toujours pas...ça me fait peter un cable...pour un truc aussi con...!!
Voici mon programme.
Ne faites pas attention a la classe pieces...
 

Code :
  1. package triomino;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import javax.swing.event.MouseInputListener;
  5. import java.awt.event.MouseEvent;
  6. /**
  7. * <p>Titre : TRIOMINO++</p>
  8. */
  9. public class UI extends JFrame {
  10.     JPanel contentPane;
  11.     JLabel label;
  12.     static int GridSpace = 150;
  13.     Piece Triangle = new Piece();
  14.     private Point clickPoint, cursorPoint;
  15.     public void creerUI() {
  16.         CoordinateArea coordinateArea = new CoordinateArea(this, Triangle);
  17.         contentPane.add(coordinateArea);
  18.         label = new JLabel();
  19.         resetLabel();
  20.         contentPane.add(label);
  21.         //Align the left edges of the components.
  22.         coordinateArea.setAlignmentX(Component.LEFT_ALIGNMENT);
  23.         label.setAlignmentX(Component.LEFT_ALIGNMENT); //redundant
  24.     }
  25.     public UI() {
  26.         try {
  27.             setDefaultCloseOperation(EXIT_ON_CLOSE);
  28.             jbInit();
  29.         } catch (Exception exception) {
  30.             exception.printStackTrace();
  31.         }
  32.     }
  33.     public void updateCursorLocation(int x, int y) {
  34.         if (x < 0 || y < 0) {
  35.             cursorPoint = null;
  36.             updateLabel();
  37.             return;
  38.         }
  39.         if (cursorPoint == null) {
  40.             cursorPoint = new Point();
  41.         }
  42.         cursorPoint.x = x;
  43.         cursorPoint.y = y;
  44.         updateLabel();
  45.     }
  46.     public void updateClickPoint(Point p) {
  47.         clickPoint = p;
  48.         updateLabel();
  49.     }
  50.     public void resetLabel() {
  51.         cursorPoint = null;
  52.         updateLabel();
  53.     }
  54.     protected void updateLabel() {
  55.         String text = "";
  56.         if ((clickPoint == null) && (cursorPoint == null)) {
  57.             text = "cliquer ou bouger le curseur dans la zone ";
  58.         } else {
  59.             if (clickPoint != null) {
  60.                 text += "Le Dernier clique etait au ("
  61.                         + clickPoint.x + ", " + clickPoint.y + " ). ";
  62.             }
  63.             if (cursorPoint != null) {
  64.                 text += "Le curseur est au ("
  65.                         + cursorPoint.x + ", " + cursorPoint.y + " ). ";
  66.             }
  67.         }
  68.         label.setText(text);
  69.     }
  70.     public static class CoordinateArea extends JComponent implements
  71.             MouseInputListener {
  72.         Point point = null;
  73.         UI controller;
  74.         Dimension preferredSize = new Dimension(400, 500);
  75.         Color gridColor;
  76.         Piece Triangle;
  77.         public CoordinateArea(UI controller, Piece Triangle) {
  78.             this.controller = controller;
  79.             this.Triangle = Triangle;
  80.             //Add a border of 5 pixels at the left and bottom,
  81.             //and 1 pixel at the top and right.
  82.             setBorder(BorderFactory.createMatteBorder(1, 5, 5, 1,
  83.                     Color.BLUE));
  84.             addMouseListener(this);
  85.             addMouseMotionListener(this);
  86.             setBackground(Color.DARK_GRAY);
  87.             setOpaque(true);
  88.         }
  89.         public void update(Graphics g) {
  90.                  paint(g);
  91.         }
  92.         public Dimension getPreferredSize() {
  93.             return preferredSize;
  94.         }
  95.         protected void paintComponent(Graphics g) {
  96.             //Paint background if we're opaque.
  97.             if (isOpaque()) {
  98.                 g.setColor(getBackground());
  99.                 g.fillRect(0, 0, getWidth(), getHeight());
  100.             }
  101.             //Paint 20x20 grid.
  102.             g.setColor(Color.GRAY);
  103.             drawGrid(g, GridSpace);
  104.             //If user has chosen a point, paint a small dot on top.
  105.             if (point != null) {
  106.                 g.setColor(getForeground());
  107.                 g.fillRect(point.x - 3, point.y - 3, 7, 7);
  108.                 DessinerTriangle(g);
  109.            }
  110.         }
  111.         //Draws a 20x20 grid using the current color.
  112.         private void drawGrid(Graphics g, int gridSpace) {
  113.             Insets insets = getInsets();
  114.             int firstX = insets.left;
  115.             int firstY = insets.top;
  116.             int lastX = getWidth() - insets.right;
  117.             int lastY = getHeight() - insets.bottom;
  118.             //Draw vertical lines.
  119.             int x = firstX;
  120.             while (x < lastX) {
  121.                 g.drawLine(x, firstY, x, lastY);
  122.                 x += gridSpace;
  123.             }
  124.             //Draw horizontal lines.
  125.             int y = firstY;
  126.             while (y < lastY) {
  127.                 g.drawLine(firstX, y, lastX, y);
  128.                 y += gridSpace;
  129.             }
  130.         }
  131.         //Methods required by the MouseInputListener interface.
  132.         public void mouseClicked(MouseEvent e) {
  133.             int x = e.getX();
  134.             int y = e.getY();
  135.             if (point == null) {
  136.                 point = new Point(x, y);
  137.             } else {
  138.                 point.x = x;
  139.                 point.y = y;
  140.             }
  141.             controller.updateClickPoint(point);
  142.             repaint();
  143.         }
  144.        
  145.         public void DessinerTriangle(Graphics g) {
  146.             int[] X = new int[3];
  147.             int[] Y = new int[3];
  148.             X[0] = (point.x / GridSpace) % GridSpace * GridSpace;
  149.             X[1] = (point.x / GridSpace) % GridSpace * GridSpace;
  150.             X[2] = (point.x / GridSpace) % GridSpace * GridSpace + GridSpace;
  151.             Y[0] = (point.y / GridSpace) % GridSpace * GridSpace;
  152.             Y[1] = (point.y / GridSpace) % GridSpace * GridSpace + GridSpace;
  153.             Y[2] = (point.y / GridSpace) % GridSpace * GridSpace + GridSpace;
  154.             //Triangle.pivoterAGauche();
  155.             //Triangle.setX(X);
  156.             //Triangle.setY(Y);
  157.             g.setColor(Color.red);
  158.             g.drawPolygon(X, Y, 3);
  159.             g.fillPolygon(X, Y, 3);
  160.         }
  161.         public void mouseMoved(MouseEvent e) {
  162.             controller.updateCursorLocation(e.getX(), e.getY());
  163.         }
  164.         public void mouseExited(MouseEvent e) {
  165.             controller.resetLabel();
  166.         }
  167.         public void mouseReleased(MouseEvent e) {}
  168.         public void mouseEntered(MouseEvent e) {}
  169.         public void mousePressed(MouseEvent e) {}
  170.         public void mouseDragged(MouseEvent e) {}
  171.     }
  172.     /**
  173.      * Initialisation du composant.
  174.      *
  175.      * @throws java.lang.Exception
  176.      */
  177.     private void jbInit() throws Exception {
  178.         contentPane = (JPanel) getContentPane();
  179.         contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
  180.         this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  181.         setSize(new Dimension(600, 500));
  182.         setTitle("TRIOMINO++" );
  183.     }
  184.     private class Piece {
  185.         /**
  186.          * Définition des coordonnées du triangle en fonction de
  187.          * la position courante de la souris.
  188.          */
  189.         private int[] X = new int[3];
  190.         private int[] Y = new int[3];
  191.         private int[] Chiffre = new int[3];
  192.         Piece(int[] x, int[] y) {
  193.             X = x;
  194.             Y = y;
  195.         }
  196.         Piece() {
  197.         }
  198.         public void setChiffre(int[] chiffre) {
  199.             Chiffre = chiffre;
  200.         }
  201.         public void setX(int[] x) {
  202.             X = x;
  203.         }
  204.         public void setY(int[] y) {
  205.             Y = y;
  206.         }
  207.         public int[] getChiffre(int[] chiffre) {
  208.             return Chiffre;
  209.         }
  210.         public void pivoterADroite() {
  211.             int aux = X[0];
  212.             X[0] = X[1];
  213.             X[1] = X[2];
  214.             X[2] = aux;
  215.             aux = Y[0];
  216.             Y[0] = Y[1];
  217.             Y[1] = Y[2];
  218.             Y[2] = aux;
  219.             aux = Chiffre[0];
  220.             Chiffre[0] = Chiffre[1];
  221.             Chiffre[1] = Chiffre[2];
  222.             Chiffre[2] = aux;
  223.         }
  224.         public void pivoterAGauche() {
  225.             int aux = X[0];
  226.             X[0] = X[2];
  227.             X[2] = X[1];
  228.             X[1] = aux;
  229.             aux = Y[0];
  230.             Y[0] = Y[2];
  231.             Y[2] = Y[1];
  232.             Y[1] = aux;
  233.             aux = Chiffre[0];
  234.             Chiffre[0] = Chiffre[2];
  235.             Chiffre[2] = Chiffre[1];
  236.             Chiffre[1] = aux;
  237.         }
  238.     }
  239. }


Message édité par sebounet le 08-12-2005 à 13:46:12
mood
Publicité
Posté le 06-12-2005 à 13:15:41  profilanswer
 

n°1261156
Trollable
Posté le 07-12-2005 à 16:51:11  profilanswer
 

reponse courte:

Code :
  1. public void update(Graphics _g) { paint(_g); }


---------------
The Million Dollar Screenshot  (seo v7ndotcom elursrebmem paesys wifi)

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  [java] pb repaint() et update()

 

Sujets relatifs
FTP javaPerceptron Java
Java/swing : équivalent de ce composant graphiqueCherche Bon livre pour commencer JAVA
[Langage D]C++ + Java + Python = DUpload en JAVA
[Java] Architecture pipes-filters[java] Tracer un rectangle en temps réel
[Java] Aide sur projet avec interface graphique ( Pas des fenêtres)[JAVA] Empecher la saisie dans une jtable
Plus de sujets relatifs à : [java] pb repaint() et update()


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR