Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2006

Tip: Looking for answers? Try searching our database.

Help with a JPanel

Thread view: 
Mail987@gmail.com - 25 Aug 2006 05:24 GMT
Hiya, I think this is a fairly common JPanel related question and would
love any help,

I want to store the width,height,x/y coords of a rectangle in an
object, then draw it onto a JPanel. (I can get this far)
I also want to be able to change the width/height/x/y of the rectangle
then have it redrawn. (Im stuck at this point)

Do I have to call some kind of redraw command on the JPanel to have it
updated? Or do I do it on the rectangle itself? Or some other way
perhas?

Pretty stuck, any sample code would be great

Thanks,
Chris
Andrew Thompson - 25 Aug 2006 05:32 GMT
Mail...@gmail.com wrote:
> Hiya, I think this is a fairly common JPanel related question ...

Note that questions that are 'fairly common' are
also 'frequently answered'.  Did you check the FAQ?

Andrew T.
IchBin - 25 Aug 2006 06:54 GMT
> Mail...@gmail.com wrote:
>> Hiya, I think this is a fairly common JPanel related question ...
[quoted text clipped - 3 lines]
>
> Andrew T.

He may not know where to find it.

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA              http://weconsultants.phpnet.us
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Andrew Thompson - 25 Aug 2006 10:54 GMT
> > Mail...@gmail.com wrote:
> >> Hiya, I think this is a fairly common JPanel related question ...
> >
> > Note that questions that are 'fairly common' are
> > also 'frequently answered'.  Did you check the FAQ?
...
> He may not know where to find it.

That is one possiblility, here are some others..
a)  The OP may know where to find it., and if not,
b)  The OP might ...
 - discover it by search for it
 - ask for directions
 - read the group for a few days, in which time the link
 will most likely appear.

(And 'He', might be 'She'..)

Andrew T.
Michael Rauscher - 25 Aug 2006 07:36 GMT
Mail987@gmail.com schrieb:
> Hiya, I think this is a fairly common JPanel related question and would
> love any help,
[quoted text clipped - 7 lines]
> updated? Or do I do it on the rectangle itself? Or some other way
> perhas?

import java.awt.*;   
import java.awt.event.*;   
import javax.swing.*;

public class Test extends JPanel {
    private Rectangle rect = new Rectangle(0,0,0,0);

    public Dimension getPreferredSize() {
        return new Dimension(600,400);
    }

    public void setRectangle( int x, int y, int w, int h ) {
        rect.x = x;
        rect.y = y;
        rect.width = w;
        rect.height = h;
        repaint();
    }

    public void setRectangle( Rectangle r ) {
        setRectangle( r.x, r.y, r.width, r.height );
    }

    public void paintComponent( Graphics g ) {
        super.paintComponent( g );
        g.drawRect( rect.x, rect.y, rect.width, rect.height );
    }

    public static final void main( String args[] ) {
        final Rectangle r = new Rectangle(10,10,20,20);
        final Test test = new Test();
        test.setRectangle( r );
        test.addMouseListener( new MouseAdapter() {
            public void mouseClicked( MouseEvent e ) {
                r.x = e.getX();
                r.y = e.getY();
                test.setRectangle( r );
            }
        });

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.setContentPane( test );
        frame.pack();
        frame.setVisible( true );
    }
}

Bye
Michael


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.