Welcome to Gaia! ::

Reply
Java Graphics

Quick Reply

Enter both words below, separated by a space:

Can't read the text? Click here

Submit

Mag9102

PostPosted: Sun Apr 16, 2006 2:16 pm


I was wondering if anyone knows if it is possible to paint on a frame from multiple classes that are working together. I am trying to find a way to shorten the huge amount of code in my paint() function. If you have any ideas please let me know!!!
Thanks
PostPosted: Mon Apr 17, 2006 1:12 pm


^_^... I don't use my paint() function that way...
I store my graphics and call repaint() every little bit and only force paint to display the graphics...
otherwise it messes uo with the fact that games go frame by frame...


and you could always create a new object or two and call functions there to do the long codework...

nadir-seen-fire


Mag9102

PostPosted: Tue Apr 18, 2006 5:51 pm


I don't quite follow what you are saying. How can you create an object to do all of the code for the paint()? Do you mean by creating the shapes first? Please clarify what you are trying to say.
Thanks for the advice though
PostPosted: Tue Apr 18, 2006 6:56 pm


Mag9102
I don't quite follow what you are saying. How can you create an object to do all of the code for the paint()? Do you mean by creating the shapes first? Please clarify what you are trying to say.
Thanks for the advice though

XP... I mean simplify it... or outsource the code to another class... for example...

Ignore the fact that circle(), square(), and line() don't exist... I'm just demonstrating the idea of moving things...
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;

/*Draw 4 shapes of different types*/
g2d.circle(x,y,50,50)
g2d.square(x+50,y,50,50)
g2d.line(x,y+50,50,50);
}[code]

provided there's a lot more sections of code... they can be broken down to other functions in a new class...

[code]public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;

/*Draw the same shapes*/
object2.drawShapes(g2d,x,y);
}

public class object2 {

public static void drawShapes(g2d,x,y) {
g2d.circle(x,y,50,50)
g2d.square(x+50,y,50,50)
g2d.line(x,y+50,50,50);
}[/code]

it looks useless from the example... but if you have a dozen different actions you can simplify the paint command by createing seperate functions for each group in a static class and calling those from the paint command...



XP... but I still prefer the idea of having a seperate thread draw the image... update that into the saved area... then call repaint() telling paint to output the BufferedImage....
It's much better for games when you have strict framerates seperate from how fast the paint command executes... cause putting frame info inside of a paint command is to unpredictable to be stable... that's why you use steps, sleeps, and threads...

nadir-seen-fire


Mag9102

PostPosted: Wed Apr 19, 2006 3:13 pm


I think I follow you. Basically you are saying that I can create a function for each of the shapes that will change the shape object as needed?Then display it from the paint(). If you use a draw shapes function does that do the work that paint() can do? I thought that you can only paint with the paint() function. If you can use a different function do you have to include a graphics parameter for it to work? I am fairly new to java programming, so sorry if I seem a little clueless...
PostPosted: Sat Apr 22, 2006 7:13 am


Nevermind, I figured out how to use other classes to paint. I didn't know you could do that.
Thanks for the help

Mag9102

Reply


 
Manage Your Items
Other Stuff
Get GCash
Offers
Get Items
More Items
Where Everyone Hangs Out
Other Community Areas
Virtual Spaces
Fun Stuff
Gaia's Games
Mini-Games
Play with GCash
Play with Platinum