import javax.swing.*; //import com.sun.java.swing.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.text.*; public class AspireGraph extends JPanel { int numberDivisions = 5; JLabel divisions[][] = new JLabel[numberDivisions][2]; int width, height; int tickHeight = 5; int leftMargin, rightMargin, topMargin, bottomMargin; String names[]; double data[][]; int count[]; double xmin, xmax, ymin, ymax; boolean drawLine; double b, m; DecimalFormat format; Container container; JLabel xAxisLabel, yAxisLabel; Image yAxisLabelIm; AspireGraph(String names[], double data[][], int count[]) { super(false); this.names = names; this.data = data; this.count = count; container = this; setLayout(null); setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(1),BorderFactory.createBevelBorder(0)),BorderFactory.createBevelBorder(1))); setBackground(Color.white); // set number formatting format = new DecimalFormat(); format.setMaximumFractionDigits(2); for(int i=0;ixmin) xmax = newxmax; setupTickLabels(); repaint(); } public void returnValue(int i) {} } class resetYminListener extends MouseAdapter implements CoverableContainer { public void mousePressed(MouseEvent e) { JModalPanel j = new JModalPanel(container, this,"What is the new value for y-min?", ""+ymin); } public void returnValue(String text) { double newymin = ymin; try { newymin = (new Double(text)).doubleValue(); } catch(Exception ex) { return; } if(newyminymin) ymax = newymax; setupTickLabels(); repaint(); } public void returnValue(int i) {} } public void paint(Graphics g) { if(width == 0) { setMargins(); initializeLabels(); } paintComponents(g); drawAxes(g); drawLine(g); drawPoints(g); if(!names[1].equals(yAxisLabel.getText()) || !names[0].equals(xAxisLabel.getText())) setupTitles(); g.drawImage(yAxisLabelIm, 15, (height-bottomMargin+topMargin)/2-yAxisLabelIm.getHeight(this)/2, this); } public void paintComponents(Graphics g) { g.setColor(Color.white); g.fillRect(0,0,width,height); Component components[] = getComponents(); for(int i=0; i= ymin) && (y <= ymax)) { xused[0] = x; yused[0] = y; } else { y = ymax; x = (y-b)/m; if((x > xmin) && (x < xmax)) { xused[0] = x; yused[0] = y; } else { yused[0] = ymin; xused[0] = (yused[0]-b)/m; } } x = xmax; y = m*x+b; if((y >= ymin) && (y <= ymax)) { xused[1] = x; yused[1] = y; } else { y = ymin; x = (y-b)/m; if((x > xmin) && (x < xmax)) { xused[1] = x; yused[1] = y; } else { yused[1] = ymax; xused[1] = (yused[1]-b)/m; } } if((xused[0] < xmin) || (xused[0] > xmax) || (yused[0] < ymin) || (yused[0] > ymax) || (xused[1] < xmin) || (xused[1] > xmax) || (yused[1] < ymin) || (yused[1] > ymax)) return; gx1 = (int)(leftMargin + (xused[0]-xmin)/(xmax-xmin)*(width-rightMargin-leftMargin)); gy1 = (int)(height - bottomMargin - (yused[0]-ymin)/(ymax-ymin)*(height-bottomMargin-topMargin)); gx2 = (int)(leftMargin + (xused[1]-xmin)/(xmax-xmin)*(width-rightMargin-leftMargin)); gy2 = (int)(height - bottomMargin - (yused[1]-ymin)/(ymax-ymin)*(height-bottomMargin-topMargin)); g.setColor(Color.blue); g.drawLine(gx1,gy1,gx2,gy2); } public void drawPoints(Graphics g) { int x, y; if(count[0] < 2) return; for(int i=0; i xmax) || (data[1][i] < ymin) || (data[1][i] > ymax)) continue; x = (int)(leftMargin + (data[0][i]-xmin)/(xmax-xmin)*(width-rightMargin-leftMargin))-1; y = (int)(height-bottomMargin - (data[1][i]-ymin)/(ymax-ymin)*(height-bottomMargin-topMargin))-1; g.setColor(new Color(0, 102, 51)); g.fillOval(x,y,3,3); } } public void drawAxes(Graphics g) { int x1, x2, y1, y2; g.setColor(Color.black); g.drawLine(leftMargin,topMargin,leftMargin,height-bottomMargin); g.drawLine(leftMargin,height-bottomMargin,width-rightMargin,height-bottomMargin); y1 = height-bottomMargin-tickHeight; y2 = height-bottomMargin+tickHeight; for(int i=0;i xmax) xmax = data[0][i]; if(data[1][i] < ymin) ymin = data[1][i]; if(data[1][i] > ymax) ymax = data[1][i]; } if(ymax == ymin) { ymin -= 1.0; ymax += 1.0; } if(xmax == xmin) { xmin -= 1.0; xmax += 1.0; } setupTickLabels(); } public void setupTickLabels() { int x, y; for(int i=0;i maxStringLength) { names[0] = names[0].substring(0, maxStringLength); names[0] += "..."; xAxisLabel.setText(names[0]); } xAxisLabel.setSize(xAxisLabel.getPreferredSize()); xAxisLabel.setLocation((width-rightMargin+leftMargin-xAxisLabel.getSize().width)/2,height-2*xAxisLabel.getSize().height); yAxisLabel.setText(names[1]); if(names[1].length() > maxStringLength) { names[1] = names[1].substring(0, maxStringLength); names[1] += "..."; yAxisLabel.setText(names[1]); } yAxisLabel.setSize(yAxisLabel.getPreferredSize()); yAxisLabel.setLocation(0,0); if(yAxisLabelIm != null) { yAxisLabelIm.flush(); yAxisLabelIm = null; } yAxisLabelIm = createImage(yAxisLabel.getPreferredSize().width, yAxisLabel.getPreferredSize().height); Graphics tmpG = yAxisLabelIm.getGraphics(); yAxisLabel.setVisible(true); tmpG.setClip(0,0,yAxisLabel.getPreferredSize().width, yAxisLabel.getPreferredSize().height); yAxisLabel.paint(tmpG); yAxisLabel.setVisible(false); tmpG.dispose(); yAxisLabelIm = rotateImage(yAxisLabelIm); } public void setMargins() { width = getSize().width; height = getSize().height; leftMargin = width/6; rightMargin = width/15; topMargin = height/15; bottomMargin = height/6; } public void setupLine(double b, double m) { drawLine = !drawLine; this.b = b; this.m = m; repaint(); } public double format(double number) { try { number = format.parse(format.format(number)).doubleValue(); } catch(ParseException pe) { System.out.println("Error: " + pe.getMessage()); pe.printStackTrace(); } return number; } public Image rotateImage(Image im) { int imWidth = im.getWidth(this); int imHeight = im.getHeight(this); int srcPixels[] = new int[imWidth*imHeight]; int newPixels[] = new int[imWidth*imHeight]; PixelGrabber pg = new PixelGrabber(im, 0, 0, imWidth, imHeight, srcPixels, 0, imWidth); try{ pg.grabPixels(); } catch(InterruptedException e) {} for(int i=0; i