Thomas Darimont
Erfahrenes Mitglied
Hallo!
Gruß Tom
Code:
/*
* Created on 29.10.2004
*/
package de.tutorials;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.plaf.basic.BasicGraphicsUtils;
/**
* @author Darimont
*
*/
public class Test39 extends JFrame {
private static final long serialVersionUID = 2868863481341142806L;
private BufferStrategy strategy;
private Point p0, p1;
private Thread runner = new Thread() {
{
setPriority(MIN_PRIORITY);
}
public void run() {
while (true) {
Graphics g = strategy.getDrawGraphics();
g.clearRect(0, 0, 400, 300);
if (p0 != null && p1 != null) {
int xMin = 0, xMax = 0, yMin = 0, yMax = 0;
xMin = Math.min(p0.x, p1.x);
xMax = xMin == p0.x ? p1.x : p0.x;
yMin = Math.min(p0.y, p1.y);
yMax = yMin == p0.y ? p1.y : p0.y;
BasicGraphicsUtils.drawDashedRect(g, xMin, yMin, xMax
- xMin, yMax - yMin);
}
g.dispose();
strategy.show();
try {
sleep(30L);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
};
public Test39() {
super("Test39");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setIgnoreRepaint(true);
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent evt) {
p1 = evt.getPoint();
}
});
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
p0 = evt.getPoint();
}
});
setSize(400, 300);
setResizable(false);
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
}
public static void main(String[] args) {
new Test39().start();
}
private void start() {
runner.start();
}
}
Gruß Tom