Parcourir la source

save btn beim graphen

Kevin Trometer il y a 7 ans
Parent
commit
db247201c5
3 fichiers modifiés avec 54 ajouts et 33 suppressions
  1. 11 17
      src/ui/view/MyCanvas.java
  2. 41 12
      src/ui/view/StatisticGraphPanel.java
  3. 2 4
      src/ui/view/splitPane.java

+ 11 - 17
src/ui/view/MyCanvas.java

@@ -89,16 +89,15 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 												// rightclicked
 
 	javax.swing.Timer animT; // animation Timer
-	private final int ANIMTIME = 500; //animation Time
-	
+	private final int ANIMTIME = 500; // animation Time
+
 	private ArrayList<AbstractCpsObject> animCps = null;
 	private int animFPS = 60;
 	private int animDuration = ANIMTIME; // animation Duration
 	private int animDelay = 1000 / animFPS; // animation Delay
 	private int animSteps = animDuration / animDelay; // animation Steps;
-	private long start = 0;
-	private long elapsedTime = 0;
-	
+	private long start = 0; // for time
+	private long elapsedTime = 0; // outprint
 
 	// contains the value of the Capacity for new created Edges
 
@@ -114,7 +113,6 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		this.add(objectTT);
 		this.controller = control;
 		this.model = mod;
-		System.setProperty("sun.java2d.opengl","True");
 
 		scalediv20 = model.getScale() / 20;
 
@@ -148,7 +146,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 				unPos = new Position(0, 0);
 				animCps = new ArrayList<>();
 				for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
-					animCps.add(cps); //add to animation Cps
+					animCps.add(cps); // add to animation Cps ArrayList
 					unPos.x += cps.getPosition().x;
 					unPos.y += cps.getPosition().y;
 				}
