Java Graphics disappears - JFrame

Why does Java Graphics disappear in JFrame?

Page 1 of 1

8 Replies - 7081 Views - Last Post: 05 June 2010 - 07:57 AM Rate Topic: -----

#1 Fromswe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 10

Java Graphics disappears - JFrame

Posted 05 June 2010 - 07:19 AM

Hi, I'm making a Mastermind game in java, right now I'm trying to get the graphics to work. I made the program in Jframe Form. When the user preses a JButton I want to make a circle representing the color choice. However if i press the button again, the graphics disappear. why? here is my draw code:

public void målablå(){
    Graphics g = getGraphics();
     super.paintComponents(g);
    g.setColor(Color.blue);
     g.fillOval(m*30, p*60, 50, 50);
     if (p==4){
         repaint();
     }



målablå is swedish for paintblue :)
m is the number of choices the player has made.
p is the number of rounds.

I'm getting really confused about this, any help apprciated:)

Is This A Good Question/Topic? 0
  • +

Replies To: Java Graphics disappears - JFrame

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Java Graphics disappears - JFrame

Posted 05 June 2010 - 07:22 AM

repaint() call's the frame's paint() function. What do you have in there?
Was This Post Helpful? 0
  • +
  • -

#3 Fromswe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 10

Re: Java Graphics disappears - JFrame

Posted 05 June 2010 - 07:26 AM

hmm not sure actually xD

I'm very new to this, but even if i remove repaint() it still removes the circle if i press the button again.. :(
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Java Graphics disappears - JFrame

Posted 05 June 2010 - 07:29 AM

hmm.. very hard to say without seeing the whole code..
However, some note:
1. better to override the JPanel class, and do the painting inside the paintComponent method there. Then, add that JPanel to a JFrame.
2. no need to have couple of paintColor methods (paintBlue, paintRed...)
instead, you can have an array of Color objects. Then, refer to the indexes of that array to paint. i.e:
Color[] colors = {Color.red, Color.blue, Color.green, Color.yellow};
to paint yellow use: colors[3], to paint red use colors[0].
3. don't call repaint in a method that paints (in paint or paintComponent).
4.if you want to keep track on the circles you painted, you will have to store them in some kind of Collection (List, array...)
because whenever you change the value of 'm' and 'p' and call repaint, you draw a new circle in a new location and the old circle will disappear.
Was This Post Helpful? 2
  • +
  • -

#5 Fromswe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 10

Re: Java Graphics disappears - JFrame

Posted 05 June 2010 - 07:43 AM

View Postjapanir, on 05 June 2010 - 06:29 AM, said:

hmm.. very hard to say without seeing the whole code..
However, some note:
1. better to override the JPanel class, and do the painting inside the paintComponent method there. Then, add that JPanel to a JFrame.
2. no need to have couple of paintColor methods (paintBlue, paintRed...)
instead, you can have an array of Color objects. Then, refer to the indexes of that array to paint. i.e:
Color[] colors = {Color.red, Color.blue, Color.green, Color.yellow};
to paint yellow use: colors[3], to paint red use colors[0].
3. don't call repaint in a method that paints (in paint or paintComponent).
4.if you want to keep track on the circles you painted, you will have to store them in some kind of Collection (List, array...)
because whenever you change the value of 'm' and 'p' and call repaint, you draw a new circle in a new location and the old circle will disappear.


Great thanks!

However the circle also disappear when i minimize the window.. Will storing the circles fix that aswell?
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Java Graphics disappears - JFrame

Posted 05 June 2010 - 07:44 AM

Any change to the size or location of the frame will cause a repaint() so anything that isn't in the current paint() call will be overwritten.
Was This Post Helpful? 0
  • +
  • -

#7 Fromswe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 10

Re: Java Graphics disappears - JFrame

Posted 05 June 2010 - 07:47 AM

View PostKYA, on 05 June 2010 - 06:44 AM, said:

Any change to the size or location of the frame will cause a repaint() so anything that isn't in the current paint() call will be overwritten.


SO how do I prevent this from happening? An example would be awesome, since I'm new it is easier to understand from a context.
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Java Graphics disappears - JFrame

Posted 05 June 2010 - 07:53 AM

Check out some of the code in this thread. Basically, the guy kept a vector/array/collection (whatever you want) of objects to draw and then when paint was called it painted whatever was "in the buffer".
Was This Post Helpful? 2
  • +
  • -

#9 Fromswe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 10

Re: Java Graphics disappears - JFrame

Posted 05 June 2010 - 07:57 AM

okey i will, thaanks :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1