📄 statisticsviewer.java
字号:
import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.awt.event.*;import java.util.Vector;/********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 StatisticsViewer extends JFrame implements /*MouseMotionListener,MouseListener,*/ ActionListener{ private Dimension dimensions; private Statistics_Data data_aray[]; private int selected_index; private Stat_Graph_Title title; private JLabel title_label; private Stat_Graph graph; private StatTimeInterface time_listener; private JTextField track_time; private JMenu graph_menu; private JMenuItem graph_list[]; private JList legend_list; private JDialog legend_dialog; private ImageIcon legend_images_selected[]; private ImageIcon legend_images[]; private boolean need_color_change; private static JFrame dummy_frame = new JFrame(); private static Vector windows = new Vector(); //JMenuItem zoomIn,zoomOut; private JMenuItem display_legend, hide_legend, all_data, no_data; private static boolean app_Enabled = false; Cursor default_cur = new Cursor(Cursor.DEFAULT_CURSOR), on_graph = new Cursor(Cursor.CROSSHAIR_CURSOR); /* Constructor */ public StatisticsViewer() { super("Statistics Viewer"); Container panel = getContentPane(); JMenuBar menubar = new JMenuBar(); panel.setLayout(new GridBagLayout()); Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize(); windows.addElement(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ Window w = e.getWindow(); windows.removeElement(w); w.setVisible(false); w.dispose(); if(app_Enabled && windows.isEmpty()) System.exit(0); } }); setSize(screen_size.width/2,screen_size.height/2); need_color_change = false; GridBagConstraints constraints = new GridBagConstraints(); constraints.anchor = GridBagConstraints.CENTER; constraints.weightx = 100; constraints.weighty = 0; menubar.setLayout(new FlowLayout(FlowLayout.LEFT,5,5)); setJMenuBar(menubar); JMenu menu = new JMenu("File"); menubar.add(menu); JMenuItem item = new JMenuItem("Open"); item.addActionListener(this); menu.add(item); item = new JMenuItem("Close"); item.addActionListener(this); menu.add(item); menu.addSeparator(); item = new JMenuItem("New Window"); item.addActionListener(this); menu.add(item); menu.addSeparator(); item = new JMenuItem("Exit"); item.addActionListener(this); menu.add(item); graph_menu = new JMenu("Graph"); menubar.add(graph_menu); menu = new JMenu("Data Sets"); menubar.add(menu); all_data = new JMenuItem("All"); all_data.addActionListener(this); all_data.setEnabled(false); menu.add(all_data); no_data = new JMenuItem("None"); no_data.addActionListener(this); no_data.setEnabled(false); menu.add(no_data); menu = new JMenu("Legend"); menubar.add(menu); display_legend = new JMenuItem("Display"); display_legend.addActionListener(this); display_legend.setEnabled(false); menu.add(display_legend); hide_legend = new JMenuItem("Hide"); hide_legend.addActionListener(this); hide_legend.setEnabled(false); menu.add(hide_legend); menubar.setBorder(new EtchedBorder()); title = new Stat_Graph_Title(); buildConstraints(constraints, 0, 1, 1, 1, GridBagConstraints.HORIZONTAL); panel.add(title,constraints); graph = new Stat_Graph(); constraints.anchor = GridBagConstraints.CENTER; constraints.weighty = 100; buildConstraints(constraints, 0, 2, 1, 1, GridBagConstraints.BOTH); panel.add(graph,constraints); legend_dialog = new JDialog(dummy_frame,"Legend"); setLocation(20*(windows.size()-1),20*(windows.size()-1)); setVisible(true); } public StatisticsViewer(boolean app_enabler) { this(); if(app_enabler) { app_Enabled = true; } else app_Enabled = false; } 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 (data_aray[selected_index].getColor(i)); renderer.fillArc(2,2,8,8,0,360); legend_images_selected[i] = new ImageIcon(temp_image); renderer.dispose(); legend_list.repaint(); } need_color_change = false; } super.paint(g); } public Dimension getDimension() { return new Dimension(dimensions); } public void setTimeListener(StatTimeInterface ti) { time_listener = ti; } /* public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e) { if((time_listener != null)*/ /*&& (e.getClickCount()==2)*//*) { Point coords = e.getPoint(), graph_origin = graph.getOrigin(); if((coords.x >= graph_origin.x) && (coords.x <= (graph_origin.x + graph.getGraphRange())) && (coords.y <= graph_origin.y) && (coords.y > graph.GRAPH_MARGIN)) time_listener.showTime (graph.mark_time (coords, graph_scroller.getViewport().getViewPosition())); } } public void mouseMoved(MouseEvent e) { Point coords = e.getPoint(), graph_origin = graph.getOrigin(); String time_value_s; if((coords.x >= graph_origin.x) && (coords.x <= (graph_origin.x + graph.getGraphRange())) && (coords.y <= graph_origin.y) && (coords.y > graph.GRAPH_MARGIN)) { graph.setCursor(on_graph); time_value_s = graph.calculateXValueString(coords.x,8); track_time.setText(time_value_s); } else graph.setCursor(default_cur); } public void mouseDragged(MouseEvent e){} */ public void actionPerformed(ActionEvent e) { if((e.getSource() == all_data) || (e.getActionCommand().compareTo("All") == 0)) { for(int i=0; (i < legend_images.length);i++) { data_aray[selected_index].setState(i,true); } legend_list.repaint(); graph.repaint(); } else if((e.getSource() == no_data) || (e.getActionCommand().compareTo("None") == 0)) { for(int i=0; (i < legend_images.length);i++) { data_aray[selected_index].setState(i,false); } legend_list.repaint(); graph.repaint(); } else if(e.getActionCommand().compareTo("Open") == 0) { initializeData(null); } else if(e.getActionCommand().compareTo("Close") == 0) { removeCurrentFile(); } else if(e.getActionCommand().compareTo("New Window") == 0) { StatisticsViewer graph_pane = new StatisticsViewer(app_Enabled); } else if(e.getActionCommand().compareTo("Exit") == 0) { StatisticsViewer.closeAll(); } else if(e.getSource() == display_legend) { legend_dialog.pack(); legend_dialog.setVisible(true); } else if(e.getSource() == hide_legend) { legend_dialog.pack(); legend_dialog.setVisible(false); } else if(e.getActionCommand().compareTo("Select/Deselect") == 0) { int selected[] = legend_list.getSelectedIndices(); for(int i=0; i < selected.length;i++) data_aray[selected_index].setState
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -