📄 graph_pane.java
字号:
import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.awt.event.*;/********Class Graph_Pane******//* this class contains the graph as well as the other components that either communicate with the graph or alter the graph. Components: 1 Menu Bar -- Graph representation menu 2 Title on a panel 3 Time Value display This class is also an event listener for these components To make Graph_Pane communicate the marked time to the top-level container, the container needs to be passed as an object to this class using the second constructor, the event code in the mouseClicked function needs to be changed - add the necessary function to the container and utillize it in the if statement, replace the (Graph_Pane) explicit typecasting with the (container class) type cast. which basically means that you need to also provide a double variable in the container called markd_time.*/public class Graph_Pane extends JPanelimplements MouseMotionListener,MouseListener,ActionListener{ private JFrame owner; private Dimension dimensions; private JPanel title; private JLabel title_label; private Graph graph; private TimeInterface time_listener; private JTextField track_time; private JMenu legend_menu; private JCheckBoxMenuItem legend[]; private JScrollPane graph_scroller; private JList legend_list; JDialog legend_dialog; private ImageIcon legend_images_selected[]; private ImageIcon legend_images[]; private boolean need_color_change; JMenuItem zoomIn,zoomOut; Cursor default_cur = new Cursor(Cursor.DEFAULT_CURSOR), on_graph = new Cursor(Cursor.CROSSHAIR_CURSOR); /* Constructor */ public Graph_Pane( JFrame parent, String label, double values[][], String value_labels[], Color value_colors[], double start_time, double end_time ) { super(new GridBagLayout()); Graph_data.resetUsedColors(); owner = parent; need_color_change = true; GridBagConstraints constraints = new GridBagConstraints(); constraints.anchor = GridBagConstraints.CENTER; constraints.weightx = 100; legend_images = new ImageIcon[value_labels.length]; legend_images_selected = new ImageIcon[value_labels.length]; JMenuBar menubar = new JMenuBar(); menubar.setLayout(new FlowLayout(FlowLayout.LEFT,5,5)); JMenu menu = new JMenu("File"); menubar.add(menu); JMenuItem item = new JMenuItem("View Statistics"); item.addActionListener(this); menu.add(item); item = new JMenuItem("Close"); item.addActionListener(this); menu.add(item); menu = new JMenu("Graph"); menubar.add(menu); JMenu submenu = new JMenu("Type"); item = new JMenuItem("Bar"); item.addActionListener(this); submenu.add(item); item = new JMenuItem("Line"); item.addActionListener(this); submenu.add(item); menu.add(submenu); submenu = new JMenu("ReScale"); ButtonGroup group = new ButtonGroup(); JRadioButtonMenuItem ritem = new JRadioButtonMenuItem("On"); ritem.addActionListener(this); ritem.setSelected(true); group.add(ritem); submenu.add(ritem); ritem = new JRadioButtonMenuItem("Off"); ritem.addActionListener(this); group.add(ritem); submenu.add(ritem); menu.add(submenu); menu = new JMenu("Data Sets"); menubar.add(menu); item = new JMenuItem("All"); item.addActionListener(this); menu.add(item); item = new JMenuItem("None"); item.addActionListener(this); menu.add(item); menu = new JMenu("Zoom"); menubar.add(menu); zoomIn = new JMenuItem("In"); zoomIn.addActionListener(this); menu.add(zoomIn); zoomOut = new JMenuItem("Out"); zoomOut.addActionListener(this); zoomOut.setEnabled(false); menu.add(zoomOut); item = new JMenuItem("Reset"); item.addActionListener(this); menu.add(item); menu = new JMenu("Legend"); menubar.add(menu); item = new JMenuItem("Display"); item.addActionListener(this); menu.add(item); item = new JMenuItem("Hide"); item.addActionListener(this); menu.add(item); menubar.setBorder(new EtchedBorder()); buildConstraints(constraints, 0, 0, 3, 1, GridBagConstraints.HORIZONTAL); add(menubar,constraints); title = new Graph_Title(label); JLabel temp_label = new JLabel("Time:",JLabel.CENTER); title.add(temp_label); track_time = new JTextField("0.0",7); track_time.setEnabled(false); title.add(track_time); buildConstraints(constraints, 0, 1, 3, 1, GridBagConstraints.HORIZONTAL); add(title,constraints); graph = new Graph(values,value_labels,value_colors, start_time,end_time); dimensions = graph.getDimension(); graph_scroller = new JScrollPane(graph, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); YAxisPanel YAxis = graph.getYAxis(); constraints.anchor = GridBagConstraints.NORTHEAST; buildConstraints(constraints, 0, 2, 1, 1, GridBagConstraints.HORIZONTAL); constraints.weightx = 0; add(YAxis,constraints); graph_scroller.setBorder(new LineBorder(Color.white)); graph_scroller.getHorizontalScrollBar(). setBorder(new ScrollBarBorder()); graph_scroller.addComponentListener ( new ComponentAdapter() { public void componentResized(ComponentEvent e) { graph.setViewportWidth (((JScrollPane)e.getSource()).getWidth()); } }); YAxis.shiftOrigin(0,1); constraints.anchor = GridBagConstraints.CENTER; constraints.weightx = 100; buildConstraints(constraints, 1, 2, 1, 1, GridBagConstraints.HORIZONTAL); add(graph_scroller,constraints); graph.addMouseMotionListener(this); graph.addMouseListener(this); XAxisEndPanel XAxisEnd = graph.getXAxisEnd(); XAxisEnd.shiftOrigin(0,1); constraints.anchor = GridBagConstraints.NORTHEAST; buildConstraints(constraints, 2, 2, 1, 1, GridBagConstraints.HORIZONTAL); constraints.weightx = 0; add(XAxisEnd,constraints); makeLegendDialog(value_labels); temp_label.setBackground(Color.white); title.setBackground(Color.white); Dimension temp_dimension = new Dimension(graph.getXAxisDimension().width, dimensions.height+15); setObjectSize(graph_scroller, temp_dimension); temp_dimension = new Dimension(YAxis.getDimension().width, dimensions.height+15); setObjectSize(YAxis, temp_dimension); setObjectSize(XAxisEnd, temp_dimension); setBackground(Color.white); } public void init() { } public void paint(Graphics g) { int i; if(need_color_change) { Image temp_image; Graphics renderer; for(i=0;i<legend_images.length;i++) { temp_image = createImage(15,15); renderer = temp_image.getGraphics(); renderer.setColor(Color.white); renderer.fillRect(0,0,15,15); renderer.setColor(Color.black); renderer.drawArc(0,0,12,12,0,360); legend_images[i] = new ImageIcon(temp_image); renderer.dispose(); temp_image = createImage(15,15); renderer = temp_image.getGraphics(); renderer.setColor(Color.white); renderer.fillRect(0,0,15,15); renderer.setColor(Color.black); renderer.drawArc(0,0,12,12,0,360); renderer.setColor(graph.getColor(i)); renderer.fillArc(2,2,8,8,0,360); legend_images_selected[i] = new ImageIcon(temp_image); renderer.dispose(); } need_color_change = false; } super.paint(g); } public Dimension getDimension() { return new Dimension(dimensions); } /* This function accepts a time value and moves the marker on the graph to the cooresponding value on the graph. */ void moveMarkerTo(double time) { graph.moveMarkerTo(time); } public void setTimeListener(TimeInterface ti) { time_listener = ti; } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -