• Post Reply Bookmark Topic Watch Topic
  • New Topic

repaint() method help

 
Merbs Llazart
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello i'm new in java programming and i have trouble understanding the repaint method. i've seen some sample programs but still doesn't get the process.

what if i created a box, and i want that box to move across the screen, how can i do it? i tried looping the x and y inside the paint method with the method repaint on it, but as the output displays, the previous shapes from the previous x and y still shows up. I badly need your help and thank you so much in advance.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot to call super.paint(g) at the start.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried looping the x and y inside the paint method


1. Never create looping code like that in a paint() method.
2. Custom painting is done in the paintComponent() method.
3. Animation is done by using a Swing Timer

Start by reading the Swing tutorial. Read the sections on

1. Performing Custom Painting - this shows you how to change x/y offsets and how to use repaint
2. How to Use Swing Timer - now you can use the Timer to automatically change the x/y offsets.
 
Merbs Llazart
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
i tried looping the x and y inside the paint method


1. Never create looping code like that in a paint() method.
2. Custom painting is done in the paintComponent() method.
3. Animation is done by using a Swing Timer

Start by reading the Swing tutorial. Read the sections on

1. Performing Custom Painting - this shows you how to change x/y offsets and how to use repaint
2. How to Use Swing Timer - now you can use the Timer to automatically change the x/y offsets.


thank you for that advice, but i still don't get it.
can you please give me an example program using JTimer ? I'd be very grateful.

p.s.
i'm looping the x and y in the paintComponent. sorry for the wrong info.
 
Merbs Llazart
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i created a character using paintComponent and i want it to move up and down .. here's the code.




i hope you can help me. thanks again in advance.
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merbs Llazart wrote:
Rob Camick wrote:
i tried looping the x and y inside the paint method


1. Never create looping code like that in a paint() method.
2. Custom painting is done in the paintComponent() method.
3. Animation is done by using a Swing Timer

Start by reading the Swing tutorial. Read the sections on

1. Performing Custom Painting - this shows you how to change x/y offsets and how to use repaint
2. How to Use Swing Timer - now you can use the Timer to automatically change the x/y offsets.


thank you for that advice, but i still don't get it.

A day isn't enough time to go through the Swing tutorials. Ba patient and continue to learn from them.

Merbs Llazart wrote:can you please give me an example program using JTimer ? I'd be very grateful.

1. This is a forum, NotACodeMill
2. The class mentioned was the Swing Timer, javax.swing.Timer. Your asssuming that it would be called JTimer is further evidence that you haven't spent nearly enough time with the tutorials.

Merbs Llazart wrote:p.s.
i'm looping the x and y in the paintComponent. sorry for the wrong info.

You were already advised to not run a loop within a painting method override. Painting methods, including paintComponent(...), are to be used strictly for painting. You have absolutely no control over how often and how many times these methods are invoked.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!