Updated to new Johan version
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
package Applicatie;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
@@ -38,9 +39,9 @@ public class DrawObject {
|
||||
return tx;
|
||||
}
|
||||
|
||||
public boolean contains(Point point) {
|
||||
public boolean contains(Point2D clickPoint) {
|
||||
Shape shape = new Rectangle2D.Double(0,0,image.getWidth(null), image.getHeight(null));
|
||||
return getTransform().createTransformedShape(shape).contains(point);
|
||||
return getTransform().createTransformedShape(shape).contains(clickPoint);
|
||||
}
|
||||
|
||||
public void setSelected(boolean b)
|
||||
@@ -1,8 +1,10 @@
|
||||
package Applicatie;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Window();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
package Applicatie;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package Applicatie;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.TexturePaint;
|
||||
import java.awt.event.MouseWheelEvent;
|
||||
import java.awt.event.MouseWheelListener;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
import java.awt.geom.NoninvertibleTransformException;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import Listneners.Mouse;
|
||||
import Listneners.MouseMotion;
|
||||
import Objects.Podium;
|
||||
|
||||
public class Panel extends JPanel {
|
||||
|
||||
BufferedImage background;
|
||||
|
||||
ArrayList<DrawObject> objects = new ArrayList<>();
|
||||
DrawObject dragObject = null;
|
||||
|
||||
Point2D cameraPoint = new Point2D.Double(getWidth()/2,getHeight()/2);
|
||||
float cameraScale = 1;
|
||||
|
||||
|
||||
Point2D lastClickPosition;
|
||||
Point lastMousePosition;
|
||||
|
||||
Panel()
|
||||
{
|
||||
try {
|
||||
background = ImageIO.read(new File("images/grass.jpg"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
objects.add(new Podium(new Point2D.Double(100, 100)));
|
||||
objects.add(new Podium(new Point2D.Double(500, 100)));
|
||||
objects.add(new Podium(new Point2D.Double(300, 400)));
|
||||
|
||||
addMouseListener(new Mouse());
|
||||
|
||||
addMouseMotionListener(new MouseMotion());
|
||||
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
g2.setClip(new Rectangle2D.Double(0,0, 200, getHeight()));
|
||||
|
||||
g2.fill(new Ellipse2D.Double(0,0,300,300));
|
||||
|
||||
|
||||
g2.setClip(new Rectangle2D.Double(200,0, getWidth()-200, getHeight()));
|
||||
AffineTransform oldTransform = g2.getTransform();
|
||||
g2.setTransform(getCamera());
|
||||
|
||||
TexturePaint p = new TexturePaint(background, new Rectangle2D.Double(0, 0, 100, 100));
|
||||
g2.setPaint(p);
|
||||
g2.fill(new Rectangle2D.Double(0,0,1920,1080));
|
||||
|
||||
for(DrawObject o : objects)
|
||||
o.draw(g2);
|
||||
|
||||
|
||||
g2.setClip(null);
|
||||
g2.setTransform(oldTransform);
|
||||
}
|
||||
|
||||
private AffineTransform getCamera() {
|
||||
AffineTransform tx = new AffineTransform();
|
||||
tx.translate(-cameraPoint.getX() + getWidth()/2, -cameraPoint.getY() + getHeight()/2);
|
||||
tx.scale(cameraScale, cameraScale);
|
||||
|
||||
return tx;
|
||||
}
|
||||
|
||||
private Point2D getClickPoint(Point point) {
|
||||
try {
|
||||
return getCamera().inverseTransform(point, null);
|
||||
} catch (NoninvertibleTransformException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
package Applicatie;
|
||||
import java.awt.BorderLayout;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
@@ -16,14 +17,8 @@ public class Window extends JFrame {
|
||||
|
||||
//CONTENT PANELS
|
||||
JPanel contentPanel = new JPanel(new BorderLayout());
|
||||
FestivalPanel fp = new FestivalPanel();
|
||||
TopBar tb = new TopBar(fp);
|
||||
SideBar sb = new SideBar(fp);
|
||||
BottomBar bb = new BottomBar(fp);
|
||||
Panel fp = new Panel();
|
||||
contentPanel.add(fp, BorderLayout.CENTER);
|
||||
contentPanel.add(tb, BorderLayout.NORTH);
|
||||
contentPanel.add(sb, BorderLayout.EAST);
|
||||
contentPanel.add(bb, BorderLayout.SOUTH);
|
||||
|
||||
setContentPane(contentPanel);
|
||||
//END CONTENT PANELS
|
||||
@@ -1,20 +0,0 @@
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
public class BottomBar extends JPanel {
|
||||
|
||||
public BottomBar(FestivalPanel fp)
|
||||
{
|
||||
super();
|
||||
setPreferredSize(new Dimension(0, 50));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.TexturePaint;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseMotionAdapter;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.awt.geom.RectangularShape;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
|
||||
public class FestivalPanel extends JPanel {
|
||||
|
||||
BufferedImage background;
|
||||
ArrayList<DrawObject> objects = new ArrayList<>();
|
||||
DrawObject dragObject = null;
|
||||
DrawObject selectedObject = null;
|
||||
Point2D dragOffset;
|
||||
|
||||
FestivalPanel()
|
||||
{
|
||||
|
||||
try {
|
||||
background = ImageIO.read(new File("images/grass.jpg"));
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
addMouseListener(new MouseAdapter()
|
||||
{
|
||||
public void mousePressed(MouseEvent e) {
|
||||
for(DrawObject o : objects){
|
||||
o.setSelected(false);
|
||||
if(o.contains(e.getPoint()))
|
||||
{
|
||||
dragObject = o;
|
||||
selectedObject = o;
|
||||
selectedObject.setSelected(true);
|
||||
dragOffset = new Point2D.Double(e.getX() - o.position.getX(), e.getY() - o.position.getY());
|
||||
}
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
dragObject = null;
|
||||
dragOffset = null;
|
||||
}
|
||||
});
|
||||
|
||||
addMouseMotionListener(new MouseMotionAdapter() {
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
if(dragObject != null)
|
||||
{
|
||||
if(SwingUtilities.isLeftMouseButton(e))
|
||||
dragObject.position = new Point2D.Double(e.getX() - dragOffset.getX(), e.getY() - dragOffset.getY());
|
||||
else
|
||||
dragObject.rotation = e.getX();
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
|
||||
TexturePaint p = new TexturePaint(background, new Rectangle2D.Double(0, 0, 100, 100));
|
||||
g2.setPaint(p);
|
||||
g2.fill(new Rectangle2D.Double(0,0,getWidth(), getHeight()));
|
||||
|
||||
for(DrawObject o : objects)
|
||||
o.draw(g2);
|
||||
|
||||
}
|
||||
|
||||
public void add(DrawObject o)
|
||||
{
|
||||
objects.add(o);
|
||||
repaint();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package Listneners;
|
||||
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
import Applicatie.DrawObject;
|
||||
import Objects.Podium;
|
||||
import Objects.Toilet;
|
||||
|
||||
public class Mouse extends MouseAdapter {
|
||||
public void mousePressed(MouseEvent e) {
|
||||
Point2D clickPoint = getClickPoint(e.getPoint());
|
||||
lastClickPosition = clickPoint;
|
||||
lastMousePosition = e.getPoint();
|
||||
if(e.getX() < 200)
|
||||
{
|
||||
if(e.getY() < 300)
|
||||
dragObject = new Podium(clickPoint);
|
||||
else
|
||||
dragObject = new Toilet(clickPoint);
|
||||
objects.add(dragObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(DrawObject o : objects)
|
||||
if(o.contains(clickPoint))
|
||||
dragObject = o;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
dragObject = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package Listneners;
|
||||
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseMotionAdapter;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class MouseMotion extends MouseMotionAdapter {
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
Point2D clickPoint = getClickPoint(e.getPoint());
|
||||
if(dragObject != null)
|
||||
{
|
||||
if(SwingUtilities.isLeftMouseButton(e))
|
||||
dragObject.position = new Point2D.Double(
|
||||
dragObject.position.getX() - (lastClickPosition.getX() - clickPoint.getX()),
|
||||
dragObject.position.getY() - (lastClickPosition.getY() - clickPoint.getY()));
|
||||
else
|
||||
dragObject.rotation += (lastClickPosition.getX() - clickPoint.getX());
|
||||
repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
cameraPoint = new Point2D.Double(
|
||||
cameraPoint.getX() + (lastMousePosition.getX() - e.getX()),
|
||||
cameraPoint.getY() + (lastMousePosition.getY() - e.getY())
|
||||
);
|
||||
repaint();
|
||||
}
|
||||
lastMousePosition = e.getPoint();
|
||||
lastClickPosition = clickPoint;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package Listneners;
|
||||
|
||||
public class MouseWheel extends MouseWheelListener {
|
||||
//??
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package Objects;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
import Applicatie.DrawObject;
|
||||
|
||||
|
||||
public class Podium extends DrawObject {
|
||||
|
||||
public Podium(Point2D position) {
|
||||
super("images/stage3.png", position);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package Objects;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
import Applicatie.DrawObject;
|
||||
|
||||
|
||||
public class Toilet extends DrawObject {
|
||||
|
||||
public Toilet(Point2D position) {
|
||||
super("images/toilet.png", position);
|
||||
}
|
||||
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Image;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
|
||||
public class Podium extends DrawObject {
|
||||
|
||||
Image selection = null;
|
||||
|
||||
public Podium(int num, Point2D position) {
|
||||
super("images/stage" + num + ".png", position);
|
||||
try {
|
||||
selection = ImageIO.read(new File("images/selection.png"));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g)
|
||||
{
|
||||
super.draw(g);
|
||||
if(selected)
|
||||
{
|
||||
//Rectangle2D rect = new Rectangle2D.Double(0,0,super.image.getWidth(null), super.image.getHeight(null));
|
||||
//Shape sh = super.getTransform().createTransformedShape(rect);
|
||||
//g.setPaint(new Color(0,0,0));
|
||||
//g.draw(sh);
|
||||
g.drawImage(selection, super.getTransform(), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
public class SideBar extends JPanel {
|
||||
|
||||
public SideBar(FestivalPanel fp)
|
||||
{
|
||||
super();
|
||||
setPreferredSize(new Dimension(200, 0));
|
||||
|
||||
}
|
||||
}
|
||||
-55
@@ -1,55 +0,0 @@
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
public class TopBar extends JPanel {
|
||||
|
||||
public TopBar(FestivalPanel fp)
|
||||
{
|
||||
super(new GridLayout(1,4));
|
||||
setPreferredSize(new Dimension(0, 150));
|
||||
|
||||
JButton addStage0 = new JButton(new ImageIcon("images/stage0.png"));
|
||||
addStage0.addActionListener(new ActionListener() {
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
fp.add(new Podium(0, new Point2D.Double(0,0)));
|
||||
}
|
||||
});
|
||||
add(addStage0);
|
||||
|
||||
JButton addStage1 = new JButton(new ImageIcon("images/stage1.png"));
|
||||
addStage1.addActionListener(new ActionListener() {
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
fp.add(new Podium(1, new Point2D.Double(0,0)));
|
||||
}
|
||||
});
|
||||
add(addStage1);
|
||||
|
||||
JButton addStage2 = new JButton(new ImageIcon("images/stage2.png"));
|
||||
addStage2.addActionListener(new ActionListener() {
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
fp.add(new Podium(2, new Point2D.Double(0,0)));
|
||||
}
|
||||
});
|
||||
add(addStage2);
|
||||
|
||||
JButton addStage3 = new JButton(new ImageIcon("images/stage3.png"));
|
||||
addStage3.addActionListener(new ActionListener() {
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
fp.add(new Podium(3, new Point2D.Double(0,0)));
|
||||
}
|
||||
});
|
||||
add(addStage3);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
Reference in New Issue
Block a user