Results 1 to 10 of 10
Like Tree4Likes
  • 1 Post By wdh321
  • 3 Post By wdh321

Thread: repaint() will not re draw panel

  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default repaint() will not re draw panel

    I am creating a GUI for a dice game.
    I hace created a JPanel which holds my dice and, as well as a rollDice() method which creates a random number between 1-6 for each dice.
    I then have a throwDice() method which calls rollDice() and then repaints the frame.
    For some reason the dice panel will not re paint with the new values.
    I have added a println into rollDice() and paintComponent to see whether the values are being created. And when i click my button, two random numbers are created, and they also appear in paintComponent.
    So it seems like the issue is just with the re painting of the panel.

    Java Code:
    public class DicePanel extends JPanel
        {
            private DiceFace diceBlack;
            private DiceFace diceRed;
            private int diceValueB = 6;
            private int diceValueR = 6;
            private final int PANEL_WIDTH = 400;
            private final int PANEL_HEIGHT = 200;
            
            public DicePanel()
            {
                setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
                diceBlack = new DiceFace(100,75,50,diceValueB,"black");
                diceRed = new DiceFace(100,325,50,diceValueR,"red"); 
            }
            
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                diceBlack.draw(g2);
                diceRed.draw(g2);
            }
            
            public void rollDice()
            {
                Random diceRoll = new Random();
                
                diceValueB = 1 + diceRoll.nextInt(6);
                diceValueR = 1 + diceRoll.nextInt(6);
                
            }
    
        }
        
        public void throwDice()
        {
            dicePanel.rollDice();
            dicePanel.repaint();
        }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: repaint() will not re draw panel

    Moved from New to Java.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem, one that shows how and from where the throwDice() method is invoked.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: repaint() will not re draw panel

    Below shows a stripped down version of the program.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Test 
    {
        public static void main(String[] args)
        {
            JFrame frame = new GameFrame();
            
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Dice Game");
            frame.setVisible(true);
        }
        
        public static class GameFrame extends JFrame
        {
            private JButton roll;
            private ActionListener listener;
            private GameFrame.DicePanel dicePanel;
        
            private static final int FRAME_WDITH = 500;
            private static final int FRAME_HEIGHT = 300;
            public GameFrame()
            {
            dicePanel = new GameFrame.DicePanel();
            add(dicePanel, BorderLayout.CENTER);
            class choiceListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    throwDice();
                }
            }
            listener = new choiceListener();
            createThrowPanel();
            pack();
    
        }
        
        public class DicePanel extends JPanel
        {
            private DiceFace diceBlack;
            private DiceFace diceRed;
            private int diceValueB = 6;
            private int diceValueR = 6;
            private final int PANEL_WIDTH = 500;
            private final int PANEL_HEIGHT = 300;
            
            public DicePanel()
            {
                setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
                diceBlack = new DiceFace(100,75,50,diceValueB,"black");
                diceRed = new DiceFace(100,325,50,diceValueR,"red"); 
            }
            
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                diceBlack.draw(g2);
                diceRed.draw(g2);
                System.out.println("paint" + diceValueB + "  " + diceValueR);
            }
            
            public void rollDice()
            {
                Random diceRoll = new Random();
                
                diceValueB = 1 + diceRoll.nextInt(6);
                diceValueR = 1 + diceRoll.nextInt(6);
                System.out.println("" + diceValueB + "  " + diceValueR);
            }
    
        }
        
        public void throwDice()
        {
            dicePanel.rollDice();
            dicePanel.repaint();
        }
            
        public JPanel createThrowPanel()
        {
            roll = new JButton("Throw Dice");
            
            roll.addActionListener(listener);
            
            JPanel panel = new JPanel();
            panel.add(roll);
            add(panel, BorderLayout.SOUTH);
            return panel;   
        }
        
    }
    }
    When the button roll is pressed it performs rollDice() which creates a random number and should then repaint the JPanel.
    I have added println's and the dice values are changing each time the button is pressed and the same dice values are present in paintcomponent when that prints out.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: repaint() will not re draw panel

    If you still need our help, please re-read the SSCCE link. Your current code does not conform to specifications as I cannot compile it, run it, nor test it.

  5. #5
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: repaint() will not re draw panel

    Forgot to add the diceface class. it should compile with the addition off this.
    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    public class DiceFace 
    {
        private Ellipse2D.Double[] dots = new Ellipse2D.Double[7];
       
        private Rectangle box;
        private int xLeft;
        private int yTop;
        private int side;
        private int diceValue;
        private String color;
        
        public DiceFace(int s, int x, int y, int v, String c)
        {
            side = s;		
    	xLeft = x;		
    	yTop = y;		
    	diceValue = v; 
            color = c;
        }
        
        public void draw(Graphics2D g2)
        {
            diceShape(g2);
            
            g2.setColor(Color.WHITE);
            
            addDots(g2);
        }
        
        public void diceShape(Graphics2D g2)
        {
            box = new Rectangle(xLeft, yTop, side, side);
            makeDots();
            
            if(color.equals("black"))
            {
                g2.setColor(Color.BLACK);
            }
            else if(color.equals("red"))
            {
                g2.setColor(Color.red);
            }
            g2.fill(box);
        }
        
        public void makeDots()
        {
            int w = side/6;   // dot width
            int h = side/6;   // dot height
    
            dots[0] =  new Ellipse2D.Double(xLeft + (2.5 * side)/6, yTop + (2.5 * side)/6, h, w);                                         
            dots[1] = new Ellipse2D.Double(xLeft + (3.75 * side)/6, yTop + (3.75 * side)/6, h, w);                                       
            dots[2] = new Ellipse2D.Double(xLeft + (1.25 * side)/6, yTop + (1.25 * side)/6, h, w);                             
            dots[3] = new Ellipse2D.Double(xLeft + (1.25 * side)/6, yTop + (3.75 * side)/6, h, w);                                        
            dots[4] = new Ellipse2D.Double(xLeft + (3.75 * side)/6, yTop + (1.25 * side)/6, h, w);                                 
            dots[5] =  new Ellipse2D.Double(xLeft + (1.25 * side)/6, yTop + (2.5 * side)/6, h, w);                                         
            dots[6] =  new Ellipse2D.Double(xLeft + (3.75 * side)/6, yTop + (2.5 * side)/6, h, w);
        }
        
        public void addDots(Graphics2D g2)
        {
            if(diceValue==1)
            {
                g2.fill(dots[0]);
            }
            else if(diceValue ==2)
            {
                g2.fill(dots[1]); g2.fill(dots[2]);
            }
            else if(diceValue == 3)
            {
                g2.fill(dots[0]); g2.fill(dots[1]); g2.fill(dots[2]);
            }
            else if(diceValue == 4)
            {
                g2.fill(dots[1]); g2.fill(dots[2]);
                g2.fill(dots[3]); g2.fill(dots[4]);
            }
            else if(diceValue == 5)
            {
                g2.fill(dots[0]); g2.fill(dots[1]);
                g2.fill(dots[2]); g2.fill(dots[3]); g2.fill(dots[4]);
            }
            else if(diceValue == 6)
            {
                g2.fill(dots[1]); g2.fill(dots[2]); g2.fill(dots[3]);
                g2.fill(dots[4]); g2.fill(dots[5]); g2.fill(dots[6]);            
            }
            
        }
          
        
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: repaint() will not re draw panel

    Look carefully in your ActionListener and trace through the code. Then tell me where you think the images should be told to change. (by the way, I see your problem, but now I want to make you think on it and figure it out).

  7. #7
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: repaint() will not re draw panel

    I have also found the problem. I have moved the creation of the dice into paintComponent as i wasn't applying the change in face value to the dice in the above code.
    Fubarable likes this.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: repaint() will not re draw panel

    Good show and exactly right! You were changing the int values held by diceValueB and diceValueR, but weren't applying those changes to the displayed Dice.

  9. #9
    FunkyJustice is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: repaint() will not re draw panel

    Would you mind posting the corrected code? I'm trying to figure out how to fix this myself and cannot, even with the explanation.

  10. #10
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: repaint() will not re draw panel

    I think both myn and Fubarable's last replys explain the solution very clearly. If you look at your code and read this section of my reply " i wasn't applying the change in face value to the dice" i think you will understand your problem and it is an easy fix.

Similar Threads

  1. panel wont update when repaint is called
    By yemista in forum AWT / Swing
    Replies: 3
    Last Post: 10-25-2011, 06:58 PM
  2. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:29 PM
  3. Replies: 11
    Last Post: 11-29-2010, 09:40 PM
  4. Replies: 3
    Last Post: 06-05-2008, 11:10 PM
  5. Repaint panel after open saved serialized array
    By King8654 in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •