Re: Help with a JPanel



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
.



Relevant Pages

  • Re: Law of Demeter can be supported without eliminating object coupling
    ... private Point upperLeft; ... public int getLeft() ... public void setLeft ... the client of the Rectangle interface can do something like ...
    (comp.object)
  • Re: A few questions about scaling transforms and clipping
    ... It basically draws a rectangle and two lines within it that intersect in the middle with the rendering scaling as the dimensions of the window change. ... public void paintComponent{ ... final double scaleFactorX = this.getWidth/ 100.0; ... final int height = this.getHeight; ...
    (comp.lang.java.gui)
  • Re: No compilation error but not the required output
    ... That's why you see one big rectangle. ... > public class Histogram extends Applet ... > public void paint ... > int x = 20; ...
    (comp.lang.java.gui)
  • please help: star animation
    ... Iam writing a program that displays my star and the star should animate and ... When I try to create the rectangle to be the borders ... public void start ... public boolean mouseDown(Event e, int x, int y) ...
    (comp.lang.java.help)
  • Re: Draw a rectangle keeping aspect ratio of viewport
    ... It's certainly helpful to know how matrices work with respect to transforming between coordinate spaces, but to some extent if you simply look at the docs for AffineTransform and see what methods it offers, that can help you understand what sorts of things a transform can do for you. ... There is still a single problem with the code I posted, if you select the x3 option it does not centre the rectangle within he scrollbar viewport, the rectangle I am drawing is supposed to be representative to an area of the image I select with the selection rectangle. ... private BufferedImage _image; ... public void setImage ...
    (comp.lang.java.programmer)