Skip to content

Instantly share code, notes, and snippets.

@faust45
Created January 5, 2010 01:58
Show Gist options
  • Save faust45/269071 to your computer and use it in GitHub Desktop.
Save faust45/269071 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
public class app {
public static void main(String [] args) {
Window w = new Window();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.show();
}
public static void out(Object obj) {
System.out.println(obj);
}
}
class Window extends JFrame {
public Window() {
setSize(WIDTH, HEIGHT);
Point coord = new Point(50, 50);
XRectangle rect = new XRectangle(coord);
Container cont = getContentPane();
cont.add(rect);
}
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
}
class XRectangle extends Component implements MouseMotionListener {
public XRectangle(Point coord) {
super();
this.coord = coord;
rect = new Rectangle2D.Double();
addMouseMotionListener(this);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
setBounds(coord.x, coord.y, width + 1, height + 1);
rect.setRect(rectX, rectY, width, height);
g2.draw(rect);
app.out("paint rec");
}
public void mouseDragged(MouseEvent e) {
app.out("Mouse Dragged");
width = e.getX();
height = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {
}
private Point coord;
private int width = 150, height = 150;
private Rectangle2D rect;
private int rectX = 0, rectY = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment