/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testpaint;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
/**
*
* @author Administrator
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
DrawingSurface m = new DrawingSurface();
m.setSize(500, 500);
m.setLocation(0, 0);
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setVisible(true);
}
}
class DrawingSurface extends JFrame implements MouseMotionListener, MouseListener
{
private PaintingComponent paintingComponent = new PaintingComponent(this);
MyLine _geo = new MyLine(new Point(10,10), new Point(100,100));
//Selektiertes Object
Geometry _selectedGeometry = null;
//Mausposition der Selektion
private Point _lastMouseLocation;
public DrawingSurface() {
getContentPane().add(paintingComponent);
getContentPane().addMouseMotionListener(this);
getContentPane().addMouseListener(this);
}
public void redraw()
{
paintingComponent.repaint();
}
public void mousePressed(MouseEvent e) {
//Bei linkem Mausklick wird das Fenster aktiviert und...
if (this.contains(e.getPoint()) && !e.isMetaDown())
{
if (_geo.Hit(e.getPoint()))
{
_selectedGeometry = _geo;
_lastMouseLocation = e.getPoint();
}
}
}
public void mouseDragged(MouseEvent e) {
if(!e.isMetaDown() && _selectedGeometry != null)
{
_selectedGeometry.Move(e.getPoint().x - _lastMouseLocation.x, e.getPoint().y - _lastMouseLocation.y);
_lastMouseLocation = e.getPoint();
this.redraw();
}
}
public void mouseReleased(MouseEvent e) {
_selectedGeometry = null;
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
class Geometry {
Point _start;
Point _end;
Path2D.Double _path = new Path2D.Double();
public Geometry(Point start, Point end){
_start = start;
_end = end;
}
public void Move(int deltaX, int deltaY)
{
AffineTransform at = new AffineTransform();
at.translate(deltaX, deltaY);
_path.transform(at);
//Setzen der neuen Start- und Endpunkte
_start.x += deltaX;
_start.x += deltaY;
_end.x += deltaX;
_end.y += deltaY;
}
//Methode zum Zeichnen des Pfades und ggf. des Rahmens
public void draw(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.black);
g2d.setStroke(new BasicStroke(1));
g2d.draw(_path);
}
public boolean Hit(Point pt)
{
return false;
}
//Methode zum Hinzufügen der Geometry zum Path
protected void AddToPath()
{
//Default
_path = new Path2D.Double(new Line2D.Double(_start, _end));
}
}
class MyLine extends Geometry{
public MyLine(Point start, Point end){
super(start, end);
AddToPath();
}
@Override
public boolean Hit(Point pt)
{
Line2D.Double myLine = new Line2D.Double(super._start, super._end);
int dist = (int)myLine.ptLineDist(pt);
if(dist < 5)
{
return true;
}
else
{
return false;
}
}
@Override protected void AddToPath()
{
super._path.append(new Line2D.Double(super._start, super._end), true);
}
}
class PaintingComponent extends JComponent
{
private DrawingSurface _surface;
public PaintingComponent(DrawingSurface surface)
{
_surface = surface;
}
public void paintComponent(Graphics g){
super.paintComponents(g);
_surface._geo.draw(g);
}
}