@@ -169,10 +167,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					public void actionPerformed(ActionEvent e) {
 						if (animDuration - animDelay > 0 && animCps.size() > 1) {
 							for (int i = 0; i < animCps.size(); i++) {
-								double x1 = animCps.get(i).getPosition().x;
-								double y1 = animCps.get(i).getPosition().y;
-								x1 = x1 - unPos.x;
-								y1 = y1 - unPos.y;
+								double x1 = animCps.get(i).getPosition().x - unPos.x;
+								double y1 = animCps.get(i).getPosition().y - unPos.y;
 								animCps.get(i).getPosition().x -= x1 / animSteps;
 								animCps.get(i).getPosition().y -= y1 / animSteps;
 							}
@@ -216,8 +212,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 				for (AbstractCpsObject cps : animCps) {
 					int x = ((CpsUpperNode) tempCps).getPosition().x;
 					int y = ((CpsUpperNode) tempCps).getPosition().y;
-					
-					cps.setPosition(new Position(x,y));
+
+					cps.setPosition(new Position(x, y));
 				}
 
 				animT = new javax.swing.Timer(animDelay, new ActionListener() {
@@ -226,10 +222,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					public void actionPerformed(ActionEvent e) {
 						if (animDuration - animDelay >= 0) {
 							for (int i = 0; i < animCps.size(); i++) {
-								double x1 = animCps.get(i).getPosition().x;
-								double y1 = animCps.get(i).getPosition().y;
-								x1 = x1 - savePos.get(i).x;
-								y1 = y1 - savePos.get(i).y;
+								double x1 = animCps.get(i).getPosition().x - savePos.get(i).x;
+								double y1 = animCps.get(i).getPosition().y - savePos.get(i).y;
 								animCps.get(i).getPosition().x -= x1 / animSteps;
 								animCps.get(i).getPosition().y -= y1 / animSteps;
 							}

+ 41 - 12
src/ui/view/StatisticGraphPanel.java

@@ -9,8 +9,11 @@ import ui.model.Model;
 
 import javax.swing.JLabel;
 import javax.swing.JButton;
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
 import javax.swing.SwingConstants;
 import javax.swing.Timer;
+import javax.swing.border.Border;
 import javax.swing.text.StyledDocument;
 
 import java.awt.BorderLayout;
@@ -24,8 +27,15 @@ import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
 import java.util.Hashtable;
+
+import javax.imageio.ImageIO;
 import javax.swing.BoxLayout;
+import javax.swing.GroupLayout.Alignment;
+
 import java.awt.GridLayout;
 
 public class StatisticGraphPanel extends JPanel {
@@ -42,13 +52,13 @@ public class StatisticGraphPanel extends JPanel {
 	private final JLabel maximumLabel = new JLabel("0");
 	private JPanel topPanel = new JPanel();
 	private JButton closeButton = new JButton("X");
+	private JButton savImgButton = new JButton("Save as Image");
 
 	// Variables
 	String graphName;
 	private final JPanel legendPanel = new JPanel();
 	private JPanel that;
 	private Hashtable<String, StatisticGraphPanel> graphHashtable;
-	private JPanel tempP; // for makeLegend
 
 	/**
 	 * Constructor.
@@ -69,9 +79,10 @@ public class StatisticGraphPanel extends JPanel {
 
 		// ******************** Component Propertys ***************//
 		// Graph
+		// this.setPreferredSize(new Dimension(280, 300));
 		sGraph.setPreferredSize(new Dimension(280, 180));
 		sGraph.setMinimumSize(new Dimension(100, 150));
-		//this.setMaximumSize(new Dimension(1000, 1000));
+		// this.setMaximumSize(new Dimension(1000, 1000));
 
 		// Graph Name
 		graphNameLabel = new JLabel(graphName);
@@ -79,17 +90,36 @@ public class StatisticGraphPanel extends JPanel {
 
 		// Panel on top (Name and Close Button)
 		topPanel.setLayout(new BorderLayout(0, 0));
-		topPanel.add(graphNameLabel, BorderLayout.CENTER);
+		topPanel.add(graphNameLabel, BorderLayout.WEST);
 		topPanel.add(closeButton, BorderLayout.EAST);
+		topPanel.add(savImgButton, BorderLayout.CENTER);
+		savImgButton.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				BufferedImage img = new BufferedImage(that.getWidth(), that.getHeight(), BufferedImage.TYPE_INT_RGB);
+				that.print(img.getGraphics());
+				try {
+					JFileChooser fileChooser = new JFileChooser();
+					if (fileChooser.showSaveDialog(new JFrame()) == JFileChooser.APPROVE_OPTION) {
+						String file = fileChooser.getSelectedFile().getPath();
+						ImageIO.write(img, "jpg", new File(file+".jpg"));
+					}
+				} catch (IOException e1) {
+					// TODO Auto-generated catch block
+					e1.printStackTrace();
+				}
+
+			}
+		});
 		topPanel.setBorder(null);
 
 		// Maximum Label
 		maximumLabel.setVerticalAlignment(SwingConstants.TOP);
 		maximumLabel.setMinimumSize(new Dimension(30, 10));
-		
-		//Legend Panel
+
+		// Legend Panel
 		legendPanel.setLayout(new GridLayout(0, 5, 0, 0));
-		
+
 		// ******************** Component Listener ****************//
 
 		that = this;
@@ -141,18 +171,17 @@ public class StatisticGraphPanel extends JPanel {
 		default:
 			break;
 		}
-		
-		
+
 		JLabel b = new JLabel(set.getCpsObject().getName() + ": " + property);
 		b.setBackground(set.getColor());
-		int color = Math.max(Math.max(set.getColor().getRed(), set.getColor().getGreen()),set.getColor().getBlue());
-		if (color<=128) {
+		int color = Math.max(Math.max(set.getColor().getRed(), set.getColor().getGreen()), set.getColor().getBlue());
+		if (color <= 128) {
 			b.setForeground(Color.WHITE);
 		}
-		
+
 		b.setOpaque(true);
 		b.addMouseListener(new MouseAdapter() {
-		
+
 			@Override
 			public void mousePressed(MouseEvent e) {
 				if (MouseEvent.BUTTON3 == e.getButton()) {

+ 2 - 4
src/ui/view/splitPane.java

@@ -379,10 +379,8 @@ public class splitPane extends JSplitPane implements GraphListener {
 									&& graphNrTxtField.getText().length() > 0) {
 								tmp = new StatisticGraphPanel(controller.getModel(), controller,
 										graphNrTxtField.getText(), graphHashtable);
-								// tmp.setPreferredSize(new Dimension(280,
-								// 150));
-								// tmp.setMaximumSize(new Dimension(1000,
-								// 1000));
+								//tmp.setPreferredSize(new Dimension(280, 150));
+								//tmp.setMaximumSize(new Dimension(1000, 1000));
 								// tmp.setMinimumSize(new Dimension(100, 45));
 								tmp.setBorder(new LineBorder(new Color(0, 0, 0), 1));
 								graphPanel.add(tmp);