Re: Help with a JPanel
- From: Michael Rauscher <michlmann@xxxxxx>
- Date: Fri, 25 Aug 2006 08:36:42 +0200
Mail987@xxxxxxxxx schrieb:
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?
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
.
- References:
- Help with a JPanel
- From: Mail987
- Help with a JPanel
- Prev by Date: Re: Help with a JPanel
- Next by Date: Re: Rendering CAD/GIS Data on a Map Canvas
- Previous by thread: Re: Help with a JPanel
- Next by thread: Set keyboard focus to Applet when it is loaded
- Index(es):
Relevant Pages
|