|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Java Graphics paint()
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
110100001101001111000
|
So I'm working on a board game in Java and I need to have a marker move around the board (an iconified JLabel). I'm using a JLayerdPane and I can get the movement just fine, and I get the shape drawn. The problem I'm having is that the graphics don't update. They just draw new markers. So after 5 moves, there are 5 markers on the board. I don't want to use something like g.dispose() and then keep creating new Graphics objects, but I'm not sure of another way to go about it.
I was thinking of using g.translate(), but to my knowledge, Graphics references are deleted as soon as they're used. Anyone know of an update, or a way I can override repaint() to simply move the current graphic to a new location? This is what I've managed to do. Which works, but it's not very clean. Code:
public void paint(java.awt.Graphics g){
java.awt.Graphics Gr = this.getGraphics();
if(Gr != null)
Gr.dispose();
g.setColor(new java.awt.Color(125,0,125));
g.fillOval(x,y,20,20);
}
public void setCoord(int xloc, int yloc){
x = xloc;
y = yloc;
repaint();
}
__________________
Last edited by C-bro : 06-30-07 at 02:26 AM. |
||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
Stick a double buffer in there. It will eliminate flickering and probably solve your issue. TBH it seems as if it isn't clearing the screen - just drawing more stuff on top of what you have
|
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
110100001101001111000
|
Quote:
This part erases the previous image: Code:
java.awt.Graphics Gr = this.getGraphics();
if(Gr != null)
Gr.dispose();
Code:
g.setColor(new java.awt.Color(125,0,125));
g.fillOval(x,y,20,20);
__________________
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
Test out dispose and make sure its doing what its supposed to be doing. Dispose and immediately repaint to see what you get.
|
|||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||
|
110100001101001111000
|
I should probably clarify. The methods that I have posted do EXACTLY what I want them to do. Here is my concern. Since, in my experience, the garbage collection in Java is rather crappy, it has always been my programming practice to create as few new objects as I can, and just update existing ones with any changes that occur. In other words, say you're going to have a window pop up 1000 times in the duration of a program. Rather than going
Code:
public void update(String msg){
output = new JFrame(msg);
// the previous output no longer has a reference so it's garbage collected whenever.
}
Code:
public void update(String msg){
output.setTitle(msg);
// the same object is used the whole time, so you don't get a bunch of new
// objects waiting to get collected.
}
__________________
|
||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
Why do you have two different graphics objects? this.getGraphics() may be doing a deep copy. I somehow doubt this is the case - but it is a worrying portion of code.
|
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||||
|
110100001101001111000
|
Quote:
On the original painting of the graphics object g, as soon as the paint method finishes, it's to my understanding from a few sources that all references to that graphics object g are severed. The only way to gain access back to that original drawn shape (as far as I know) is to use this.getGraphics(); in order to have some reference to that original drawn shape. I MUST get access to that original g in order to dispose/erase it, since I only want one movement marker to be visible per player. As I mentioned, this functionally works perfectly, but it's not an ideal approach. If you know of some way to gain access to the original graphics object so it can be erased/updated, without calling getGraphics(), it would be incredibly helpful. I've played a few games through completely with no problems with whatsoever, so it may not be a huge concern.
__________________
|
|||||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||
|
Programmer
|
say the background (whatever's behind the 'marker' that is being moved) is black. modify your paint() method assuming this is true:
void paint(Graphics g) { g.setColor(java.awt.Color.BLACK); g.fillOval(x,y,20,20); g.setColor(new java.awt.Color(125,0,125)); g.fillOval(newX,newY,20,20); x = newX; y= newY; } public void setCoord(int xloc, int yloc) { newX = xloc; newY = yloc; repaint(); } if you wanted to use multiple markers, just do a foreach loop int the paint method for every marker in an array of markers. in this case, you should probably have the x, y, newX, and newY variables be state variables local to a Marker object...but this should work for now
__________________
*)AE&nw7@#~~;hf!ousb#gtoun^%bnpwn1000s.9.943&< ^string of useless bs...you think?
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|