Results 1 to 10 of 10
Thread: repaint() will not re draw panel
- 10-27-2012, 07:59 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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(); }
- 10-27-2012, 08:10 PM #2
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 10-27-2012, 09:05 PM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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; } } }
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.
-
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.
- 10-27-2012, 09:33 PM #5
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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]); } } }
-
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).
- 10-27-2012, 09:51 PM #7
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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.
-
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.
- 10-28-2012, 09:18 PM #9
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
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-28-2012, 09:58 PM #10
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Similar Threads
-
panel wont update when repaint is called
By yemista in forum AWT / SwingReplies: 3Last Post: 10-25-2011, 07:58 PM -
panel on a panel not visible
By warchieflll in forum Advanced JavaReplies: 2Last Post: 01-29-2011, 09:29 PM -
How to display the black Polygon that I draw on the panel?
By BeijingDuck in forum AWT / SwingReplies: 11Last Post: 11-29-2010, 10:40 PM -
how to pause and repaint in a loop, to draw a mathematical function.
By Abdelhamidem in forum AWT / SwingReplies: 3Last Post: 06-06-2008, 12:10 AM -
Repaint panel after open saved serialized array
By King8654 in forum AWT / SwingReplies: 1Last Post: 04-24-2008, 10:37 AM
Bookmarks