drawing image on jframe

H

helena

can you tell me the simpliest way to draw animation on frame(or internal
frame).i tryed overriding paint method and using java.awt.Toolkit to get
image,but that doesn't work.I just need a sugestion on how this is
usually done.
i need to transfere applet code to frame.

Than you in advance
 
B

Babu Kalakrishnan

helena said:
can you tell me the simpliest way to draw animation on frame(or internal
frame).i tryed overriding paint method and using java.awt.Toolkit to get
image,but that doesn't work.I just need a sugestion on how this is
usually done.
i need to transfere applet code to frame.

When using Swing, you never paint on the JFrame surface, but on its
contentPane. The easiest way is to create a custom JPanel descendant
class with your painting code in its (overridden) paintComponent method
(_not_ the paint() method). You can then set this panel as the
contentPane of the JFrame by calling its setContentPane method. Make
sure you give the panel a preferredsize as well (depending on what
you're painting on it and how big you want it to be)

When performing animation, it is better to preload all images using a
java.awt.MediaTracker. You can then use a javax.swing.Timer to switch
between these images and trigger a repaint on your panel so that the new
image is painted over the earlier one.

Look at the API docs of the above classes for details.

BK
 
J

John C. Bollinger

helena said:
can you tell me the simpliest way to draw animation on frame(or internal
frame).i tryed overriding paint method and using java.awt.Toolkit to get
image,but that doesn't work.I just need a sugestion on how this is
usually done.
i need to transfere applet code to frame.

For a JFrame, remember that you want to draw on the JFrame's content
pane, not on the JFrame itself. This would be equally applicable to a
JApplet, so it may be that you have less conversion work to do than you
think. There are innumerable ways to do it, but somewhere you must hook
the relevant Graphics context and use it to paint the image. That might
be by assigning as the content pane a custom JPanel subclass with an
overridden paintComponent(Graphics) method; it might be by putting an
appropriate custom component (likewise with a suitable paintComponent
method) _into_ the content pane. There are variations on those basic
approaches. Make sure you understand how the Applet is doing it; with
any luck at all you won't have to change much.


John Bollinger
(e-mail address removed)
 
H

helena

thank you for your answer ,i tried this aproach with jPanel and
overriding paintComponent,but everything works fine exept g.drawImage()
(drawRectangle,drawString is working)..Anyway thank you aou are very
kind to give me a direction
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top