Bar Graph Design

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

Bar Graph Design. Left-side/Right-side. mechanical processing. creative, abstract reasoning. Why Is He Doing This?. The Problem:. Two frames (Views) and a data structure (Model). Observer Pattern:. Observer Pattern Times Four:. - PowerPoint PPT Presentation

Text of Bar Graph Design

  • Bar Graph Design

  • Left-side/Right-sidemechanicalprocessingcreative, abstract reasoning

  • Why Is He Doing This?

  • The Problem:Two frames (Views) and a data structure (Model).Observer Pattern:

  • Observer Pattern Times Four:JTextField: ActionListener receives event after you hit .The ActionListener updates the Data (Model) object.JTextFieldanonymous objectActionListeneraddActionListeneractionPerformed

  • Observer Pattern Times Four:Data: Is observed by the BarGraph (ChangeListener) for any changes to the model.DataBarGraphChangeListenerattachstateChanged

  • Observer Pattern Times Four:BarGraph: MouseListener receives event when you click on the bar graph.mousePressed() moves the bar and updates Data object.BarGraphMouse AdapterMouseListeneraddMouseListenermousePressed

  • Observer Pattern Times Four:Data: Is observed by TextFrame (ChangeListener) for any changes to data. DataTextFrameChangeListenerattachstateChanged

  • Class Diagram:

  • Sequence DiagramEnter new number

  • Sequence Diagram:Click on BarGraph:

  • Data.javapublic class Data {public Data(ArrayList d) { data = d; listeners = new ArrayList(); }

    public ArrayList getData() { return (ArrayList) (data.clone()); }

    public void attach(ChangeListener c) { listeners.add(c); }

    }

  • Data.javapublic class Data { public void update(int loc, double val) { if (val > MAX) val = MAX; if (val < 0) val = 0; data.set(loc, new Double(val)); // for each listener, let the listener know a change has occurred for (ChangeListener l : listeners) { l.stateChanged(new ChangeEvent(this)); } }

    public Double get(int i) { return data.get(i); } private ArrayList data; private ArrayList listeners; public static final double MAX = 100.0;

    }

  • TextFrame:public class TextFrame extends JFrame implements ChangeListener {public TextFrame (Data md) {d = md; ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent e) { JTextField c = (JTextField) e.getSource(); int ndx = findBox(c); if (ndx >= 0) d.update(ndx,Double.parseDouble(c.getText().trim())); } };

    JPanel tPanel = new JPanel();tPanel.setLayout(new BoxLayout(tPanel,BoxLayout.PAGE_AXIS));

    tf = new JTextField[5];for (int i = 0; i < 5; i++) {tf[i] = new JTextField(10);tf[i].addActionListener(l);tPanel.add(tf[i]);}add(tPanel, BorderLayout.CENTER);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);pack();setVisible(true);}...}

  • TextFrame:@Overridepublic void stateChanged(ChangeEvent e) {// how I notify the text frame if the bar graph changesfor (int i = 0; i < 5; i++){double d1 = d.get(i).doubleValue();String s = tf[i].getText().trim();double d2;try {d2 = Double.parseDouble(s);} catch(NumberFormatException nfe ) {d2 = 0.0;}if (Math.abs(d1 - d2) > 0.01)tf[i].setText(new Double(d1).toString());}}public int findBox(JTextField t) {int i = 0;while (i < 5) {if (t == tf[i])return i;i++;}return -1;}private Data d; private JtextField[ ] tf;}

  • BarGraph:public class BarGraph extends JFrame implements ChangeListener { public BarGraph(Data md) { d = md; setLocation(0,300); Icon barIcon = new Icon() { public int getIconWidth() { return WIDTH; } public int getIconHeight() { return HEIGHT; }

    public void paintIcon(Component c, Graphics g , int x, int y) { Graphics2D g2 = (Graphics2D) g; Color cl = g2.getColor(); for (int i = 0; i < 5; i++) { Rectangle2D.Double rectangle = new Rectangle2D.Double(0, (HEIGHT/6)*i, WIDTH*(d.get(i).doubleValue()/Data.MAX), (HEIGHT/6)); g2.setPaint(Color.cyan); g2.fill(rectangle); g2.setColor(Color.black); g2.draw(rectangle); } Rectangle2D.Double rectangle = new Rectangle2D.Double(0,375,WIDTH,(HEIGHT/6)); g2.setPaint(cl); g2.fill(rectangle);g2.setColor(cl);g2.draw(rectangle); } }; // end of barIcon panel ...}

  • BarGraph:public class BarGraph extends JFrame implements ChangeListener {

    public BarGraph(Data md) { ... add(new JLabel(barIcon)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MouseListener m = new MouseAdapter() {public void mousePressed(MouseEvent e){ Point2D p = e.getPoint(); double dy = p.getY(); int barToMove = (int)(dy / BARHEIGHT); if (barToMove < 0 || barToMove > 4) return; double dx = p.getX(); double newV = (dx/WIDTH)* Data.MAX; d.update(barToMove, newV); } }; addMouseListener(m); pack(); setVisible(true); } // end constructor

    } // end class