From d769129415d6cf2425eaee66132aaa65d3b80473 Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 24 Feb 2015 23:54:50 +0000 Subject: [PATCH] Dragging to change time in Stage possible --- StagePanel.java | 24 ++++++++++++++++----- Timeline.java | 57 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/StagePanel.java b/StagePanel.java index b090550..400247c 100644 --- a/StagePanel.java +++ b/StagePanel.java @@ -13,19 +13,17 @@ public class StagePanel extends JPanel { private BufferedImage image; private int width, height, posX; - private boolean listening; - public StagePanel(int width, int height, int posX) + private Stage stage; + public StagePanel(int width, int height, int posX, Stage stage) { this.width = width; this.height = height; this.posX = posX; - listening = false; + this.stage = stage; image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = image.createGraphics(); g2.setColor(Color.blue); g2.fillRect(0, 0, image.getWidth(), image.getHeight()); -// this.setSize(new Dimension(width, height)); -// System.out.println(this.getSize()); } public void paint(Graphics g) @@ -51,6 +49,7 @@ public class StagePanel extends JPanel this.posX = posx; repaint(); } + public StagePanel(LayoutManager arg0, boolean arg1) { @@ -58,6 +57,21 @@ public class StagePanel extends JPanel } + public int getPosX() + { + return this.posX; + } + + public void setStageStartTime(int startTime) + { + stage.setStartTime(startTime); + } + + public int getStageStartTime() + { + return stage.getStartTime(); + } + public int getImageWidth() { return this.width; diff --git a/Timeline.java b/Timeline.java index f9314ef..d0c7b0c 100644 --- a/Timeline.java +++ b/Timeline.java @@ -65,14 +65,14 @@ public class Timeline extends JPanel for(Stage stage : stages) { - StagePanel stagepanel = new StagePanel(calcLengthOfStage(stage.getLength()), 30, calcshit(stage.getStartTime())); + StagePanel stagepanel = new StagePanel(calcLengthOfStage(stage.getLength()), 30, calcPositionOfStage(stage.getStartTime()), stage); Container content = stagepanel; content.setMaximumSize(new Dimension(frame.getWidth() - 70,30)); content.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { - resizeTimeline(); + resizeTimeline(stagepanel); } @Override @@ -114,40 +114,65 @@ public class Timeline extends JPanel this.add(centerPanel, BorderLayout.CENTER); } - public void resizeTimeline() + public void resizeTimeline(StagePanel stagepanel) { + int min = findMinMax(1); + int max = findMinMax(2); + System.out.println(stagepanel.getStageStartTime()); + System.out.println(calcStartTimeOfStagePanel(stagepanel.getPosX())); + stagepanel.setStageStartTime(calcStartTimeOfStagePanel(stagepanel.getPosX())); + } - //testcode + public int calcLengthOfStage(int length) { double lengthOfStage = 0; - int min = test(1); - int max = test(2); + int min = findMinMax(1); + int max = findMinMax(2); double frameWidth = frame.getWidth(); double pixelsPerLength = frameWidth / (max - min); -// System.out.println(pixelsPerLength); lengthOfStage = pixelsPerLength * (double)length; -// System.out.println(length); - System.out.println(lengthOfStage); return (int)lengthOfStage; } - //Testcode - public int calcshit(int startTime) + public int calcPositionOfStage(int startTime) { int posx = 0; - int min = test(1); - int max = test(2); + int min = findMinMax(1); + int max = findMinMax(2); posx = startTime - min; - + posx = posx * frame.getWidth() / (max-min); +// System.out.println("Frame: " + frame.getWidth()); +// System.out.println("startTime: " + startTime); +// System.out.println("Max: " + max + " min: " + min); +// System.out.println("Posx: " + posx); return posx; } + + public int calcStartTimeOfStagePanel(int posx) + { + int min = findMinMax(1); + int max = findMinMax(2); +// double startTime = (posx / (frame.getWidth() / (max - min )) ) + min; + //Omdat het anders onnodig afgerond wordt, alles apart in doubles. + double minmax = max-min; + double bottom = frame.getWidth() / minmax; + double all = posx / bottom; + double startTime = all + min; + + +// System.out.println("starttime----------------"); +// System.out.println("Frame: " + frame.getWidth()); +// System.out.println("startTime: " + startTime); +// System.out.println("Max: " + max + " min: " + min); +// System.out.println("Posx: " + posx); + return (int)startTime; + } - //testcode - public int test(int i) + public int findMinMax(int i) { // Event oldevent = new Event(0, 0, null, "Base", 0); // ArrayList oldevents = new ArrayList();