Bunch of <JFrame> and <JPanel> Questions

K

KevinSimonson

I haven't done anything with Swing for a while, and decided I wanted
to brush up my skills, so I thought I'd write a Java program that drew
lines on a <JPanel> on a <JFrame>. Does anybody know how to get ac-
cess to a <JPanel> on a <JFrame>? What the steps are to draw the
lines that show up in the <JPanel> on the <JFrame>?

I think that I want to modify one of the <paint()> or
<paintComponent()> methods of the <JPanel>, and do <drawLine()> calls
with its <Graphics> object. Does anybody know what the difference is
between modifying <paint()> and <paintComponent()> so I can know which
I want to use?

I'd also like to have some code that changes the lines that are drawn
depending on where the user clicks the mouse. If I remember right I
need to make my <JPanel> object implement an interface in order for
that to happen, and the interface will include some methods that get
called when the user clicks the mouse. And is there another interface
that defines the methods that get called when the user _resizes_ the
<JFrame>, so that I can have my application draw the <JPanel> with the
new size? And when the user _moves_ the <JFrame>, so that I can have
my application redraw the <JPanel> in its new location?

Any pointers anybody can give me on this would be greatly appreciated.

Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
J

John B. Matthews

KevinSimonson said:
I haven't done anything with Swing for a while, and decided I wanted
to brush up my skills, so I thought I'd write a Java program that
drew lines on a <JPanel> on a <JFrame>. Does anybody know how to get
ac- cess to a <JPanel> on a <JFrame>? What the steps are to draw the
lines that show up in the <JPanel> on the <JFrame>?

Definitely start with the tutorial:

I think that I want to modify one of the <paint()> or
<paintComponent()> methods of the <JPanel>, and do <drawLine()> calls
with its <Graphics> object. Does anybody know what the difference is
between modifying <paint()> and <paintComponent()> so I can know
which I want to use?

<http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html>
I'd also like to have some code that changes the lines that are drawn
depending on where the user clicks the mouse. If I remember right I
need to make my <JPanel> object implement an interface in order for
that to happen, and the interface will include some methods that get
called when the user clicks the mouse.

Here's a simple object drawing example:

And is there another interface that defines the methods that get
called when the user _resizes_ the <JFrame>, so that I can have my
application draw the <JPanel> with the new size? And when the user
_moves_ the <JFrame>, so that I can have my application redraw the
<JPanel> in its new location?

Look at using a layout manager and component listener:

<http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html>
<http://java.sun.com/docs/books/tutorial/uiswing/events/componentlistener
..html>
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top