Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.

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

Web Design & Development Lecture 19. Java Graphics 2.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
COM 1020 Martin Cooke 2D Graphics. Objectives To show how to draw in a Swing- based GUI using Java 2D See Java2Demo in demo/jfc/Java2D.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
UID – Swing and Graphics Boriana Koleva
User Interface Programming in C#: Graphics
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.
Graphics Programming UQC117S2 Semester /4. Session 3 1 Drawing in Java 2D Graphics API.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
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.
CS 112 GUI 06 May 2008 Bilkent. Java GUI API Containers: ◦ contain other GUI components. E.g, Window, Panel, Applet, Frame Dialog. Components: ◦ Buttons,
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.
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
Web Design & Development Lecture 18. Java Graphics.
CS324e - Elements of Graphics and Visualization Java2D Graphics.
Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.
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.
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Java Graphics Swing Graphics
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
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 Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
Java Graphics Stuart Hansen 11/6/03. What’s Wrong with OpenGL Graphics – not GUI –No real support of text boxes, buttons, etc. Procedural - not OOP –No.
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
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)
Java Direct Manipulation Chris North cs3724: HCI.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
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,
Java Dynamic Graphics.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Review_6 AWT, Swing, ActionListener, and Graphics.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Java Graphics Chris North cs3724: HCI. Presentations peter hou Vote: UI Hall of Fame/Shame?
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Graphics in Java Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
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.
Eleventh Graphics in Java.
12 Graphics and Java 2D™.
Creating Your Own Widgets
Java Graphics.
Java Graphics CS 2511.
Chapter 10 Graphics.
Java Graphics The basic rendering mechanism is the drawing system that controls when and how programs can draw on a graphics component. When a component.
GUI Graphics Chris North cs3724: HCI.
Model, View, Controller design pattern
Presentation transcript:

Java Graphics

Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement

Useful Java DataStructures Vector (dynamic array) V = new Vector( ); V.add(item); V.elementAt(5); HashTable (maps keys to items) H = new HashTable( ); H.add(key, item); H.get(key); Iterators (automatic FOR loops) I = V.iterator( ); While (I.hasNext( )) dosomething(I.next( ));

Graphics Window is like a painter’s canvas App must paint its window contents Java components paint themselves Anything else: Programmer When to paint? How to paint? JButton

How to Paint?

Pixels

Coordinate System Upside-down Cartesian Y window = height - Y cartesian (0,0) (width,0) (0,height)(width, height)

Component Hierarchy Each component has its own subwindow Subwindow = rectangular area within parent component Has own coordinate system Clipping: Can’t paint outside its subwindow Can’t paint over child components? (0,0) (w p, h p ) (0,0) (w b, h b ) JPanel JButton

Painting Components Can paint any component JPanel is good for custom graphics area JButton JPanel

Painting in Java import java.awt.Graphics import java.awt.Graphics2D// Java2 1. Get the “graphics context” of component Graphics g = myJPanel.getGraphics( ); Graphics2D g2 = (Graphics2D) g; 2. Paint in it g2.drawLine(x1,y1, x2,y2);

Graphics Primitives DrawFill Point (x,y) Line (pt1,pt2) PolyLine (pt list) Arc Oval (pt, w,h) Rectangle (pt, w,h) RoundRectangle Polygon (pt list) Image (file, x,y) Text (string, x,y) label

Graphics Attributes Color Font Stroke attributes: –Line width, dash, end caps, joins, miter Paint attributes: –Color, gradient, texture Composite: –Blending Transforms: –Translate, rotate, flip, shear, scale

Color Combinations of Red, Green, Blue Each [0, 255] Java: new Color(255, 150, 0) Hokie Orange

in Java Drawing primitives: g2.drawLine( ),.drawRect( ), … g2.fillRect( ), … Object oriented: 1.Create shape: import java.awt.geom.* shape = new Point2D.Float(x, y); Line2D, Rect2D, CubicCurve2D, … 2.Draw the shape: g2.draw(shape); g2.fill(shape);

in Java Color and font: g2.setColor( new Color(r,g,b) ); g2.setFont( new Font(…) ); Advanced: g2.setStroke(…); g2.setPaint(…); g2.setComposite(…); g2.setTransform(…); 1.Set graphics attributes 2.Draw graphics

When to Paint?

Re-Paint Screen is like a painter’s canvas All windows paint on the same surface! Windows don’t “remember” whats under them Need to re-paint when portions are newly exposed Receive Repaint events Open, resize, bring to front When other windows in front move, resize, close

MyApp

Open WinExp, Notepad

Close WinExplorer Repaint event sent to: MyApp, Desktop

Desktop gets repaint event

MyApp gets repaint event MyApp JPanel gets repaint event

MyApp gets repaint event MyApp JPanel forwards repaint event to JButton

Repainting Static Graphics Repaint event: Erase subwindow (fill with background color) Draw subwindow contents

In Java Repaint event: Java Swing components catch repaint event, and call their paintComponent( ) method Default paintComponent( ) method paints component –E.g. panel background color Sub-class the component (typically JPanel) Over-ride paintComponent( ) method Custom graphics here Can call repaint( ) to invoke paintComponent( )

Code public class MyPanel extends JPanel { public void paintComponent(Graphics g){ super.paintComponent(g); // erases background Graphics2D g2 = (Graphics2D)g; //cast for java2 // my graphics: g2.setColor(new Color(255,0,0)); g2.fillRect(10,10,200,50); g2.setColor(new Color(0,0,0)); g2.drawString("Hello World", 10, 10); } Hello World

Typical program structure for Dynamic Graphics Store data structure of window contents E.g. user drawn picture in paint program Repaint event: Erase window (draw background color) Draw window contents using data structure Other event that alters window contents: modify the data structure send repaint event

In JBuilder Subclassing JPanel Overriding paintComponent( ) Using subclassed JPanel in a JFrame

Storing window contents 2 approaches: Store logical contents in a data structure »E.g. drawing program: lines, shapes, colors, … » Store visual contents as an off-screen image (bitmap) »E.g. pixels »Then use g2.drawImage( ) in paintComponent( ) »

Problem: Flashing Ugly flashing when repaint: Paint background Redraw shapes

Solution: Double buffering

Double buffered repaint: Draw all graphics in temporary off-screen image »Paint background color »Paint shapes Then paint image to JPanel Bonus: Swing does this for you! Draw graphics on JPanel JPanel maintains off-screen image

What Subwindows Do Directs mouse input to correct component Determines repaint events Own coordinate system Don’t need repaint when moving Clipping: hides drawing behind a subwindow Some subwindows remember what’s under them: Popup menus