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.

Slides:



Advertisements
Similar presentations
Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
Advertisements

AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Lecture 5 Event Handling.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Topics AWT Classes Window Fundamentals Working with Frame Windows Creating a Frame Window in an Applet Creating a Windowed Program Displaying Information.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Graphics in Java Applications The Graphics Class The Canvas Class The MouseListener.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Objectives of This Session
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
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.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
 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.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Welcome To java
User Interface Programming In Java
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Java Swing.
Graphics Applets By Mr. Dave Clausen
Abstract Window ToolKit (AWT)
Java Applets.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
AWT Components and Containers
APPLET.
Graphics Programming - Frames
Presentation transcript:

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 application in java. Java AWT components are platform- dependent i.e. components are displayed according to the view of operating system. The java.awt package provides classes for AWT API such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

CSI 3125, Preliminaries, page 3 Java AWT Hierarchy

CSI 3125, Preliminaries, page 4 AWT Window Class The two most common windows are Panel, which is used by applets, And Frame, which creates a standard window. Much of the functionality of these windows is derived from their parent classes.

CSI 3125, Preliminaries, page 5 AWT Component class At the top of the AWT hierarchy is the Component class. Component is an abstract class that encapsulates all of the attributes of a visual component that are displayed on the screen It defines over a hundred public methods that are responsible for managing events, such as mouse and keyboard input, positioning and sizing the window, and repainting. A Component object is responsible for remembering the current foreground and background colors and the currently selected text font.

CSI 3125, Preliminaries, page 6 AWT Container class The Container class is a subclass of Component. A container is responsible for laying out (that is, positioning) any components that it contains. It does this through the use of various layout managers

CSI 3125, Preliminaries, page 7 AWT Panel class The Panel class is a subclass of Container. It doesn’t add any new methods; it simply implements Container. Panel is a window that does not contain a title bar, menu bar, or border. Other components can be added to a Panel object by its add( ) method (inherited from Container). Once these components have been added, you can position and resize them manually using the setLocation( ), setSize( ), or setBounds( ) methods defined by Component.

CSI 3125, Preliminaries, page 8 AWT Frame class Frame encapsulates what is commonly thought of as a “window.” It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners. When a Frame window is created by a program rather than an applet, a normal window is created.

CSI 3125, Preliminaries, page 9 Frame Window Frame’s constructors: Frame( ) Frame(String title) The first form creates a standard window that does not contain a title. The second form creates a window with the title specified by title.

CSI 3125, Preliminaries, page 10 Frame Window setSize( ) method is used to set the dimensions of the window. void setSize(int newWidth, int newHeight) After a frame window has been created, it will not be visible until you call setVisible( ). void setVisible(boolean visibleFlag) setTitle( ) used to set title void setTitle(String newTitle)

CSI 3125, Preliminaries, page 11 Frame Window prog import java.awt.*; class frame1 extends Frame { frame1() { setSize(500,500); setVisible(true); } public static void main(String args[]){ frame1 f=new frame1(); }

CSI 3125, Preliminaries, page 12 Frame Window Closing a Frame Window By calling setVisible(false). Implement the windowClosing( ) method of the WindowListener interface. Inside windowClosing( ), call setVisible(false)

CSI 3125, Preliminaries, page 13 Frame Closing Prog import java.awt.*; import java.awt.event.*; class frame1 extends Frame { frame1() { addWindowListener(new My()); setSize(500,500); setVisible(true); } class My extends WindowAdapter { public void windowClosing(WindowEvent me) { System.out.println("Closed"); setVisible(false); } public static void main(String args[]){ frame1 f=new frame1(); } }

CSI 3125, Preliminaries, page 14 Working with Graphics Drawing Lines Lines are drawn by means of the drawLine( ) method, void drawLine(int startX, int startY, int endX, int endY) Drawing Rectangles The drawRect( ) and fillRect( ) methods display an outlined and filled rectangle void drawRect(int top, int left, int width, int height) void fillRect(int top, int left, int width, int height)

CSI 3125, Preliminaries, page 15 Working with Graphics To draw a rounded rectangle, use drawRoundRect( ) or fillRoundRect( ), both void drawRoundRect(int top, int left, int width, int height, int xDiam, int yDiam) void fillRoundRect(int top, int left, int width, int height, int xDiam, int yDiam) Drawing Ellipses void drawOval(int top, int left, int width, int height) void fillOval(int top, int left, int width, int height)

CSI 3125, Preliminaries, page 16 Working with Graphics Drawing Arcs Arcs can be drawn with drawArc( ) and fillArc( ) void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle) void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)

