CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
1 Fac.Scienze – Università di Trento Programmazione 2 - Marco Ronchetti Events Gestione degli eventi.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser.
Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Intro to Java Java Applications Swing Class Graphics.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Objectives of This Session
1 Review Polymorphism –“having many forms” –Helps build extensible systems Dynamic/late binding –Implements polymorphic processing of objects –Use superclass.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Layout Managers Arranges and lays out the GUI components on a container.
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Basics of GUI Programming Chapter 11 and Chapter 22.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Introducing, the JFrame Gives us a work area beside System.out.println.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Component-Based Software Engineering GUI Basics Paul J Krause.
Eleventh Graphics in Java.
A Quick Java Swing Tutorial
GUI building with the AWT
Welcome To java
Introduction to Graphics
Java Swing.
Abstract Window ToolKit (AWT)
Introduction to Computing Using Java
Graphics Programming - Frames
CS431 ws99 Half Text Half Graphics
Programming Graphical User Interface (GUI)
GUI building with the AWT
Graphical User Interface
Presentation transcript:

CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;

CSTP FS99CS423 (cotter)2 Graphics in Applications Must include a main() method Must extend the AWT Frame class

CSTP FS99CS423 (cotter)3 awt Frame Inheritance Object Component Container Window Frame Class MyNewApp extends Frame

CSTP FS99CS423 (cotter)4 HellowWorldWindow import java.awt.*; public class HelloWorldWindow extends Frame { public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent (evt); } public void paint(Graphics g) { g.drawString(“A Windows version of Hello, World”,25,75); } public static void main(String[] args) { Frame f = new HelloWorldWindow(); f.setSize(300, 200); f.show(); }

CSTP FS99CS423 (cotter)5 HelloWorldWindow Output

CSTP FS99CS423 (cotter)6 public void paint (Graphics g) Provides an initial presentation of Graphics Works on Graphics object –(like device context). –Place to store settings for screen output (text, images. etc.) Must be regenerated following changes.

CSTP FS99CS423 (cotter)7 Event Driven Programming Operating System recognizes an event Sends a signal to appropriate object Object receives event notification and call appropriate function public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent (evt);

CSTP FS99CS423 (cotter)8 Component.handleEvent( ) action gotFocus lostFocus keyDown keyUp mouseEnter mouseExit mouseMove mouseDrag mouseDown mouseUp

CSTP FS99CS423 (cotter)9 Accessing super class methods Object will execute method version that is closest to it in the class hierarchy. If a method is extended, only the extended behaviour is given in the extension. To access previous versions, use: super.method ( ); super.handleEvent (evt);

CSTP FS99CS423 (cotter)10 Add Font Control /* modify paint method to specify font */ public void paint (Graphics g) { Font f = new Font(“Helvetica”, Font.BOLD, 14); g.setFont (f); g.drawString(“A Windows version of Hello, World”, 75, 100); }

CSTP FS99CS423 (cotter)11 HelloWorldWindow Output 2

CSTP FS99CS423 (cotter)12 Swing Components Defined in package javax.swing –Pure Java components AWT components tied to local platform GUI –UNIX java windows look like X windows –Windows java windows look like windows windows (??) –etc. Swing defines a common look and feel for Java.

CSTP FS99CS423 (cotter)13 Swing Components Derive from awt classes java.lang.Object -> java.awt.Component -> jav.awt.Container -> javax.swing.Jcomponent Methods overloaded in swing to allow different behaviour

CSTP FS99CS423 (cotter)14 Java 1.2 Graphics // From: Java: How to Program - Deitel & Deitel // Fig. 11.5: ShowColors.java Demonstrating Colors import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ShowColors extends JFrame { public ShowColors() { super( "Using colors" ); setSize( 400, 130 ); show(); }

CSTP FS99CS423 (cotter)15 ShowColors.java public void paint( Graphics g ) { // set new drawing color using integers g.setColor( new Color( 255, 0, 0 ) ); g.fillRect( 25, 25, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(),130,40); // set new drawing color using floats g.setColor( new Color( 0.0f, 1.0f, 0.0f ) ); g.fillRect( 25, 50, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(),130,65); // set new drawing color using static Color objects g.setColor( Color.blue ); g.fillRect( 25, 75, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(),130,90);

CSTP FS99CS423 (cotter)16 ShowColors.java // display individual RGB values Color c = Color.magenta; g.setColor( c ); g.fillRect( 25, 100, 100, 20 ); g.drawString( "RGB values: " + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue(), 130, 115 ); } public static void main( String args[] ) { ShowColors app = new ShowColors(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); /*end of main ()*/ } /*end of paint*/ } // end of class

CSTP FS99CS423 (cotter)17 ShowColors.java

CSTP FS99CS423 (cotter)18 Object Layout BorderLayout (default for Frames) FlowLayout (default for Panels) GridLayout GridbagLayout CardLayout Fixed: –object.reshape (x, y, width, height);

CSTP FS99CS423 (cotter)19 Display Objects Button Checkbox CheckboxGroup TextField TextArea etc.

CSTP FS99CS423 (cotter)20 TextTest Example

CSTP FS99CS423 (cotter)21 import java.awt.*; public class MyTextTest extends Frame { public MyTextTest() { setTitle("MyTextTest"); Panel p = new Panel(); p.setLayout(new FlowLayout()); p.add(new Button("Tick")); p.add(new Button("Set time")); hourField = new TextField("12", 3); p.add(hourField); minuteField = new TextField("00", 3); p.add(minuteField); timeField = new TextField("", 12); p.add(timeField); add("North", p); } MyTextTest

CSTP FS99CS423 (cotter)22 MyTextTest public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } public boolean action(Event evt, Object arg) { if (arg.equals("Tick")) { int minutes = Integer.parseInt(minuteField.getText()); minutes += 1; String min = String.valueOf(minutes); minuteField.setText(min); }

CSTP FS99CS423 (cotter)23 MyTextTest else if (arg.equals("Set time")) { int hours = Integer.parseInt(hourField.getText()); int minutes = Integer.parseInt(minuteField.getText()); String tim = hourField.getText()+ ":" + minuteField.getText(); timeField.setText(tim); } else return false; return true; } private TextField hourField; private TextField minuteField; private TextField timeField; public static void main(String[] args) { Frame f = new MyTextTest(); f.resize(400, 100); f.show(); }