📄 graph2dplot.java
字号:
/********************************************************************** This library is free software; you can redistribute it and/or* modify it under the terms of the GNU Library General Public* License as published by the Free Software Foundation; either* version 2 of the License, or (at your option) any later version.** This library is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU* Library General Public License for more details.** You should have received a copy of the GNU Library General Public* License along with this library; if not, write to the* Free Software Foundation, Inc., 59 Temple Place - Suite 330,* Boston, MA 02111-1307, USA.** @author: Copyright (C) Tim Carver*********************************************************************/package org.emboss.jemboss.graphics;import javax.swing.*;import javax.swing.event.ChangeListener;import javax.swing.event.ChangeEvent;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;import java.text.DecimalFormat;import java.awt.geom.AffineTransform;import org.emboss.jemboss.editor.SequenceProperties;import org.emboss.jemboss.gui.filetree.FileEditorDisplay;import org.emboss.jemboss.gui.form.TextFieldInt;import org.emboss.jemboss.gui.form.TextFieldFloat;import org.emboss.jemboss.gui.ScrollPanel; /**** Use java 2D to draw data points from EMBOSS applications.**/public class Graph2DPlot extends ScrollPanel{ private Cursor cbusy = new Cursor(Cursor.WAIT_CURSOR); private Cursor cdone = new Cursor(Cursor.DEFAULT_CURSOR); private Object[][] emboss_data; private float xmin = 0; private float xmax = 0; private float ymin = 0; private float ymax = 0; private float xmin_screen = 0; private float xmax_screen = 0; private float ymin_screen = 0; private float ymax_screen = 0; private StringBuffer graph_data; private Color graph_colour = Color.black; private TextFieldFloat graph_line; private TextFieldFloat xstart; private TextFieldFloat xend; private TextFieldFloat ystart; private TextFieldFloat yend; private int xborder = 100; private int yborder = 100; private int WID = 700; private int HGT = 700; private int width = WID; private int height = HGT; private JComboBox x_formatList = null; private JComboBox y_formatList = null; private TextFieldInt xticks = null; private TextFieldInt yticks = null; private JTextField maintitle_field = null; private JTextField xtitle_field = null; private JTextField ytitle_field = null; // private static int LINE = 1; private static int TEXT = 2; private static int RECTANGLE = 3; private static int FILLED_RECTANGLE = 4; private static int TEXTLINE = 5; private static int AXIS = 6; // draw rectangle around graph private boolean draw_axes = true; private boolean rectangle = false; private boolean screen_min_max = false; private Image offscreen = null; private String maintitle = ""; private String xtitle = ""; private String ytitle = ""; private String fileName = null; private Color plplot_colour[] = { Color.black, Color.red, Color.yellow, Color.green, SequenceProperties.AQUAMARINE, Color.pink, SequenceProperties.WHEAT, Color.gray, SequenceProperties.BROWN, Color.blue, SequenceProperties.BLUEVIOLET, Color.cyan, SequenceProperties.TURQUOISE, SequenceProperties.MAGENTA, SequenceProperties.SALMON, Color.white }; /** * * Contructor for graph object. * */ public Graph2DPlot() { setPreferredSize(new Dimension(width,height)); setToolTipText(""); } /** * * Set the data to plot. * */ public void setData(Object[][] emboss_data) { this.emboss_data = emboss_data; calcMinMax(); } /** * * Pass in EMBOSS data graphics file * */ public void setFileData(String s, String fileName) { this.fileName = fileName; try { StringReader reader = new StringReader(s); emboss_data = readGraph(reader); } catch(FileNotFoundException fnne){} catch(IOException ioe){} if(fileName.indexOf("plotorf") > -1) { WID = 1000; HGT = 400; width = WID; height = HGT; }// if(xmin == 0 && xmax == 0 && ymin == 0 && ymax == 0) calcMinMax(); } /** * * Pass in EMBOSS data graphics file * */ public void setFileData(File filename) { this.fileName = filename.getName(); try { FileReader reader = new FileReader(filename); emboss_data = readGraph(reader); } catch(FileNotFoundException fnne){} catch(IOException ioe){} if(fileName.indexOf("plotorf") > -1) { WID = 1000; HGT = 400; width = WID; height = HGT; }// if(xmin == 0 && xmax == 0 && ymin == 0 && ymax == 0) calcMinMax(); } /** * * Create a JMenuBar for this graph * */ public JMenuBar getMenuBar(boolean bexit, final JFrame frame) { JMenuBar menubar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); JMenuItem showMenu = new JMenuItem("Display data..."); showMenu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFrame frame = new JFrame("EMBOSS data file"); FileEditorDisplay fed = new FileEditorDisplay("graph_data.dat", graph_data.toString()); JScrollPane jsp = new JScrollPane(fed); fed.setCaretPosition(0); frame.getContentPane().add(jsp); frame.pack(); frame.setSize(640,580); frame.setVisible(true); } }); fileMenu.add(showMenu); JMenuItem printMenu = new JMenuItem("Print..."); printMenu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { PrintPlot pp = new PrintPlot(Graph2DPlot.this); pp.print(); } }); fileMenu.add(printMenu); fileMenu.add(new JSeparator()); if(bexit) { JMenuItem fileMenuExit = new JMenuItem("Exit"); fileMenuExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); fileMenu.add(fileMenuExit); } else { JMenuItem fileMenuExit = new JMenuItem("Close"); fileMenuExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame.setVisible(false); frame.dispose(); } }); fileMenu.add(fileMenuExit); } menubar.add(fileMenu); JMenu optionsMenu = new JMenu("Options"); JMenuItem axesOptions = new JMenuItem("Axes, Labels..."); axesOptions.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String formats[] = { "default", "##0.0", "##0.00", "#0.##E0", "##0.##E0", "###0.##E0" }; Box xbdown = Box.createVerticalBox(); Box ybdown = Box.createVerticalBox(); Dimension dim = new Dimension(130,20); Box bacross = Box.createHorizontalBox(); x_formatList = new JComboBox(formats); x_formatList.setPreferredSize(dim); x_formatList.setEditable(true); bacross.add(x_formatList); bacross.add(new JLabel(" X-axis Number Format ")); xbdown.add(bacross); xbdown.add(Box.createVerticalStrut(5)); bacross = Box.createHorizontalBox(); y_formatList = new JComboBox(formats); y_formatList.setPreferredSize(dim); y_formatList.setEditable(true); bacross.add(y_formatList); bacross.add(new JLabel(" Y-axis Number Format")); ybdown.add(bacross); ybdown.add(Box.createVerticalStrut(5));// x-ticks bacross = Box.createHorizontalBox(); if(xticks == null) { xticks = new TextFieldInt(); xticks.setValue(10); } xticks.setPreferredSize(dim); xticks.setMaximumSize(dim); bacross.add(xticks); bacross.add(new JLabel(" Number of X ticks ")); bacross.add(Box.createHorizontalGlue()); xbdown.add(bacross); xbdown.add(Box.createVerticalStrut(5));// y-ticks bacross = Box.createHorizontalBox(); if(yticks == null) { yticks = new TextFieldInt(); yticks.setValue(10); } yticks.setPreferredSize(dim); yticks.setMaximumSize(dim); bacross.add(yticks); bacross.add(new JLabel(" Number of Y ticks")); bacross.add(Box.createHorizontalGlue()); ybdown.add(bacross); ybdown.add(Box.createVerticalStrut(5));// x-ticks bacross = Box.createHorizontalBox(); if(xstart == null) { xstart = new TextFieldFloat(); xstart.setValue(xmin); } xstart.setPreferredSize(dim); xstart.setMaximumSize(dim); bacross.add(xstart); bacross.add(new JLabel(" Start X Tick ")); bacross.add(Box.createHorizontalGlue()); xbdown.add(bacross); xbdown.add(Box.createVerticalStrut(5));// y-ticks bacross = Box.createHorizontalBox(); if(ystart == null) { ystart = new TextFieldFloat(); ystart.setValue(ymin); } ystart.setPreferredSize(dim); ystart.setMaximumSize(dim); bacross.add(ystart); bacross.add(new JLabel(" Start Y Tick")); bacross.add(Box.createHorizontalGlue()); ybdown.add(bacross); ybdown.add(Box.createVerticalStrut(5)); // x-ticks bacross = Box.createHorizontalBox(); if(xend == null) { xend = new TextFieldFloat(); xend.setValue(xmax); } xend.setPreferredSize(dim); xend.setMaximumSize(dim); bacross.add(xend); bacross.add(new JLabel(" End X Tick ")); bacross.add(Box.createHorizontalGlue()); xbdown.add(bacross); xbdown.add(Box.createVerticalStrut(5)); xbdown.add(new JSeparator()); xbdown.add(Box.createVerticalStrut(5));// y-ticks bacross = Box.createHorizontalBox(); if(yend == null) { yend = new TextFieldFloat(); yend.setValue(ymax); } yend.setPreferredSize(dim); yend.setMaximumSize(dim); bacross.add(yend); bacross.add(new JLabel(" End Y Tick")); bacross.add(Box.createHorizontalGlue()); ybdown.add(bacross); ybdown.add(Box.createVerticalStrut(5)); ybdown.add(new JSeparator()); ybdown.add(Box.createVerticalStrut(5));// x-label bacross = Box.createHorizontalBox(); if(xtitle_field == null) xtitle_field = new JTextField(xtitle); xtitle_field.setPreferredSize(dim); xtitle_field.setMaximumSize(dim); bacross.add(xtitle_field); bacross.add(new JLabel(" X Axis Label ")); bacross.add(Box.createHorizontalGlue()); xbdown.add(bacross); xbdown.add(Box.createVerticalStrut(5)); xbdown.add(new JSeparator()); xbdown.add(Box.createVerticalStrut(5));// y-label bacross = Box.createHorizontalBox(); if(ytitle_field == null) ytitle_field = new JTextField(ytitle); ytitle_field.setPreferredSize(dim); ytitle_field.setMaximumSize(dim); bacross.add(ytitle_field); bacross.add(new JLabel(" Y Axis Label")); bacross.add(Box.createHorizontalGlue()); ybdown.add(bacross); ybdown.add(Box.createVerticalStrut(5)); ybdown.add(new JSeparator()); ybdown.add(Box.createVerticalStrut(5));// width bacross = Box.createHorizontalBox(); TextFieldInt xwidth = new TextFieldInt(); xwidth.setValue(WID); xwidth.setPreferredSize(dim); xwidth.setMaximumSize(dim); bacross.add(xwidth); bacross.add(new JLabel(" Graph Width ")); bacross.add(Box.createHorizontalGlue()); xbdown.add(bacross); xbdown.add(Box.createVerticalStrut(5)); // height bacross = Box.createHorizontalBox(); TextFieldInt yheight = new TextFieldInt(); yheight.setValue(HGT); yheight.setPreferredSize(dim); yheight.setMaximumSize(dim); bacross.add(yheight); bacross.add(new JLabel(" Graph Height ")); bacross.add(Box.createHorizontalGlue()); ybdown.add(bacross); ybdown.add(Box.createVerticalStrut(5)); // graph colour Box graphBox = Box.createVerticalBox(); bacross = Box.createHorizontalBox(); final JButton button_colour = new JButton(); button_colour.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Color newColour= JColorChooser.showDialog(null, "Graph Colour", graph_colour); if(newColour != null) { graph_colour = newColour; button_colour.setBackground(graph_colour); } } }); final Dimension buttonSize = new Dimension(22,24); button_colour.setBackground(graph_colour); button_colour.setPreferredSize(buttonSize); button_colour.setMaximumSize(buttonSize); bacross.add(button_colour); bacross.add(new JLabel(" Graph Colour ")); bacross.add(Box.createHorizontalGlue()); graphBox.add(bacross); graphBox.add(Box.createVerticalStrut(5));// graph line size bacross = Box.createHorizontalBox(); if(graph_line == null) { graph_line = new TextFieldFloat(); graph_line.setValue(1.f); } graph_line.setPreferredSize(dim); graph_line.setMaximumSize(dim); bacross.add(graph_line); bacross.add(new JLabel(" Graph Line Width ")); bacross.add(Box.createHorizontalGlue()); graphBox.add(bacross); // main title label Box bdown = Box.createVerticalBox(); bacross = Box.createHorizontalBox(); if(maintitle_field == null) maintitle_field = new JTextField(maintitle); dim = new Dimension(260,20); maintitle_field.setPreferredSize(dim); maintitle_field.setMaximumSize(dim); bacross.add(maintitle_field); bacross.add(new JLabel(" Main Title")); bacross.add(Box.createHorizontalGlue()); bdown.add(bacross); bdown.add(Box.createVerticalStrut(5)); bdown.add(new JSeparator()); bdown.add(Box.createVerticalStrut(5)); JPanel pane = new JPanel(new BorderLayout()); pane.add(bdown, BorderLayout.NORTH); pane.add(xbdown, BorderLayout.CENTER); pane.add(ybdown, BorderLayout.EAST); pane.add(graphBox, BorderLayout.SOUTH); Object[] options = { "OK", "APPLY", "CANCEL"}; int select = 1; while(select == 1) { select = JOptionPane.showOptionDialog(null, pane, "Graph Options", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if(select < 2) { if(WID != (int)xwidth.getValue() || HGT != (int)yheight.getValue()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -