Fixed
This commit is contained in:
@@ -48,4 +48,42 @@ public class DrawObject {
|
||||
{
|
||||
selected = b;
|
||||
}
|
||||
|
||||
public Point2D getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(Point2D position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public double getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public void setRotation(double rotation) {
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public double getScale() {
|
||||
return scale;
|
||||
}
|
||||
|
||||
public void setScale(double scale) {
|
||||
this.scale = scale;
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(Image image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public boolean isSelected() {
|
||||
return selected;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+76
-22
@@ -20,6 +20,7 @@ import javax.swing.JPanel;
|
||||
|
||||
import Listneners.Mouse;
|
||||
import Listneners.MouseMotion;
|
||||
import Listneners.MouseWheel;
|
||||
import Objects.Podium;
|
||||
|
||||
public class Panel extends JPanel {
|
||||
@@ -48,32 +49,20 @@ public class Panel extends JPanel {
|
||||
objects.add(new Podium(new Point2D.Double(500, 100)));
|
||||
objects.add(new Podium(new Point2D.Double(300, 400)));
|
||||
|
||||
addMouseListener(new Mouse());
|
||||
addMouseListener(new Mouse(this));
|
||||
|
||||
addMouseMotionListener(new MouseMotion());
|
||||
addMouseMotionListener(new MouseMotion(this));
|
||||
|
||||
addMouseWheelListener(new MouseWheelListener() {
|
||||
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||
Point2D clickPoint = getClickPoint(e.getPoint());
|
||||
for(DrawObject o : objects)
|
||||
{
|
||||
if(o.contains(clickPoint))
|
||||
{
|
||||
o.scale *= 1 + (e.getPreciseWheelRotation()/10.0);
|
||||
repaint();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// cameraPoint
|
||||
cameraScale *= 1 - (e.getPreciseWheelRotation()/10.0);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
addMouseWheelListener(new MouseWheel(this));
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void add(DrawObject dragObject)
|
||||
{
|
||||
objects.add(dragObject);
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
@@ -99,7 +88,7 @@ public class Panel extends JPanel {
|
||||
g2.setTransform(oldTransform);
|
||||
}
|
||||
|
||||
private AffineTransform getCamera() {
|
||||
public AffineTransform getCamera() {
|
||||
AffineTransform tx = new AffineTransform();
|
||||
tx.translate(-cameraPoint.getX() + getWidth()/2, -cameraPoint.getY() + getHeight()/2);
|
||||
tx.scale(cameraScale, cameraScale);
|
||||
@@ -107,7 +96,7 @@ public class Panel extends JPanel {
|
||||
return tx;
|
||||
}
|
||||
|
||||
private Point2D getClickPoint(Point point) {
|
||||
public Point2D getClickPoint(Point point) {
|
||||
try {
|
||||
return getCamera().inverseTransform(point, null);
|
||||
} catch (NoninvertibleTransformException e1) {
|
||||
@@ -115,6 +104,71 @@ public class Panel extends JPanel {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public void setBackground(BufferedImage background) {
|
||||
this.background = background;
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<DrawObject> getObjects() {
|
||||
return objects;
|
||||
}
|
||||
|
||||
|
||||
public void setObjects(ArrayList<DrawObject> objects) {
|
||||
this.objects = objects;
|
||||
}
|
||||
|
||||
|
||||
public DrawObject getDragObject() {
|
||||
return dragObject;
|
||||
}
|
||||
|
||||
|
||||
public void setDragObject(DrawObject dragObject) {
|
||||
this.dragObject = dragObject;
|
||||
}
|
||||
|
||||
|
||||
public Point2D getCameraPoint() {
|
||||
return cameraPoint;
|
||||
}
|
||||
|
||||
|
||||
public void setCameraPoint(Point2D cameraPoint) {
|
||||
this.cameraPoint = cameraPoint;
|
||||
}
|
||||
|
||||
|
||||
public float getCameraScale() {
|
||||
return cameraScale;
|
||||
}
|
||||
|
||||
|
||||
public void setCameraScale(float cameraScale) {
|
||||
this.cameraScale = cameraScale;
|
||||
}
|
||||
|
||||
|
||||
public Point2D getLastClickPosition() {
|
||||
return lastClickPosition;
|
||||
}
|
||||
|
||||
|
||||
public void setLastClickPosition(Point2D lastClickPosition) {
|
||||
this.lastClickPosition = lastClickPosition;
|
||||
}
|
||||
|
||||
|
||||
public Point getLastMousePosition() {
|
||||
return lastMousePosition;
|
||||
}
|
||||
|
||||
|
||||
public void setLastMousePosition(Point lastMousePosition) {
|
||||
this.lastMousePosition = lastMousePosition;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+18
-10
@@ -7,30 +7,38 @@ import java.awt.geom.Point2D;
|
||||
import Applicatie.DrawObject;
|
||||
import Objects.Podium;
|
||||
import Objects.Toilet;
|
||||
import Applicatie.Panel;
|
||||
|
||||
public class Mouse extends MouseAdapter {
|
||||
public void mousePressed(MouseEvent e) {
|
||||
Point2D clickPoint = getClickPoint(e.getPoint());
|
||||
lastClickPosition = clickPoint;
|
||||
lastMousePosition = e.getPoint();
|
||||
private Panel panel;
|
||||
|
||||
public Mouse(Panel panel)
|
||||
{
|
||||
this.panel = panel;
|
||||
}
|
||||
|
||||
public void mousePressed(MouseEvent e) {
|
||||
Point2D clickPoint = panel.getClickPoint(e.getPoint());
|
||||
panel.setLastClickPosition(clickPoint);
|
||||
panel.setLastMousePosition(e.getPoint());
|
||||
if(e.getX() < 200)
|
||||
{
|
||||
if(e.getY() < 300)
|
||||
dragObject = new Podium(clickPoint);
|
||||
panel.setDragObject(new Podium(clickPoint));
|
||||
else
|
||||
dragObject = new Toilet(clickPoint);
|
||||
objects.add(dragObject);
|
||||
panel.setDragObject(new Toilet(clickPoint));
|
||||
panel.add(panel.getDragObject());
|
||||
}
|
||||
else
|
||||
{
|
||||
for(DrawObject o : objects)
|
||||
for(DrawObject o : panel.getObjects())
|
||||
if(o.contains(clickPoint))
|
||||
dragObject = o;
|
||||
panel.setDragObject(o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
dragObject = null;
|
||||
panel.setDragObject(null);
|
||||
}
|
||||
}
|
||||
|
||||
+31
-15
@@ -1,33 +1,49 @@
|
||||
package Listneners;
|
||||
import Applicatie.Panel;
|
||||
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseMotionAdapter;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class MouseMotion extends MouseMotionAdapter {
|
||||
private Panel panel;
|
||||
|
||||
public MouseMotion(Panel panel)
|
||||
{
|
||||
this.panel = panel;
|
||||
}
|
||||
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
Point2D clickPoint = getClickPoint(e.getPoint());
|
||||
if(dragObject != null)
|
||||
Point2D clickPoint = panel.getClickPoint(e.getPoint());
|
||||
if(panel.getDragObject() != null)
|
||||
{
|
||||
if(SwingUtilities.isLeftMouseButton(e))
|
||||
dragObject.position = new Point2D.Double(
|
||||
dragObject.position.getX() - (lastClickPosition.getX() - clickPoint.getX()),
|
||||
dragObject.position.getY() - (lastClickPosition.getY() - clickPoint.getY()));
|
||||
if(SwingUtilities.isLeftMouseButton(e)){
|
||||
|
||||
|
||||
panel.getDragObject().setPosition(new Point2D.Double(
|
||||
panel.getDragObject().getPosition().getX() - (panel.getLastClickPosition().getX() - clickPoint.getX()),
|
||||
panel.getDragObject().getPosition().getY() - (panel.getLastClickPosition().getY() - clickPoint.getY())));
|
||||
}
|
||||
else
|
||||
dragObject.rotation += (lastClickPosition.getX() - clickPoint.getX());
|
||||
repaint();
|
||||
{
|
||||
|
||||
double tempval = panel.getDragObject().getRotation() + (panel.getLastClickPosition().getX() - clickPoint.getX());
|
||||
panel.getDragObject().setRotation(tempval);
|
||||
}
|
||||
panel.repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
cameraPoint = new Point2D.Double(
|
||||
cameraPoint.getX() + (lastMousePosition.getX() - e.getX()),
|
||||
cameraPoint.getY() + (lastMousePosition.getY() - e.getY())
|
||||
);
|
||||
repaint();
|
||||
panel.setCameraPoint(new Point2D.Double(
|
||||
panel.getCameraPoint().getX() + (panel.getLastMousePosition().getX() - e.getX()),
|
||||
panel.getCameraPoint().getY() + (panel.getLastMousePosition().getY() - e.getY())
|
||||
));
|
||||
panel.repaint();
|
||||
}
|
||||
lastMousePosition = e.getPoint();
|
||||
lastClickPosition = clickPoint;
|
||||
panel.setLastMousePosition(e.getPoint());
|
||||
panel.setLastClickPosition(clickPoint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,36 @@
|
||||
package Listneners;
|
||||
|
||||
public class MouseWheel extends MouseWheelListener {
|
||||
//??
|
||||
import java.awt.event.MouseWheelEvent;
|
||||
import java.awt.event.MouseWheelListener;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
import Applicatie.DrawObject;
|
||||
import Applicatie.Panel;
|
||||
|
||||
public class MouseWheel implements MouseWheelListener {
|
||||
private Panel panel;
|
||||
|
||||
public MouseWheel(Panel panel)
|
||||
{
|
||||
this.panel = panel;
|
||||
}
|
||||
|
||||
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||
Point2D clickPoint = panel.getClickPoint(e.getPoint());
|
||||
for(DrawObject o : panel.getObjects())
|
||||
{
|
||||
if(o.contains(clickPoint))
|
||||
{
|
||||
double scale = o.getScale()* 1 + (e.getPreciseWheelRotation()/10.0);
|
||||
o.setScale(scale);
|
||||
panel.repaint();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// cameraPoint
|
||||
double cameraScale = panel.getCameraScale() * 1 - (e.getPreciseWheelRotation()/10);
|
||||
panel.setCameraScale((float) cameraScale);
|
||||
panel.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user