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 Creating Hierarchical Window frame control panel x TextField y TextField New Point Button Container Component view panel Quit Button AWT Classes Component Container Frame Panel TextField Button Creating Hierarchical Structure Frame frame new Frame Points Plotter frame Panel control new Panel frame add control frame add new Panel control add new Button New Point x control add new Button Quit TextField control panel y TextField New Point Button control add new TextField 0 control add new TextField 0 frame setSize FRAME WIDTH FRAME HEIGHT frame setVisible true view panel Quit Button Hierarchical Structure Vs Layout frame control panel x TextField y TextField New Point Button view panel Quit Button Hierarchical Structure Vs Layout frame control panel x TextField y TextField New Point Button view panel Quit Button Layout Managers BorderLayout control panel GridLayout x TextField frame y TextField New Point Button view panel Quit Button BorderLayout North North West West Center Center East East South South 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 Laying out Hierarchical Structure Frame frame new Frame Points Plotter Default frame setLayout new BorderLayout Will use Panel control new Panel subclasses Frame add control frame add control BorderLayout WEST control Frame add frame add new new Panel Panel BorderLayout CENTER panel frame view panel control setLayout new GridLayout 4 1 control add new Button New Point x y TextField New Point Button control add new Button Quit TextField control add new TextField 0 control add new TextField 0 frame setSize FRAME WIDTH FRAME HEIGHT frame setVisible true Quit Button Implementing Graphical UI Create the window React to user input Display output frame control panel x TextField y TextField New Point Button view panel Quit Button Reacting to New Point Click Listenable Listener addActionListener New Point Button NewPoint Listener actionPerformed PointHistory Model addElement Button Clicked newPointButton addActionListener newPointListener Action Listener Code import declarations public class 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 public void actionPerformed ActionEvent ae pointHistory addElement Integer parseInt xField getText Integer parseInt yField getText Implementing Graphical UI Create the window React to user input Display output frame control panel x TextField y TextField New Point Button view panel Quit Button Displaying Output paint Graphics g repaint Invoke Graphics Operations Panel Resized paint Graphics g Panel Uncovered Panel extends View Panel 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 public void paint Graphics g drawBars g void drawBars Graphics g if pointHistory null return final int 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 MVC in a Graphical Application Controller AConsoleController Controller AnAWTController Composer Single Main Class Model APointHistoryModel View AConsoleView View ABarChartView Controller AnAWTController View APlotterView Controller ASwingController View APlotterView MVC in a Graphical Application Composer Single Main Class Controller AnAWTController View ABarChartView Model APointHistoryModel frame Same as Same as control panel NewPoint Listener x TextField y TextField New Point Button view panel Quit Button View Panel View paint Graphics g Invoke Graphics Operations repaint Panel extends Panel Resized paint Graphics g MVC View Panel View Panel Uncovered Model updated 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 Panel Controller Create control components text fields buttons Add components Panel Create component listeners Register listeners with components extends Constructor MVC Controller Controller 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
View Full Document