Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 06-30-07   #1 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default Java Graphics paint()

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();
    }

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N

Last edited by C-bro : 06-30-07 at 02:26 AM.
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 06-30-07   #2 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 06-30-07   #3 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

Quote:
Originally Posted by rabidgnome229 View Post
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
It's clearing fine and working without flickering, but it's doing so very inefficiently. It's creating a new Graphics object every time the shape redraws. It disposes the original graphic and creates a new one rather than just updating the old one.

This part erases the previous image:

Code:
java.awt.Graphics Gr = this.getGraphics();
        if(Gr != null)
            Gr.dispose();
And this part draws the new location:

Code:
g.setColor(new java.awt.Color(125,0,125));
        g.fillOval(x,y,20,20);
Ideally I would somehow give the existing drawn shape a new coordinate and just repaint it. It's seems, however, that once a shape is drawn all references relating to it are eliminated. Forcing you do dispose all current graphics and draw new ones. Other than getGraphics(), I haven't found a way to gain access to the original drawn shape.

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 06-30-07   #4 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

Test out dispose and make sure its doing what its supposed to be doing. Dispose and immediately repaint to see what you get.
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 06-30-07   #5 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

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.
}
With that method, you just keep creating new objects in order to see the new information. That's how my painting method works right now. It releases all the graphics references and paints new ones. Instead, I'd like something like this:

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.
 }
I was hoping to be able to use the same graphics object the entire time, and just shift it's position, much like you'd use a set method to update an objects properties rather than create a whole new object with the desired properties. I wouldn't be so worried, but I had to do a major re-write on a couple methods because I just kept creating new objects rather than modifying existing ones, and the garbage collection couldn't keep out and I ran into heap overflows.

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 07-01-07   #6 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

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.
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-01-07   #7 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

Quote:
Originally Posted by rabidgnome229 View Post
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.
The reason for 2 graphics objects is as follows:

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.

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 07-06-07   #8 (permalink)
Programmer
 
blade19's Avatar
 
intel nvidia

Join Date: Jan 2007
Posts: 263

Rep: 26 blade19 is acknowledged by some
Unique Rep: 21
Folding Team Rank: 466
Team Name: iSore
Trader Rating: 3
Default

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 pm me if you want; my subscriptions are full.
__________________
*)AE&nw7@#~~;hf!ousb#gtoun^%bnpwn1000s.9.943&<

^string of useless bs...you think?

System: razor
CPU
Conroe E6600
Motherboard
DFI LanParty DK P35-T2RS
Memory
4 x 1GB DDR2 Ballistix 1066MHz Dual Channel -D9MGH
Graphics Card
eVGA 8800 GTS 640MB
Hard Drive
2 x 7200.11 500GB RAID 0 + 2 x 7200.10 500GB
Sound Card
SB Audigy 2 ZS Platinum + Front Panel
Power Supply
Ultra X-Finity 500W
Case
Some stupid diablotek pos with some customizations
CPU cooling
TT Bigwater SE w/ custom blowhole up top
GPU cooling
Stock FTW
OS
Vista Ult + a bunch of VMs :)
Monitor
Acer X241W 24" WS
blade19 is offline I fold for Overclock.net   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 02:19 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.28512 seconds with 8 queries