Posted: Wed Apr 14, 2010 1:22 am Post subject: Help With Graphics.
Hi, Im wondering if anyone here had some examples of different ways I can use graphics in java, like for web and stand alone programs. I learn best from fiddleing with the files, so please If I could get any working basic examples I would like that alot.
Sponsor Sponsor
DemonWasp
Posted: Wed Apr 14, 2010 4:01 am Post subject: RE:Help With Graphics.
Posted: Wed Apr 14, 2010 7:21 pm Post subject: RE:Help With Graphics.
I just asked for help. you guys dont need to give me sarcasim and be soo rude... I was told by my teacher to ask here.
TheGuardian001
Posted: Wed Apr 14, 2010 7:33 pm Post subject: Re: Help With Graphics.
Well, let's see...
To use Graphics, you will need:
1)an import of java.awt.* (technically you could import the individual classes, but blanket importing's easier, and I'm assuming efficiency is not an issue, since you're just learning them)
2)A container to put it in (Frame, Window, Applet, etc). The easiest way to use these is to have your class extend them.
//Which can be called by calling: publicvoid doStuff(){ //this:
repaint();
}
The repaint method will automatically retrieve the drawable component of your application, and call paint() with that as it's argument.
To actually draw Graphics, you simply use the methods contained in g, within your paint method, IE:
code:
public void paint(Graphics g)
{
g.setColor(Color.GREEN);
g.fillRect(20,20,20,20);
g.clearRect(20,20,20,20);
}
there are tons of methods inside the Graphics class that will allow you to do pretty much whatever you want in terms of Graphics. You can see a complete list of methods here
DemonWasp
Posted: Wed Apr 14, 2010 11:23 pm Post subject: RE:Help With Graphics.
My point was that if you tried googling this yourself, you'd get an answer as fast as Google will return it to you. If you ask on a forum, you're going to have to wait minutes or hours for a reply.
There are a lot of ways to do graphics in Java. Swing is the simplest way (see TheGuardian's post), but you can also use AWT or SWT. If you want 3d graphics, you can use the OpenGL-interfaces LWJGL or JOGL, or an engine that sits on top of those, like jME.
OmniC-Programmer
Posted: Thu Apr 15, 2010 10:50 pm Post subject: Re: Help With Graphics.
//Which can be called by calling: publicvoid doStuff(){ //this:
repaint();
}
The repaint method will automatically retrieve the drawable component of your application, and call paint() with that as it's argument.
How does this work lol. I dont get it :/ calling repaint will call paint? this doesnt make sence to me logically can you help explain this further please?
OmniC-Programmer
Posted: Mon Apr 19, 2010 8:10 pm Post subject: RE:Help With Graphics.
Just updating the thread since it has seemed to be forgotten.
Sponsor Sponsor
TerranceN
Posted: Mon Apr 19, 2010 8:32 pm Post subject: RE:Help With Graphics.
Just so you know, "updating the thread" as you put it, is commonly called bumping.
As for your question, I'm pretty sure it just calls paint() with getGraphics() as the argument, so the method would look something like this:
Java:
// You wouldn't be able to see this code cause its built in to the container void repaint() {
paint(getGraphics());
}
Oh and it may actually be createGraphics(), I have not used Java in awhile.
OmniC-Programmer
Posted: Mon Apr 19, 2010 8:53 pm Post subject: RE:Help With Graphics.
Bumping, yes! thats the word.
So what, is repaint kinda like main?
TheGuardian001
Posted: Mon Apr 19, 2010 8:55 pm Post subject: Re: Help With Graphics.
Calling repaint (as far as I know) essentially just simplifies the process by getting the default graphics context for you. Calling paint and manually specifying your own graphics object / manually specifying the default graphics object will work just as well.
TerranceN
Posted: Mon Apr 19, 2010 8:59 pm Post subject: RE:Help With Graphics.
No, main automatically gets called, and must be created by you for your program to run. repaint on the other hand, must be called by you, and is already created for you (its part of a class you have to inherit to make a window/applet).
andrew.
Posted: Mon Apr 19, 2010 9:04 pm Post subject: RE:Help With Graphics.
Basically, you have a method called paint which draws your stuff. It takes in Graphics. If I am correct, paint automatically runs when your program or applet runs. If you want to run it again to update what's shown on screen, the easiest way is to call a method called repaint(). This method will automatically call paint with the proper parameters and your paint method will run again.
OmniC-Programmer
Posted: Mon Apr 19, 2010 10:15 pm Post subject: RE:Help With Graphics.
Ok, thanks everyone.
Unnamed.t
Posted: Fri May 28, 2010 9:57 pm Post subject: Re: Help With Graphics.
Right so I need a bit of help too and it is directly related to this topic. I have tried to use the repaint method the way it is showed. The problems i'm having is that i want to run the method that has the repaint statement in it in other methods, but i cannot do so because the paint method isn't static. If I try to make the paint method static, it wouldn't work (obviously).
Is there another method that can be used so I can run the graphics in all methods.
P.S. I have also tried using the repaint method directly in my other methods, but unfortunately they are all static aswell.