//LinearApplet.java import java.util.*; import java.text.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class LinearApplet extends JApplet implements ActionListener, ItemListener { JLabel xLowLabel, xHighLabel, yLowLabel, yHighLabel, tickIntLabel; JTextField xLowField, xHighField, yLowField, yHighField, tickIntField; JLabel[] mLabels, bLabels, zeroLabels, x1Labels, y1Labels, x2Labels, y2Labels; JTextField[] mFields, bFields, zeroFields, x1Fields, y1Fields, x2Fields, y2Fields; JRadioButton intersectionsRadioButton; JRadioButton[][] choiceRadios; ButtonGroup[] choiceRadioGroups; String lineChoice[]; JButton zoomInButton, zoomOutButton; public final int MaxNumLines=5; Linear[] linearArray; //array of Linear objects Color colors[] = {Color.BLUE,Color.RED,Color.GREEN,Color.CYAN,Color.MAGENTA, Color.PINK,Color.YELLOW,Color.ORANGE}; // double m, b; double xLo, xHi, yLo, yHi, tickInt, scale; boolean showIntersections=true; DecimalFormat d2; public final int westPanelWidth=250; public void init() { d2 = new DecimalFormat ("0.00"); Container container = getContentPane(); container.setLayout(null); //** no Layout Manager JPanel gridParamsPanel = new JPanel(); //graph parameters //Box gridParamsPanelLabels = new Box(BoxLayout.Y_AXIS); //Box gridParamsPanelFields = new Box(BoxLayout.Y_AXIS); gridParamsPanel.setLayout( new GridLayout(5,2) ); gridParamsPanel.setBackground(Color.PINK); gridParamsPanel.setBounds(westPanelWidth-100,getHeight()-100, 100,100); gridParamsPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED)); //gridParamsPanelLabels.setBounds(0,getHeight()-100, 50,100); //gridParamsPanelFields.setBounds(50,getHeight()-100, 50,100); //container.add(gridParamsPanelLabels); //container.add(gridParamsPanelFields); container.add(gridParamsPanel); JPanel westPanel = new JPanel(); //westPanel.setLayout( new GridLayout(10,4) ); westPanel.setBackground(Color.LIGHT_GRAY); westPanel.setBounds(0,0, westPanelWidth,getHeight()-200); container.add(westPanel); //container.add(westPanel,BorderLayout.WEST); JPanel miscPanel = new JPanel(); miscPanel.setBackground(Color.YELLOW); miscPanel.setBounds(0,getHeight()-200, westPanelWidth,200); container.add(miscPanel); Box[] lineBoxes = new Box[MaxNumLines]; Box[] pointsBoxes = new Box[MaxNumLines]; Box[] choiceBoxes = new Box[MaxNumLines]; mLabels = new JLabel[MaxNumLines]; bLabels = new JLabel[MaxNumLines]; zeroLabels = new JLabel[MaxNumLines]; mFields = new JTextField[MaxNumLines]; bFields = new JTextField[MaxNumLines]; zeroFields = new JTextField[MaxNumLines]; x1Labels = new JLabel[MaxNumLines]; y1Labels = new JLabel[MaxNumLines]; x2Labels = new JLabel[MaxNumLines]; y2Labels = new JLabel[MaxNumLines]; x1Fields = new JTextField[MaxNumLines]; y1Fields = new JTextField[MaxNumLines]; x2Fields = new JTextField[MaxNumLines]; y2Fields = new JTextField[MaxNumLines]; choiceRadios = new JRadioButton[MaxNumLines][]; for (int i=0; ixHiTry || yLoTry>yHiTry || tickIntTry<=0) { JOptionPane.showMessageDialog(null, "Bad plotting coordinates", "Linear plotting: Invalid input", JOptionPane.ERROR_MESSAGE); return; } xLo = xLoTry; xHi = xHiTry; yLo = yLoTry; yHi = yHiTry; tickInt = tickIntTry; repaint(); } else if ( event.getSource() == zoomInButton ) { scale = .9; //shrink the grid by 10% xLo *= scale; xHi *= scale; yLo *= scale; yHi *= scale; xLowField.setText(""+d2.format(xLo)); xHighField.setText(""+d2.format(xHi)); yLowField.setText(""+d2.format(yLo)); yHighField.setText(""+d2.format(yHi)); repaint(); } else if ( event.getSource() == zoomOutButton ) { scale = 1.1; //grow the grid by 10% xLo *= scale; xHi *= scale; yLo *= scale; yHi *= scale; xLowField.setText(""+d2.format(xLo)); xHighField.setText(""+d2.format(xHi)); yLowField.setText(""+d2.format(yLo)); yHighField.setText(""+d2.format(yHi)); repaint(); } else { //loop to find which textfield sent event... ??is there a better way? i = 0; while (i=xLo; tick-=tickInt) g.drawLine((int)(tick * xScale - xOffset),0,(int)(tick * xScale - xOffset),h); for (double tick=tickInt; tick<=yHi; tick+=tickInt) g.drawLine(westPanelWidth,h-(int)(tick * yScale - yOffset),getWidth(),h-(int)(tick * yScale - yOffset)); for (double tick=-tickInt; tick>=yLo; tick-=tickInt) g.drawLine(westPanelWidth,h-(int)(tick * yScale - yOffset),getWidth(),h-(int)(tick * yScale - yOffset)); g.setColor(Color.BLACK); g.drawLine(westPanelWidth,xAxis,getWidth(),xAxis ); //x axis g.drawLine(yAxis,0,yAxis,h); //y axis for (double tick=tickInt; tick<=xHi; tick+=tickInt) g.drawLine((int)(tick * xScale - xOffset),xAxis-3,(int)(tick * xScale - xOffset),xAxis+3); for (double tick=-tickInt; tick>=xLo; tick-=tickInt) g.drawLine((int)(tick * xScale - xOffset),xAxis-3,(int)(tick * xScale - xOffset),xAxis+3); for (double tick=tickInt; tick<=yHi; tick+=tickInt) g.drawLine(yAxis-3,h-(int)(tick * yScale - yOffset),yAxis+3,h-(int)(tick * yScale - yOffset)); for (double tick=-tickInt; tick>=yLo; tick-=tickInt) g.drawLine(yAxis-3,h-(int)(tick * yScale - yOffset),yAxis+3,h-(int)(tick * yScale - yOffset)); for (int i=0; i