Getting Started with GUI Programming Chapter 10 CSCI 1302.

Slides:



Advertisements
Similar presentations
Chapter 13 Graphics.
Advertisements

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.
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 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.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 14 Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Graphics.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
1 L37 Graphics and Java 2D™ (2). 2 OBJECTIVES To use methods of class Graphics to draw lines,  rectangles,  rectangles with rounded corners,  three-dimensional.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics F The architecture.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L02 (Chapter 13) Graphics.
May 13, 1998CS102-02Lecture 7-2 The End of Graphics in Java CS Lecture 7-2 A picture's worth a thousand words.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
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.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 12 GUI Basics.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 11 Getting Started with GUI Programming 百闻不如一见.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Java Graphics Swing Graphics
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
2D Graphics Basics Chapter 2. Bird’s Eye View  Overview of Computer Graphics  2D Graphics: Basics –Basic 2D graphics, rendering pipeline, geometry,
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.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
© A+ Computer Science - Chicken yeller = new Chicken();
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.
Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
עקרונות תכנות מונחה עצמים תרגול 6 - GUI. Outline  Game of Life  Painting.
Review_6 AWT, Swing, ActionListener, and Graphics.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
1 Chapter 2 Getting Started with GUI Programming.
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.
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,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Graphics Drawing Things With Java. Today's Topics Course Updates Java Graphics –Java 1.1 versus Java 1.2 (Java 2) Drawing Lines Drawing Shapes.
Chapter 14 Applets and More. 2 Contents I. Introduction to Applets II. A Brief Introduction to HTML III. Creating Applets with Swing IV. Using AWT for.
Graphics Applets By Mr. Dave Clausen
Java Graphics CS 2511.
Graphics Applets By Mr. Dave Clausen
Java Applets.
Computer Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
עקרונות תכנות מונחה עצמים
A+ Computer Science METHODS.
Chapter 13 Graphics.
Chapter 13 Graphics.
Presentation transcript:

Getting Started with GUI Programming Chapter 10 CSCI 1302

CSCI 1302 – Getting Started with GUI Programming2 Drawing Graphics on Panels JPanel can be used for both containing components and for direct drawing To draw in a JPanel, you create a new class that extends JPanel and override the paintComponent method Doing this prevents you from interfering with other components Override this method protected void paintComponent(Graphics g) g is provided automatically by JVM

CSCI 1302 – Getting Started with GUI Programming3 Drawing Graphics on Panels Create a new type of panel Override the paintComponent method to modify drawing behavior Add your new panel to a frame See DrawMessage.java

CSCI 1302 – Getting Started with GUI Programming4 Drawing Graphics on Panels Java doesn’t use the traditional coordinate system

CSCI 1302 – Getting Started with GUI Programming5 Drawing Lines drawLine(int x1, int y1, int x2, int y2)

CSCI 1302 – Getting Started with GUI Programming6 Drawing Rectangles drawRect(int x, int y, int w, int h) fillRect(int x, int y, int w, int h) draw3DRect(int x, int y, int w, int h, int raised) x, y are upper left corner w, h are width and height

CSCI 1302 – Getting Started with GUI Programming7 Drawing Rectangles drawRoundRect(int x, int y, int w, int h, int aw, int ah) fillRoundRect(int x, int y, int w, int h, int aw, int ah) See DrawRectangles.java

CSCI 1302 – Getting Started with GUI Programming8 Drawing Ovals Based on bounding box of rectangle drawOval(int x, int y, int w, int h) fillOval(int x, int y, int w, int h) See DrawOvals.java

CSCI 1302 – Getting Started with GUI Programming9 Drawing Arcs Based on bounding rectangle drawArc(int x, int y, int w, int h, int startAngle, int arcAngle) fillArc(int x, int y, int w, int h, int startAngle, int arcAngle)

CSCI 1302 – Getting Started with GUI Programming10 The Polygon Class Description of a closed 2D region Arbitrary number of line segments Internally represented as a list of coordinate pairs Each pair represents a vertex, each successive pair represent the endpoints of a line First and last pairs are joined by a line to close the polygon

CSCI 1302 – Getting Started with GUI Programming11 The Polygon Class Two constructors public Polygon() public Polygon(int[] xpoints, int[] ypoints, int npoints) Draw polygons with one of 4 methods drawPolygon(Polygon polygon) drawPolygon(int[] xs, int[] ys, int n) fillPolygon(Polygon polygon) fillPolygon(int[] xs, int[] ys, int n)

CSCI 1302 – Getting Started with GUI Programming12 The Polygon Class Can add points manually, or through arrays Use the respective drawPolygon or fillPolygon methods to actually draw the shape on the canvas Also a drawPolyline method that doesn’t close the shape Polygon p = new Polygon(); p.addPoint(40,20); p.addPoint(70,40); p.addPoint(60,80); g.drawPolygon(p); OR int x[] = {40, 70, 60}; int y[] = {20, 40, 80}; g.drawPolygon(x,y, x.length) See DrawPolygon.java

CSCI 1302 – Getting Started with GUI Programming13 The FontMetrics Class FontMetrics can measure these attributes Leading, Ascent, Descent, Height Use getters, getFontMetrics and stringWidth method See CenterMessage.java

CSCI 1302 – Getting Started with GUI Programming14 Case Study: MessagePanel Develop a class that displays a message in a panel User can set the location of the message, center the message, or move the location UML on next slide provides the class contract; users can use the class without knowing how the class is implemented

CSCI 1302 – Getting Started with GUI Programming15 Case Study: MessagePanel

CSCI 1302 – Getting Started with GUI Programming16 Case Study: MessagePanel Do not invoke the paintComponent method directly Use the repaint method to force the JVM to update the viewing area, the JVM will call the paintComponent method Notice the reusability of MessagePanel, it can now be used in any other class to display a message on a panel