CSI 3125, Preliminaries, page 17 Working with Graphics Drawing Polygons It is possible to draw arbitrarily shaped figures using drawPolygon( ) and fillPolygon( ), void drawPolygon(int x[ ], int y[ ], int numPoints) void fillPolygon(int x[ ], int y[ ], int numPoints)

CSI 3125, Preliminaries, page 18 Working with Graphics Prog import java.applet.*; import java.awt.*; public class awt2 extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillRoundRect(100,100,200,200,30,40); int x[]={10,20,30,100,40,300,550}; int y[]={2,44,55,66,77,345,123}; g.drawPolygon(x,y,7); } /* */

CSI 3125, Preliminaries, page 19 Working with Colors Color class have 3 constructors Using this we can define our own colors Color(int red, int green, int blue) Color(int rgbValue) Color(float red, float green, float blue) The first constructor takes three integers that specify the color as a mix of red, green,and blue. These values must be between 0 and 255, as in this example: new Color(255, 100, 100); // light red. The second color constructor takes a single integer that contains the mix of red, green, and blue packed into an integer. The integer is organized with red in bits 16 to 23, green in bits 8 to 15, and blue in bits 0 to 7. Here is an example of this constructor: int newRed = (0xff | (0xc0 << 16) | (0x00 << 8) | 0x00); Color darkRed = new Color(newRed); The third constructor, Color(float, float, float), takes three float values (between 0.0 and 1.0) that specify

CSI 3125, Preliminaries, page 20 Working with Colors Once you have created a color, you can use it to set the foreground and/or background color by using the setForeground( ) and setBackground( ) methods eg setBackground(new Color(200,100,230) ); setForeground(new Color(200,100,230) )

CSI 3125, Preliminaries, page 21 Working with Fonts Font f1 = new Font(String fontName, int fontStyle, int fontSize); To give the font style as an integer value, the Font class comes with 3 symbolic constants. public final static int PLAIN = 0; public final static int BOLD = 1; public final static int ITALIC = 2; Font font = new Font("Arial", Font.BOLD,20); OR Font font = new Font("Arial", Font.BOLD+ Font.ITALIC,20); OR g.setFont(font);

CSI 3125, Preliminaries, page 22 Working with Fonts import java.applet.*; import java.awt.*; /* */ public class M53 extends Applet { public void paint(Graphics g) { Font font = new Font("Arial", Font.BOLD,20); g.setFont(font); g.drawString("popo",50,50); } }

CSI 3125, Preliminaries, page 23 Working with Fonts import java.applet.*; import java.awt.*; /* */ public class M53 extends Applet { public void paint(Graphics g) { Font f1 = new Font("Arial", 2,20); g.setFont(f1); g.drawString("Font Name: " + f1.getFontName(), 15, 80); g.drawString("Font Style: " + f1.getStyle(), 15, 100); g.drawString("Font Size: " + f1.getSize(), 15, 120); g.drawString("isBold(): " + f1.isBold(), 15, 140); g.drawString("isItalic(): " + f1.isItalic(), 15, 160); g.drawString("popo",50,50); } }

CSI 3125, Preliminaries, page 24 AWT

CSI 3125, Preliminaries, page 25 AWT