Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Corresponds with Chapter 12
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
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.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
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.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
(c) University of Washington07-1 CSC 143 Introduction to Graphical Interfaces in Java: AWT and Swing Reading: Ch. 17.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
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.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
Session 27 Swing vs. AWT. AWT (Abstract Window ToolKit) It is a portable GUI library for stand-alone applications and/or applets. The Abstract Window.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– 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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Creating Your Own Widgets CompSci 230 S Software Construction.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Applets Yong Choi School of Business CSU, Bakersfield.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Java Dynamic Graphics.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Basics of GUI Programming Chapter 11 and Chapter 22.
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
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.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Java Graphics.
A First Look at GUI Applications
Java Graphics CS 2511.
Java Swing.
Lecture 27 Creating Custom GUIs
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
GUI AND GRAPHICS.
Introduction to Graphical Interfaces in Java: AWT and Swing
Lecture 4: Standard Java Graphics
Presentation transcript:

Graphics Programming

In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in a frame Using Graphics and Graphics 2D objects The repaint() method Using the drawString() method to draw strings Using Color Displaying text with multiple fonts

Introduction to Graphics Programming So far, user interaction has been limited –application parameters and JOptionPane –Users expect more varied interactions Today we will discuss how to: –resize windows on the screen –display text in a panel –adding color to windows and objects within the window –displaying text with multiple fonts Next class we will go further and talk about: –displaying 2D and 3D objects –copying an area –displaying images

Introduction to Graphics Programming After that we will discuss how to: –process events such as keystrokes and mouse clicks –add interface elements like menus and buttons

AWT and Swing Java 1.0 was introduced with a class library called Abstract Window Toolkit (AWT) –used for basic user interface –delegated creation and behavior of interface elements to the native GUI tookit on the target platform Windows vs. Solaris vs. Macintosh, etc –Downside to AWT: Worked well for simple applications but difficult to write high- quality portable graphics Limited graphics programming to the lowest common denominator. Different platforms had different bugs

AWT and Swing In 1996 Sun worked with Netscape to create Swing –In Swing user interface elements are painted onto blank windows –Swing is not a complete replacement of AWT. Instead it works with AWT. –Swing simply gives you more capable user interface components. –However, even though AWT components are still available, you will rarely use them.

Swing Reasons to choose Swing: –much richer and more convenient set of user interface elements –depends far less on the underlying platform so it is less vulnerable to platform-specific bugs –gives a consistent user experience across platforms –fullfill’s Java’s promise of “Write Once, Run Anywhere” –Easier to use than AWT

How to Create Graphics in Java Here are the basic steps you need to take to include graphics in your program: –Create a frame –Create a panel –Override the paintComponent() method in your panel

Creating a Frame A frame is the top-level window (a window that is not contained in another window) Frames are containers. Use the JFrame class –This is Swing’s version of frame. –Note: Swing component classes start with a ‘J’. –Swing components are placed in the javax.swing package.

Creating a Frame see FrameTest.java FrameTest.java

Frame Positioning Most methods for working the size and position of a frame come from the various superclasses of JFrame. Some important methods include: –the dispose() method: closes the window and reclaims system resources. –the setIconImage() method: takes an Image object to use as the icon when the window is minimized –the setTitle() method: changes the text in the title bar. –the setResizable() method: takes a boolean to determine if a frame will be resizeable by the user. –the setLocation() method: repositions a frame –the setBounds() method: both repositions and resizes a frame. –the setExtendedState(Frame.MAXIMIZED_BOTH): maximizes the size of a frame

Frame Positioning Note: If you don’t explicitly size the frame, it will default to being 0 by 0 pixels, which is invisible. –In a professional application, you should check the resolution of the user’s screen to determine the appropriate frame size.

Displaying Information in a Panel It is possible to draw messages and objects directly onto a frame, but this is not considered good programming practice. –In Java frames are really designed to be containers for components such as menu bars and other user interface elements. –You normally draw on another component called a panel. –You add a panel to your frame. –Swing programmers are most concerned with the content pane

Displaying Information in a Panel The content pane can be obtained this way: Container contentPane = frame.getContentPane(); Once we have the content pane, we can add a panel to it. Panels: –implemented by extending the JPanel class –have a surface onto which you can draw –are themselves containers so they can hold other user interface components like menus, buttons, etc.

Displaying Information in a Panel To draw on a panel you need to: –define a class that extends JPanel –override the paintComponent mehtod in that class The paintComponent method takes in one parameter of type Graphics.

Displaying Information in a Panel The Graphics object: –remembers a collection of settings for drawing images and text (e.g. fonts, current color, etc.) –all drawing in Java must go through a Graphics object –has methods to draw patterns, images, and text These operations are limited though (e.g. can’t vary line thickness or rotate shapes, etc.) –So, Sun created the Graphics2D class which is much more powerful

Displaying Information in a Panel The Graphics2D object: –is a subclass of the Graphics object –to draw shapes using the Graphics2D object, simply cast the Graphics object in your paintComponent method to a Graphics2D object public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; …. }

Displaying Information in a Panel So, now that we understand the Graphics obect, let’s go back to the paintComponent method –the first line in your override of this method needs to be super.paintComponent(g) so that the superclass method gets executed –never call the paintComponent method yourself in your programs Every time a window needs to be redrawn (e.g. user resizes a window), an event handler notifies the component causing the paintComponent method to be executed. To force a repainting of the screen, use the repaint method.

Using the drawString() Method to Draw Text drawString() method –allows you to draw a String in a Swing window –is a method of the Graphics class Requires three arguments: –A String –An x-axis coordinate –A y-axis coordinate Example: –g.drawString(“hello”, 75, 100) draws the string 75 pixels to the right and 100 pixels down starting from the upper left corner of the component.

Drawing Text Using Multiple Fonts Specify a font by its font face name (font family name with optional suffix) –e.g. Helvetica Bold To find out which fonts are available on a computer, call the getAvailableFontFamilyNames method of the GraphicsEnvironment class. –see pg. 262 in your books for example code To draw characters in a font, you must first crate an object of the class Font. –e.g. Font helvb14 = new Font(“Helvetica”, Font.BOLD, 14);

Centering Text To center text, you need to first get the width and height of the string in pixels. These dimensions depend on three factors: –the font used –the string –the device on which the font is drawn (e.g. the computer screen) To get the object that represents the font characteristics of the screen device, call the getFontRenderContext method of the Graphics2D class. It returns an object of the FontRenderContext

Centering Text The Graphics2D class is a subclass of the Graphics class. –You can instantiate your own Graphics or Graphics 2D objects The following code returns the rectangle that encloses the string: FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); See pg. 265 to see the code to use all of this information to center a string in its surrounding panel.

Example of Drawing Text Strings with Fonts see