Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with repaint

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help with repaint

    Hey guys,

    Ok so basically what I want to do is that when the user clicks on one of the squares, the cercle with in it changes places.

    So far, I have the hit detection ( I know which square, so which cercle have been clicked ). I have added the coordinates, but the repaint()( in my GestEVT and mousePressed) doesnt work...

    I followed the following tutorials:
    http://zetcode.com/tutorials/java2dtutorial/hitmove/

    Here is my code:
     
    /**
     * Travail fait par Aravinthan Sivaneswaran
     * Date: 27-Sep-2011
     * Nom du projet: Projet d'intégration (La guerre des pions)
     */
    /**
     * Cette classe permet d'avoir les differents fenetres
     * necessaire pour le projet ainsi que les
     * �v�nements de gestion ( clavier et souris )
     */
    // Les imports necessaire au projet
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
     
    class views extends JFrame {
     
        // Declarer le panneau de jeu
        JPanel gameplayPanel;
     
        // D�clarer les boutons qui sera utilis� a travers la classe.
        JComboBox moveWhite;
        JComboBox moveBlack;
     
        // Declarer les formes qui sera utilise a travers la classe.
        Rectangle2D.Double[] rectangle;
        ZEllipse[] cercle;
     
     
        // Fonction qui permet d'avoir les differents f�netres du fonction.
        public views() {
     
            super("Creer les formes pour le jeu");
     
            setTitle("Game window");
            setSize(1200, 700);
     
            gameplayPanel = new JPanel();
            JPanel movesPanel = new JPanel();
     
            GestEvt listener=new GestEvt();
            gameplayPanel.addMouseListener(listener);
     
            Container contentPane = getContentPane();
     
     
            JLabel white_label = new JLabel("Bouger les pions blanc");
            moveWhite = new JComboBox();
            //moveWhite.addItemListener(this);
            moveWhite.addItem("Bouger pion 1");
            moveWhite.addItem("Bouger pion 2");
     
            JLabel black_label = new JLabel("Bouger les pions noir");
            moveBlack = new JComboBox();
            //moveBlack.addItemListener(this);
            moveBlack.addItem("Bouger pion 1");
            moveBlack.addItem("Bouger pion 2");
     
            JTextArea userMoves = new JTextArea(20, 100);
     
     
            gameplayPanel.add(white_label);
            gameplayPanel.add(moveWhite);
            gameplayPanel.add(black_label);
            gameplayPanel.add(moveBlack);
     
            movesPanel.add(userMoves);
     
            contentPane.add(gameplayPanel, BorderLayout.CENTER);
            contentPane.add(movesPanel, BorderLayout.SOUTH);
     
        }
     
        // Fonction qui permet de dessiner les diff�rent formes 2D.
        public void paint(Graphics g) {
     
            // Appeler la méthode paint de la superclasse.
            super.paint(g);
     
            // Créer la 2D par le remplacement du type de g en Graphics2D.
            Graphics2D g2d = (Graphics2D) g;
     
     
            // Tracer autant de rectangles demandes par l'utilisateur.
            rectangle = new Rectangle.Double[5];
     
            for (int x = 0; x < 5; x++) {
                rectangle[x] = new Rectangle2D.Double(250 + (x * 100), 250, 100, 100);
                rectangle[x].setFrame(250 + (x * 100), 250, 100, 100);
                g2d.draw(rectangle[x]);
            }
     
            // Tracer autant de cercles necessaire.
            cercle = new ZEllipse[4];
            int y = 0;
     
            for (int x = 0; x < 5; x++) {
                g2d.setPaint(Color.black);
                if (x == 2) {
                    g2d.setPaint(Color.white);
                    x = 3;
                } else if (x == 4) {
                    g2d.setPaint(Color.white);
                }
                cercle[y] = new ZEllipse(300 + (x * 100), 300, 10, 10);
                g2d.fill(cercle[y]);
                y++;
            }
     
        }
     
        class ZEllipse extends Ellipse2D.Float {
            public ZEllipse(float x, float y, float width, float height) {
                setFrame(x, y, width, height);
            }
     
            public void addX(float x) {
                this.x += x;
            }
     
            public void addY(float y) {
                this.y += y;
            }
     
     
        }
     
        class validation{
            public boolean isHit(int s, int c, int x, int y){
                double cercleX = cercle[c].getX();
                double cercleY = cercle[c].getY();
                System.out.println("Square"+s+"Cercle"+c);
                if(rectangle[s].contains(cercleX, cercleY)){
                    return true;
                }
                else{
                    return false;
                }
     
            }
        }
     
        private class GestEvt extends MouseAdapter{
            public void mousePressed(MouseEvent event){
               int x = event.getX();
               int y = event.getY();
               int square;
               int point;
               validation v = new validation();
     
               // Creer la zone du rectangle
               if(((y>249 && y<351) && (x>250 && x<(250+(5*100))))){
                    square = (x-250)/100;
                    point = square;
                    if(square == 4){
                        point = 3;
                    }
     
                    // Verifier s'il contient un point
                    for (int i = 0; i < 4; i++) {
                        if(v.isHit(square, i, x, y)){
                            System.out.println("IS HIT");
                            System.out.println("X before:" +cercle[i].getX());
                            cercle[i].addX(100);
                            cercle[i].addY(100);
                            repaint();
                            System.out.println("X after:" +cercle[i].getX()); 
                        }
                    }
     
              }
            }
        }
     
    }

    Thanks for your help,
    Ara
    Last edited by AraHabs; October 16th, 2011 at 09:33 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with repaint

    Please define "Doesn't work". For what its worth, you should consider drawing within a JPanel, and override the paintComponent method (as opposed to drawing directly in a JFrame using paint)

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with repaint

    Quote Originally Posted by copeg View Post
    Please define "Doesn't work". For what its worth, you should consider drawing within a JPanel, and override the paintComponent method (as opposed to drawing directly in a JFrame using paint)
    Well, it doesnt paint. I mean the println give me the right infos ( "IS HIT" ), changes the x coordinates but it doesnt repaint...
    As for drawing in JPanel, it would mean that I would have to repaint the whole Panel... woudn't it be easier to repaint just that shape?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with repaint

    Quote Originally Posted by AraHabs View Post
    Well, it doesnt paint. I mean the println give me the right infos ( "IS HIT" ), changes the x coordinates but it doesnt repaint...
    The cercle references are remade every call to paint - they are not being reused. Thus every call to addX/Y is lost when the reference is assigned a new value
    Quote Originally Posted by AraHabs View Post
    As for drawing in JPanel, it would mean that I would have to repaint the whole Panel... woudn't it be easier to repaint just that shape?
    That is exactly what you are doing now, repainting the JFrame.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with repaint

    Well what should I do then?

    I am kind of lost to what to do... I've been trying to make this work for the last week or so...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with repaint

    What does your code look like now. Did you add a JPanel to contain the drawing and override its paintComponent method?

Similar Threads

  1. My JFrame won't repaint correctly please help me out!
    By ocolegrove in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2011, 10:04 PM
  2. help in update and repaint() methods (Grahpics2D)
    By Nawaf in forum Java Theory & Questions
    Replies: 2
    Last Post: December 18th, 2010, 11:05 AM
  3. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 04:00 PM
  4. Repaint,
    By Time in forum AWT / Java Swing
    Replies: 3
    Last Post: May 21st, 2010, 11:23 PM
  5. Repaint doesn't repaint?
    By PotataChipz in forum AWT / Java Swing
    Replies: 6
    Last Post: January 18th, 2010, 09:56 PM