Electrical and Computer Engineering Object-Oriented Topic : Graphics GUI Part I Maj Joel Young Joel.Young@afit.edu 15-Sep-03 Maj Joel Young
A Brief History Lesson AWT Abstract Window Toolkit Implemented with native platform GUI toolkit Lowest-common denominator for all platforms Platform dependent bugs Swing GUI widgets painted onto blank windows Only native support was that required to display window Look-and-feel adaptable to Windows, Motif, Metal, GTK (new) More consistent behavior across platforms 2
Model-View-Controller Model: The data that drives what is to be shown (application semantics) View: How the data is shown to the user (output) Controller: Mechanism that reacts to the user (user input) View Model (data) Controller Swing Container, Controls 3
GUI in Java Often easier to think in terms of containers and controls Container: An area of the display that can hold visual controls and other containers Controls: A visual display item that can be manipulated by the user directly (buttons, fields, etc.) controls containers Enter Name: text field 4
Containers Basic window container is a JFrame Has a title bar with icon, minimize, maximize, close buttons Is automatically given an additional container called a content pane Title Here JFrame JPanel (content pane) 5
Frame import javax.swing.*; public class SimpleFrameTest public static void main(string[] args) SimpleFrame frame = new SimpleFrame(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.show(); class SimpleFrame extends JFrame public SimpleFrame() setsize(width, HEIGHT); public static final int WIDTH = 300; public static final int HEIGHT = 200; 6
Containers Basic workhorse container is the JPanel Used to group controls, other containers Can have its own border Inherits from class Jcomponent To draw on a panel Define class that extends JPanel Override the paintcomponent method Title Here Other JPanels JFrame JPanel (content pane) 7
Containers class NotHelloWorldPanel extends JPanel public void paintcomponent(graphics g) super.paintcomponent(g); g.drawstring("not a Hello, World program", MESSAGE_X, MESSAGE_Y); public static final int MESSAGE_X = 75; public static final int MESSAGE_Y = 100; 8
2D Shapes Requires Graphics2D class Subclass of Graphics Stores context information for graphics rendering Current color, brush width, etc. Use draw method to render a shape public void paintcomponent(graphics g) super.paintcomponent(g); Graphics2D g2 = (Graphics2D)g;... Rectangle2D rect = new Rectangle2D.Double(leftX, topy, width, height); g2.draw(rect); Basic shapes Line2D, Rectangle2D, Ellipse2D Outline or filled Core Java Examples 9
Homework Look in L:\eng students\csce093\javastuff\v1ch7 and run the examples in there Look in L:\eng students\csce093\course Notes\Stacks and Parsing Arithmetic Expressions.pdf Write a program that uses a stack. Push some numbers on to the stack, pop them off, clear the stack, etc. Write a program that takes the first argument static public void main(string args[]) Evaluator e = new Evaluator(); System.out.println(e.eval(args[0])); So Java eval 6 * 7 + 4 will yield 46. STEAL CODE FROM the Stacks pdf... Cooperate to Graduate Your code better be your own tho, or stolen from an outside source 10