1 | /*
|
---|
2 | * Class PlotGraph
|
---|
3 | *
|
---|
4 | * A class that creates a window and displays within that window
|
---|
5 | * a graph of one or more x-y data sets
|
---|
6 | *
|
---|
7 | * This class extends Plot (also from Michael Thomas Flanagan's Library)
|
---|
8 | *
|
---|
9 | * For use if you are incorporating a plot into your own Java program
|
---|
10 | * See Plotter for a free standing graph plotting application
|
---|
11 | *
|
---|
12 | * WRITTEN BY: Dr Michael Thomas Flanagan
|
---|
13 | *
|
---|
14 | * DATE: February 2002
|
---|
15 | * UPDATED: 22 April 2004 and 14 August 2004, 7 July 2008
|
---|
16 | *
|
---|
17 | * DOCUMENTATION:
|
---|
18 | * See Michael Thomas Flanagan's Java library on-line web page:
|
---|
19 | * http://www.ee.ucl.ac.uk/~mflanaga/java/PlotGraph.html
|
---|
20 | * http://www.ee.ucl.ac.uk/~mflanaga/java/
|
---|
21 | *
|
---|
22 | * Copyright (c) 2002 - 2008
|
---|
23 | *
|
---|
24 | * PERMISSION TO COPY:
|
---|
25 | * Permission to use, copy and modify this software and its documentation for
|
---|
26 | * NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
|
---|
27 | * to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
|
---|
28 | *
|
---|
29 | * Dr Michael Thomas Flanagan makes no representations about the suitability
|
---|
30 | * or fitness of the software for any or for a particular purpose.
|
---|
31 | * Michael Thomas Flanagan shall not be liable for any damages suffered
|
---|
32 | * as a result of using, modifying or distributing this software or its derivatives.
|
---|
33 | *
|
---|
34 | ***************************************************************************************/
|
---|
35 |
|
---|
36 | package agents.anac.y2015.agentBuyogV2.flanagan.plot;
|
---|
37 |
|
---|
38 | // Include the windowing libraries
|
---|
39 | import java.awt.*;
|
---|
40 | import java.awt.event.*;
|
---|
41 | import javax.swing.*;
|
---|
42 | import javax.swing.JFrame;
|
---|
43 | import java.io.Serializable;
|
---|
44 |
|
---|
45 | // Declare a class that creates a window capable of being drawn to
|
---|
46 | public class PlotGraph extends Plot implements Serializable{
|
---|
47 |
|
---|
48 | protected static final long serialVersionUID = 1L; // serial version unique identifier
|
---|
49 |
|
---|
50 | protected int graphWidth = 800; // width of the window for the graph in pixels
|
---|
51 | protected int graphHeight = 600; // height of the window for the graph in pixels
|
---|
52 | protected int closeChoice = 1; // =1 clicking on close icon causes window to close
|
---|
53 | // and the the program is exited.
|
---|
54 | // =2 clicking on close icon causes window to close
|
---|
55 | // leaving the program running.
|
---|
56 | // Create the window object
|
---|
57 | protected JFrame window = new JFrame("Michael T Flanagan's plotting program - PlotGraph");
|
---|
58 |
|
---|
59 | // Constructor
|
---|
60 | // One 2-dimensional data arrays
|
---|
61 | public PlotGraph(double[][] data){
|
---|
62 | super(data);
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Constructor
|
---|
66 | //Two 1-dimensional data arrays
|
---|
67 | public PlotGraph(double[] xData, double[] yData){
|
---|
68 | super(xData, yData);
|
---|
69 | }
|
---|
70 |
|
---|
71 | // Rescale the y dimension of the graph window and graph
|
---|
72 | public void rescaleY(double yScaleFactor)
|
---|
73 | {
|
---|
74 | this.graphHeight=(int)Math.round((double)graphHeight*yScaleFactor);
|
---|
75 | super.yLen=(int)Math.round((double)super.yLen*yScaleFactor);
|
---|
76 | super.yTop=(int)Math.round((double)super.yTop*yScaleFactor);
|
---|
77 | super.yBot=super.yTop + super.yLen;
|
---|
78 | }
|
---|
79 |
|
---|
80 | // Rescale the x dimension of the graph window and graph
|
---|
81 | public void rescaleX(double xScaleFactor)
|
---|
82 | {
|
---|
83 | this.graphWidth=(int)Math.round((double)graphWidth*xScaleFactor);
|
---|
84 | super.xLen=(int)Math.round((double)super.xLen*xScaleFactor);
|
---|
85 | super.xBot=(int)Math.round((double)super.xBot*xScaleFactor);
|
---|
86 | super.xTop=super.xBot + super.xLen;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // Get pixel width of the PlotGraph window
|
---|
90 | public int getGraphWidth(){
|
---|
91 | return this.graphWidth;
|
---|
92 | }
|
---|
93 |
|
---|
94 | // Get pixel height of the PlotGraph window
|
---|
95 | public int getGraphHeight(){
|
---|
96 | return this.graphHeight;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // Reset height of graph window (pixels)
|
---|
100 | public void setGraphHeight(int graphHeight){
|
---|
101 | this.graphHeight=graphHeight;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // Reset width of graph window (pixels)
|
---|
105 | public void setGraphWidth(int graphWidth){
|
---|
106 | this.graphWidth=graphWidth;
|
---|
107 | }
|
---|
108 |
|
---|
109 | // Get close choice
|
---|
110 | public int getCloseChoice(){
|
---|
111 | return this.closeChoice;
|
---|
112 | }
|
---|
113 |
|
---|
114 | // Reset close choice
|
---|
115 | public void setCloseChoice(int choice){
|
---|
116 | this.closeChoice = choice;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // The paint method to draw the graph.
|
---|
120 | public void paint(Graphics g){
|
---|
121 |
|
---|
122 | // Rescale - needed for redrawing if graph window is resized by dragging
|
---|
123 | double newGraphWidth = this.getSize().width;
|
---|
124 | double newGraphHeight = this.getSize().height;
|
---|
125 | double xScale = newGraphWidth/(double)this.graphWidth;
|
---|
126 | double yScale = newGraphHeight/(double)this.graphHeight;
|
---|
127 | rescaleX(xScale);
|
---|
128 | rescaleY(yScale);
|
---|
129 |
|
---|
130 | // Call graphing method
|
---|
131 | graph(g);
|
---|
132 | }
|
---|
133 |
|
---|
134 | // Set up the window and show graph
|
---|
135 | public void plot(){
|
---|
136 | // Set the initial size of the graph window
|
---|
137 | setSize(this.graphWidth, this.graphHeight);
|
---|
138 |
|
---|
139 | // Set background colour
|
---|
140 | window.getContentPane().setBackground(Color.white);
|
---|
141 |
|
---|
142 | // Choose close box
|
---|
143 | if(this.closeChoice==1){
|
---|
144 | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
145 | }
|
---|
146 | else{
|
---|
147 | window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
|
---|
148 | }
|
---|
149 |
|
---|
150 | // Add graph canvas
|
---|
151 | window.getContentPane().add("Center", this);
|
---|
152 |
|
---|
153 | // Set the window up
|
---|
154 | window.pack();
|
---|
155 | window.setResizable(true);
|
---|
156 | window.toFront();
|
---|
157 |
|
---|
158 | // Show the window
|
---|
159 | window.setVisible(true);
|
---|
160 | }
|
---|
161 |
|
---|
162 | // Displays dialogue box asking if you wish to exit program
|
---|
163 | // Answering yes end program - will simultaneously close the graph windows
|
---|
164 | public void endProgram(){
|
---|
165 |
|
---|
166 | int ans = JOptionPane.showConfirmDialog(null, "Do you wish to end the program\n"+"This will also close the graph window or windows", "End Program", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
|
---|
167 | if(ans==0){
|
---|
168 | System.exit(0);
|
---|
169 | }
|
---|
170 | else{
|
---|
171 | String message = "Now you must press the appropriate escape key/s, e.g. Ctrl C, to exit this program\n";
|
---|
172 | if(this.closeChoice==1)message += "or close a graph window";
|
---|
173 | JOptionPane.showMessageDialog(null, message);
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | // Return the serial version unique identifier
|
---|
178 | public static long getSerialVersionUID(){
|
---|
179 | return PlotGraph.serialVersionUID;
|
---|
180 | }
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|