This commit is contained in:
unknown
2015-03-24 13:47:45 +01:00
parent ca90d98d9a
commit b9d42cd4eb
12 changed files with 308 additions and 110 deletions
+5 -2
View File
@@ -1,4 +1,5 @@
package Agenda;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
@@ -137,6 +138,7 @@ public class Agenda implements Serializable {
public void fillAgenda(File file) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(file);
@@ -191,6 +193,7 @@ public class Agenda implements Serializable {
}
public void saveAgenda(File file) {
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
@@ -200,7 +203,7 @@ public class Agenda implements Serializable {
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void clearAgenda()
{
@@ -215,4 +218,4 @@ public class Agenda implements Serializable {
fillStages();
fillArtists();
}
}
}
+2
View File
@@ -1,4 +1,6 @@
package Agenda;
import java.io.Serializable;
import javax.swing.ImageIcon;
+22 -1
View File
@@ -1,7 +1,8 @@
package Agenda;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
@@ -13,6 +14,10 @@ public class Event implements Serializable {
private String eventName;
private GregorianCalendar startDate;
private GregorianCalendar endDate;
private int startHour;
private int startMinute;
private int stopHour;
private int stopMinute;
private Artist artist;
private Stage stage;
private String description;
@@ -27,6 +32,10 @@ public class Event implements Serializable {
startDay, startHour, startMinute);
this.endDate = new GregorianCalendar(endYear, endMonth - 1, endDay,
endHour, endMinute);
this.startHour = startHour;
this.startMinute = startMinute;
this.stopHour = stopHour;
this.stopMinute = stopMinute;
this.stage = stage;
this.artist = artist;
this.description = description;
@@ -44,6 +53,18 @@ public class Event implements Serializable {
this.description = description;
this.expectedPopularity = expectedPopularity;
}
public int getStart(){
return (startHour * 100) + startMinute;
}
public int getStop(){
return (stopHour * 100) + stopMinute;
}
public int getStopHour(){
return this.stopHour;
}
public String getEventName() {
return this.eventName;
+2
View File
@@ -1,4 +1,6 @@
package Agenda;
import java.io.Serializable;
public class Stage implements Serializable {
+32
View File
@@ -0,0 +1,32 @@
package Applicatie;
import java.awt.geom.Point2D;
public class Action {
private Point2D position;
private int starttime;
private int duration;
public Action(Point2D position, int starttime, int duration){
this.position = position;
this.starttime = starttime;
this.duration = duration;
}
public Point2D getPosition(){
return position;
}
public int getStartTime(){
return starttime;
}
public int getStoptime(){
return (starttime + duration);
}
public int getDuration(){
return duration;
}
}
+25 -3
View File
@@ -68,9 +68,7 @@ public class DrawObject implements Serializable{
g.fill(bottomRightCorner); //bottomright
g.setColor(Color.RED);
rotateDot = new Ellipse2D.Double((rektAngle.getX() + (rektAngle.getWidth()/2))-8,rektAngle.getY()-8,15,15);
g.fill(rotateDot);
g.fill(rotateDot);
}
}
@@ -208,4 +206,28 @@ public class DrawObject implements Serializable{
rektAngle = rectangle;
}
public int getX(){
return (int) position.getX();
}
public int getY(){
return (int) position.getY();
}
public int getEndX(){
Image image = new ImageIcon(filename).getImage();
//return (int) (position.getX() + (image.getWidth(null) * scale));
return (int) (position.getX() + image.getWidth(null));
}
public int getEndY(){
Image image = new ImageIcon(filename).getImage();
//return (int) (position.getY() + (image.getHeight(null) * scale));
return (int) (position.getY() + image.getHeight(null));
}
public String getFileName(){
return filename;
}
}
+1
View File
@@ -1,3 +1,4 @@
package Applicatie;
public class Main {
+10 -1
View File
@@ -33,14 +33,23 @@ public class MenuBar extends JMenuBar {
}
});
JMenuItem load = new JMenuItem("Load");
JMenuItem load = new JMenuItem("Load terrain");
load.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SaveLoad.load(w.fp);
}
});
JMenuItem loadagenda = new JMenuItem("Load agenda");
loadagenda.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
w.fp.agenda.loadAgenda();
w.fp.addVisitors();
}
});
file.add(load);
file.add(loadagenda);
file.add(save);
file.add(exit);
+36 -2
View File
@@ -4,6 +4,8 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
@@ -15,6 +17,7 @@ import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
import Agenda.Agenda;
import Listeners.Mouse;
@@ -28,7 +31,7 @@ import Objects.Wall;
@SuppressWarnings("serial")
public class Panel extends JPanel {
public class Panel extends JPanel implements ActionListener {
BufferedImage background;
BufferedImage podiumImage, toiletImage, entranceImage, pathImage,
@@ -40,6 +43,7 @@ public class Panel extends JPanel {
// private ArrayList<Object> panelTypes = new ArrayList<Object>();
ArrayList<DrawObject> objects = new ArrayList<>();
ArrayList<Visitor> visitors = new ArrayList<>();
DrawObject dragObject = null;
private DrawObject selectedObject;
private String clickedOption = "drag";
@@ -51,7 +55,9 @@ public class Panel extends JPanel {
Point2D lastClickPosition = new Point(0, 0);
Point lastMousePosition = new Point(0, 0);
public Agenda agenda;
Agenda agenda;
int currentTime = 540;
int tick = 0;
// how to nieuwe dingen aan het panel toe te voegen:
// maak bufferedimage global aan, voeg er een image aan toe, en voeg de
@@ -89,10 +95,17 @@ public class Panel extends JPanel {
addMouseListener(new Mouse(this));
addMouseMotionListener(new MouseMotion(this));
agenda = new Agenda();
addMouseWheelListener(new MouseWheel(this));
new Timer(1000/20, this).start();
}
public void addVisitors(){
visitors.add(new Visitor("images/coin.png", new Point(100,300), agenda, objects));
}
public int getPanelInfoLength() {
int panelInfoLength = 0;
@@ -154,10 +167,31 @@ public class Panel extends JPanel {
for (DrawObject o : objects) {
o.draw(g2);
}
for (Visitor v : visitors){
v.draw(g2);
}
g2.setClip(null);
g2.setTransform(oldTransform);
}
@Override
public void actionPerformed(ActionEvent e) {
tick++;
if ( tick >= 10){
tick = 0;
currentTime++;
System.out.println(currentTime);
}
for (Visitor v: visitors){
v.update(objects, currentTime);
}
repaint();
}
public AffineTransform getCamera() {
AffineTransform tx = new AffineTransform();
+2 -5
View File
@@ -13,7 +13,6 @@ import javax.swing.filechooser.FileFilter;
import Agenda.Agenda;
public class SaveLoad {
@@ -67,7 +66,6 @@ public class SaveLoad {
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
//oos.writeObject(panel.agenda);
for (DrawObject object : panel.objects) {
oos.writeObject(object);
}
@@ -113,7 +111,8 @@ public class SaveLoad {
JOptionPane.showMessageDialog(null, "Loaded succesfully");
}
}
}
}
public static void fillTerrain(File file, Panel panel) {
FileInputStream fis = null;
@@ -127,8 +126,6 @@ public class SaveLoad {
Object object;
object = ois.readObject();
try {
//panel.agenda = (Agenda) object;
//object = ois.readObject();
panel.clearObjects();
while (object != null) {
panel.objects.add((DrawObject) object);
+171
View File
@@ -0,0 +1,171 @@
package Applicatie;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import Agenda.Agenda;
import Agenda.Event;
public class Visitor {
Point2D position;
double rotation;
double scale;
String filename;
ArrayList<Action> actions;
Agenda agenda;
ArrayList<DrawObject> objects;
public Visitor(String filename, Point2D position, Agenda agenda, ArrayList<DrawObject> objects) {
this.position = position;
this.filename = filename;
scale = 1;
rotation = 0;
actions = new ArrayList<Action>();
this.agenda = agenda;
this.objects = objects;
fillActions();
for (Action a : actions){
System.out.println(a.getStartTime());
}
}
public void draw(Graphics2D g2) {
Image image = new ImageIcon(filename).getImage();
g2.drawImage(image, getTransform(), null);
}
private AffineTransform getTransform() {
AffineTransform tx = new AffineTransform();
tx.scale(scale, scale);
tx.translate(position.getX(), position.getY());
Image image = new ImageIcon(filename).getImage();
tx.rotate(Math.toRadians(rotation), image.getWidth(null) / 2,
image.getHeight(null) / 2);
return tx;
}
public void update(ArrayList<DrawObject> objects, int currentTime) {
Point2D target = new Point(-100,-100);
for (Action a : actions){
if (currentTime >= a.getStartTime() && currentTime < a.getStoptime()){
target = a.getPosition();
}
}
moveToTarget(target, objects);
}
public void moveToTarget(Point2D target, ArrayList<DrawObject> objects){
int x = 0;
int y = 0;
if (target.getX() > position.getX()){
x = 2;
} else if (target.getX() < position.getX()){
x = - 2;
}
if (target.getY() > position.getY()){
y = 2;
} else if (target.getY() < position.getY()){
y = - 2;
}
Point2D oldPosition = position;
position = new Point2D.Double((int) (position.getX() + x),
(int) (position.getY() + y));
boolean possible = true;
for (DrawObject object : objects) {
if (hitTest(object)) {
possible = false;
}
}
if (possible == false) {
position = oldPosition;
}
}
public void fillActions() {
int startTime = 540;
int stopTime = 1260;
while (startTime < stopTime) {
int random = (int) Math.floor(Math.random() * 51);
if (random < 30) {
Point2D position = null;
for (Event e : agenda.getEvents()) {
if (convertMinutesToHours(startTime) > e.getStart()
&& convertMinutesToHours(startTime) < e.getStop()) {
for (DrawObject d : objects) {
if (d.getFileName().equals(e.getStage().getName())) {
position = d.getPosition();
}
}
}
}
int randomtime = (int) (40 + (Math.random() * (60 - 40)));
if ( position != null ){
actions.add(new Action(position, startTime, randomtime));
}
startTime = startTime + randomtime;
} else if (random >= 30 && random < 35) {
Point2D position = null;
for (DrawObject d : objects) {
if (d.getFileName().equals("images/entrance.png")) {
position = d.getPosition();
}
}
if ( position != null){
actions.add(new Action(position, startTime, 35));
startTime = startTime + 35;
}
startTime = startTime + 35;
} else if (random > 35) {
Point2D position = null;
for (DrawObject d : objects) {
if (d.getFileName().equals("images/wc.png")) {
position = d.getPosition();
}
}
if ( position != null){
actions.add(new Action(position, startTime, 35));
}
startTime = startTime + 35;
}
}
}
public int convertMinutesToHours(int startTime) {
int time = 0;
int hours = startTime / 60;
int minutes = startTime % 60;
time = (hours * 100) + minutes;
return time;
}
public int getEndX() {
Image image = new ImageIcon(filename).getImage();
return (int) (position.getX() + image.getWidth(null));
}
public int getEndY() {
Image image = new ImageIcon(filename).getImage();
return (int) (position.getY() + image.getHeight(null));
}
public boolean hitTest(DrawObject to2) {
return (getEndX() >= to2.getX() && getEndY() >= to2.getY()
&& to2.getEndX() >= position.getX() && to2.getEndY() >= position
.getY());
}
}
-96
View File
@@ -1,96 +0,0 @@
package Listeners;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.Map;
import Applicatie.DrawObject;
import Applicatie.Panel;
import Objects.Podium;
public class Mouse extends MouseAdapter {
private Panel panel;
private DrawObject selectedObject;
public Mouse(Panel panel) {
this.panel = panel;
selectedObject = null;
}
public void mousePressed(MouseEvent e) {
Point2D clickPoint = panel.getClickPoint(e.getPoint());
panel.setLastClickPosition(clickPoint);
panel.setLastMousePosition(e.getPoint());
if (e.getY() < 150) {
// if(e.getX() < 51) //51 = width podium image
// panel.setDragObject(new Podium(clickPoint));
// else
// panel.setDragObject(new Toilet(clickPoint));
int i = 0;
int last = 0;
for (BufferedImage image : panel.getPanelInfo()) {
if (e.getX() >= panel.getScrollfactor()
&& e.getX() < last + image.getWidth()
+ panel.getScrollfactor()) {
DrawObject tempDrawObj = panel.createNewDrawObject(i);
tempDrawObj.setPosition(clickPoint);
panel.setDragObject(tempDrawObj);
break;
}
i++;
last += image.getWidth();
}
if (panel.getDragObject() != null) {
panel.add(panel.getDragObject());
}
} else {
for (DrawObject o : panel.getObjects()) {
if (o.contains(clickPoint)) {
if (o == selectedObject) {
boolean upperLeft = o.containsCorner(clickPoint, 0);
boolean upperRight = o.containsCorner(clickPoint, 1);
boolean bottomLeft = o.containsCorner(clickPoint, 2);
boolean bottomRight = o.containsCorner(clickPoint, 3);
boolean rotate = o.containsCorner(clickPoint, 4);
if (upperLeft) {
panel.setClickedOption("upperLeft");
} else if (upperRight) {
panel.setClickedOption("upperRight");
} else if (bottomLeft) {
panel.setClickedOption("bottomLeft");
} else if (bottomRight) {
panel.setClickedOption("bottomRight");
} else if (rotate) {
panel.setClickedOption("rotate");
} else {
panel.setClickedOption("drag");
}
}
if (selectedObject != null)
selectedObject.setSelected(false);
panel.setDragObject(o);
o.setSelected(true);
selectedObject = o;
panel.setDragObject(o);
panel.getPP().setSelected(o);
}
}
}
if (panel.getDragObject() == null && selectedObject != null) {
selectedObject.setSelected(false);
selectedObject = null;
}
panel.repaint();
}
public void mouseReleased(MouseEvent e) {
panel.setDragObject(null);
panel.setClickedOption("drag");
panel.repaint();
}
}