/*
* Created on 01.10.2004
*
*/
/**
* @author
*
* TODO
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class PaintLine1 {
static class Line {
public Point from, to;
Line(Point from, Point to) {
this.from = from;
this.to = to;
}
/*
public String toString() {
return from + "=>" + to;
}
*/
}
static class DrawingPanel extends JPanel {
private Stack lines = new Stack();
public DrawingPanel() {
addMouseListener(new MouseAdapter() {
private Point from;
public void mousePressed(MouseEvent e) {
from = new Point(e.getPoint().x, e.getPoint().y);
}
public void mouseReleased(MouseEvent e) {
addLine(new Line(from, new Point(e.getPoint().x, e
.getPoint().y)));
}
});
}
void addLine(Line line) {
lines.add(line);
repaint();
}
// Undo-Funktion
void undo() {
if (lines.size() > 0) {
lines.pop();
repaint();
}
}
// hier folgt die Redo-Funktionalität
void redo() {
//if (lines.size() > 0) {
// lines.pop();
// repaint();
//}
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width, getSize().height);
g.setColor(Color.black);
for (Iterator i = lines.iterator(); i.hasNext();) {
Line line = (Line) i.next();
g.drawLine(line.from.x, line.from.y, line.to.x, line.to.y);
}
}
}
public static void main(String[] argv) throws Exception {
final DrawingPanel panel = new DrawingPanel();
JFrame frame = new JFrame(PaintLine1.class.getName());
frame.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if ((e.getModifiersEx() | e.CTRL_DOWN_MASK) == e
.getModifiersEx()
&& e.getKeyCode() == e.VK_Z) {
panel.undo();
}
if ((e.getModifiersEx() | e.CTRL_DOWN_MASK) == e
.getModifiersEx()
&& e.getKeyCode() == e.VK_Y) {
panel.redo();
}
}
});
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 640, 480);
frame.setVisible(true);
}
}