330 likes | 537 Views
Toolkits. Console I/O Vs Toolkits Containers and Components Event Listeners Toolkit Classes Frames Panel TextField Buttons BorderLayout. Console Vs Graphical User Interface. Hand-crafted hierarchical Vs Automatic Linear Window Graphics and Text Vs Text-Only Editing Vs Transcript
E N D
Toolkits • Console I/O Vs Toolkits • Containers and Components • Event Listeners • Toolkit Classes • Frames • Panel • TextField • Buttons • BorderLayout
Console Vs Graphical User Interface • Hand-crafted hierarchical Vs Automatic Linear Window • Graphics and Text Vs Text-Only • Editing Vs Transcript • User-Driven Vs Program Driven • Point and Click Vs Typing
Implementing Graphical UI • Create the window • React to user input • Display output
Container frame Component view panel control panel x TextField y TextField New Point Button Quit Button Creating Hierarchical Window
AWT Classes Component Container TextField Button Frame Panel
frame view panel control panel x TextField y TextField New Point Button Quit Button Creating Hierarchical Structure Frame frame = new Frame("Points Plotter"); Panel control = new Panel(); frame.add(control); frame.add(new Panel()); control.add(new Button(“New Point”)); control.add(new Button(“Quit”); control.add(new TextField(“0”)); control.add(new TextField(“0”)); frame.setSize ( FRAME_WIDTH, FRAME_HEIGHT) frame.setVisible ( true)
frame view panel control panel x TextField y TextField New Point Button Quit Button Hierarchical Structure Vs Layout
frame view panel control panel x TextField y TextField New Point Button Quit Button Hierarchical Structure Vs Layout
frame view panel control panel x TextField y TextField New Point Button Quit Button Layout Managers BorderLayout GridLayout
North West East Center North South West Center East South BorderLayout frame.setLayout(new BorderLayout()); frame.add( new Panel(), BorderLayout.CENTER); frame.add(new Panel(), BorderLayout.WEST); frame.add(new Panel(), BorderLayout.EAST); frame.add(new Panel(), BorderLayout.NORTH); frame.add(new Panel(), BorderLayout.SOUTH);
GridLayout controller.setLayout(new GridLayout(2,3)); controller.add(new TextField()); controller.add(new TextField()); controller.add(new TextField()); controller.add(new TextField());
Default frame Will use subclasses view panel control panel x TextField y TextField New Point Button Quit Button Laying out Hierarchical Structure Frame frame = new Frame("Points Plotter"); frame.setLayout(new BorderLayout()); Panel control = new Panel(); Frame.add(control); frame.add(control, BorderLayout.WEST); Frame.add(new Panel()); frame.add(new Panel(), BorderLayout.CENTER); control.setLayout(new GridLayout(4, 1 )); control.add(new Button(“New Point”)); control.add(new Button(“Quit”)); control.add(new TextField(“0”)); control.add(new TextField(“0”)); frame.setSize ( FRAME_WIDTH, FRAME_HEIGHT); frame.setVisible ( true);
frame view panel control panel x TextField y TextField New Point Button Quit Button Implementing Graphical UI • Create the window • React to user input • Display output
Listenable Listener New Point Button PointHistoryModel addElement() Reacting to New Point Click NewPoint Listener addActionListener() actionPerformed() Button Clicked newPointButton.addActionListener(newPointListener);
Action Listener Code // import declarations …. publicclass ANewPointActionListener implements ActionListener { PointHistory pointHistory; TextField xField; TextField yField; public ANewPointActionListener (PointHistory thePointHistory, TextField theXField, TextField theYField) { pointHistory = thePointHistory; xField = theXField; yField = theYField; } // Called when the New Point button is pressed. publicvoid actionPerformed(ActionEvent ae) { pointHistory.addElement( Integer.parseInt(xField.getText()), Integer.parseInt(yField.getText())); } }
frame view panel control panel x TextField y TextField New Point Button Quit Button Implementing Graphical UI • Create the window • React to user input • Display output
Panel Resized Panel Uncovered Invoke Graphics Operations View Panel Panel extends Displaying Output paint(Graphics g) repaint() paint(Graphics g)
Graphics Operations • drawLine( int x1, int y1, int x2, int y2 ) • drawString( String s, int x, int y ) • drawRect( int x, int y, int width, int height ) • drawOval( int x, int y, int width, int height ) • fillOval( int x, int y, int width, int height) • setColor(Color newColor)
Drawing Bars publicvoid paint(Graphics g) { drawBars(g); } void drawBars (Graphics g) { if (pointHistory == null) return; finalint BAR_WIDTH = 6; Rectangle myBounds = getBounds(); for (int i = 0; i < pointHistory.size(); i++) { PointModel nextPoint = (PointModel) pointHistory.elementAt(i); g.setColor(java.awt.Color.darkGray); g.fillRect(nextPoint.getX() - BAR_WIDTH, myBounds.height - nextPoint.getY(), BAR_WIDTH, nextPoint.getY()); } }
Implementing Graphical UI • Create the window • Use Frames, LayoutManagers, Panels, Buttons, TextFields • React to user input • Define Listeners that call methods in Model • Display output • Override paint() method • Call graphics operations
Displaying Points All four views displayed concurrently and kept in Sync.
Composer: Single Main Class MVC in a Graphical Application Controller: AConsoleController View: AConsoleView Controller: AnAWTController View: ABarChartView Model: APointHistoryModel Controller: AnAWTController View: APlotterView Controller: ASwingController View: APlotterView
frame Controller: AnAWTController View: ABarChartView view panel control panel Composer: Single Main Class Model: APointHistoryModel Same as x TextField y TextField New Point Button Quit Button Same as MVC in a Graphical Application NewPoint Listener
Panel Resized Panel Uncovered MVC View View Panel Panel Model updated View Panel View paint(Graphics g) repaint() Invoke Graphics Operations extends paint(Graphics g) pointHostoryUpdated()
View import java.awt.Rectangle; import shapes.PointModel; public class APointHistoryBarChart extends Panel implements PointHistoryListener { PointHistory pointHistory; public void pointHistoryUpdated(PointHistory thePointHistory) { pointHistory = thePointHistory; repaint(); } // Called by repaint and other methods that refresh the view. public void paint(Graphics g) { drawBars(g); }
View (contd) void drawBars (Graphics g) { if (pointHistory == null) return; final int BAR_WIDTH = 6; Rectangle myBounds = this.getBounds(); for (int i = 0; i < pointHistory.size(); i++) { PointModel nextPoint = (PointModel) pointHistory.elementAt(i); g.setColor(java.awt.Color.darkGray); g.fillRect(nextPoint.getX() - BAR_WIDTH, myBounds.height - nextPoint.getY(), BAR_WIDTH, nextPoint.getY()); } } }
MVC Controller Panel Panel Controller • Create control components (text fields, buttons) • Add components • Create component listeners • Register listeners with components. extends Constructor
import java.awt.GridLayout; import java.awt.Button; import java.awt.TextField; import java.awt.event.ActionListener; public class APointHistoryAWTController extends Panel { int numComponents = 0; PointHistory pointHistory ; public APointHistoryAWTController (PointHistory thePointHistory) { pointHistory = thePointHistory; createComponents (); setLayout(new GridLayout(numComponents,1)); } void createComponents() { TextField xField = createTextField(“0”); add(xField); TextField yField = createTextField(“0”); add(yField); add (createButton ("New Point", new ANewPointActionListener(pointHistory, xField, yField))); add (createButton ("Quit", new AQuitActionListener())); } Controller
Controller (contd) Button createButton(String name, ActionListener listener) { Button button = new Button(name); button.addActionListener(listener); numComponents++; return button; } TextField createTextField(String text) { TextField field = new TextField(text); field.setEditable(true); numComponents++; return field; } }
Composer: Single Main Class Composer main() • Create frame • Set its layout manager • Create model, view(s) and controller(s) • Make views listener of model • Add views and controllers to frame • Set frame size • Make frame visible • Set frame location
2 Barchart Composer • publicclass APointHistoryBarchartComposer { • staticfinalint FRAME_WIDTH = 300; • staticfinalint FRAME_HEIGHT = 300; • publicstaticvoid main (String args[]) { • PointHistoryModel pointHistoryModel = new APointHistoryModel(); • createBarChartEditor(pointHistoryModel); • createBarChartEditor(pointHistoryModel); • } • publicstaticvoid createBarChartEditor (PointHistoryModel pointHistoryModel) { • Frame frame = new Frame("Points Bar Chart"); • frame.add (createAWTController(pointHistoryModel), • BorderLayout.WEST); • frame.add (createBarChart(pointHistoryModel), BorderLayout.CENTER); // important to set this, default seems to be (0,0) • frame.setSize (FRAME_WIDTH, FRAME_HEIGHT); • setLocation (frame); • // important to make frame visible, default is invisible • frame.setVisible(true); • }
Barchart Composer (contd) publicstatic APointHistoryBarChart createBarChart(PointHistoryModel pointHistoryModel) { APointHistoryBarChart pointHistoryView = new APointHistoryBarChart(); // make view an eavesdropper of model pointHistoryModel.addListener(pointHistoryView); return pointHistoryView; } publicstatic APointHistoryAWTController createAWTController(PointHistory pointHistory) { returnnew APointHistoryAWTController(pointHistory); }
Barchart Composer (contd) staticint curX = 0; staticint curY = 0; staticfinalint X_OFFSET = 100; staticfinalint Y_OFFSET = 100; staticvoid setLocation (Frame frame) { frame.setLocation (curX, curY); curX += X_OFFSET; curY += Y_OFFSET; } }