Sign in to follow this  
hazle

Drawing in java applications

Recommended Posts

hazle    100
i cna't figure out how to draw on to my frame for my game. i read in a book that you use a sub class of the JPanel class but if you do that your stuck with a static image that you can't change. in order to have a fun game you haved to be able to see things move and so far nothing moves becuase of this. thanks in advaned.

Share this post


Link to post
Share on other sites
Zahlman    1682
Generally speaking, to get Java GUI widgets to refresh their appearance, you need to:

(a) tell them how to draw the current appearance, by implementing void paint(Graphics g). The Graphics object is one that is created internally by the GUI framework, and represents the displayable area of the widget. You should make method calls on it, according to the data in your object, so as to draw a representation of the object.

(b) tell it when to redraw, by calling repaint() on the widget when needed. The repaint() method is implemented in some superclass of the widget, making use of internal stuff that you don't have access to: but basically, it arranges to create a Graphics object and call your paint() method with it, at some later time.

Basically, your paint() is a callback method; the setup is similar to how OpenGL works (with its registration of functions) or Java threading (where start() arranges for run() to execute in a new thread).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
your stuck with a static image that you can't change


When you use any gui lib to write a windowed application you are using the 'event driven' paradigm. This means the code only executes when an event happens, like a key press, mouse click or timer signal.

There are several ways to achieve animation in java. In java you can use the Timer class to signal an even. You can create a thread to run constantly and place your main loop in the run() method. You can use a buffer strategy.

Google should lead you to many resources concerning this topic.

Share this post


Link to post
Share on other sites
hazle    100
i don't need to know about wigets how do i draw on the the frame.when i search google i only find stuff about applets.i all ready know how to do this whith applets so the info is useless

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this