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.

Slides:



Advertisements
Similar presentations
Applets and Graphics.
Advertisements

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.
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
CSC1401 Drawing in Java - 2. Reminder from last class How do you save your modified picture? String filename = …; Picture stevePicture = new Picture(filename);
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
1 Graphics -- Introduction zThe use of graphics is common among modern software systems  Java has strong API support for graphics in the java.awt (abstract.
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 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
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.
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.
May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS Lecture 7-1 A picture's worth a thousand words.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
May 13, 1998CS102-02Lecture 7-2 The End of Graphics in Java CS Lecture 7-2 A picture's worth a thousand words.
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.
Agenda For Feb Discussion (Assignments & Rubric) 2. PowerPoint Presentation (Drawing Basic Shapes). 4.Read Unit 3 carefully (pages ) then.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
© 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.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Exposure Java-A 2006 Chapter 4 Slides Using Methods and Parameters
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(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.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
1 Graphics, Fonts and Color Chapter 9. 2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs)
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.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
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,
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
12 Graphics and Java 2D™.
Drawing Geometric Objects
Chapter 8 Graphics.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Building Java Programs
Building Java Programs
Building Java Programs
Today’s topics Java Applications Upcoming Reading Graphics
JAVA 2 Design and programming of GUI
Chapter 10 Graphics.
Fitting Curve Models to Edges
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
CASE Tools Graphical User Interface Programming Using C#
Graphics -- Introduction
Digital Media Dr. Jim Rowan ITEC 2110.
Building Java Programs
Chapter 8 Graphics.
Presentation transcript:

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

Course Updates & Notices Pre-Computer Science Pizza Feed –Share your opinions with Dr. Quinn –Monday, January 5:30 PM –Covell Hall, Room 216 Schedule Change –Program #2 is due Next Friday 2/4 Some changes to the assignment –Quiz #3 is next Wednesday 2/2 –Guest Lecture Friday 2/4

The Java Graphics System Java provides a set of graphic commands that allow you, the programmer to: –Display graphical shapes on the screen size shape location are under programmers control –Display strings size, font, style are under programmers control –Display images –Color these objects –Move these objects

Coordinate Systems Java’s coordinate system is not like the coordinate system you will use in physics and general math classes The Origin is in the upper left hand corner X increases to the right Y increases downward The dimensional unit is a pixel –How big is a pixel?

Java Coordinates X Y

Drawn and Filled Shapes Java lets you draw lines and shapes Java shape drawing methods come in two styles –those where the outline only is shown drawShape()draw(shapeClass) –those where the center is filled fillShape()fill(shapeClass) Java provides the ability to display predefined images Java provides the ability to display widgets

Displaying Things First we need a Graphics context –That portion of the screen to draw upon How do we obtain a graphics context? –We get one as the argument to paint() Be careful about passing the graphics context around, –it may not always be 'in context' Java is state driven –Things tend to stay they way they are until they are changed Once we set a color it will stay that way

Some Graphical things Line segments Connected line segments Rectangles Ellipse Arcs Rectangles with rounded corners Polygons More in Java 2!!!

Drawing Lines The 1.1 AWT provides three methods for drawing lines: –Straight Line Segments drawLine(...) –Connected Line Segments drawPolyline(…) –Portions of an Ellipse drawArc(…) Major Java 1.1 Graphic deficiency: –We cannot set the line size or style.

Line Segments The command is of the form: GraphicsObject.drawLine(int xStart, int yStart, int xStop, int yStop) Draws a line from position point (xStart,yStart) to (xStop,yStop) Why don't straight lines look straight? –our pixels are in a grid Sometimes this produces a stair step effect

Example drawLine() public void paint( Graphics grfx ){ grfx.drawLine( 10, 10, 50, 30 ); } (10,10) (50,30)

Drawing and Filling Rectangles Drawing Rectangles void drawRect(int x, // upper left x coordinate int y, // upper left y coordinate int width, // bounding box width int height) // bounding box height Draws an outline of a rectangle bounded whose upper left hand corner is defined at the point ( x,y), and is width wide, and height tall. To draw a solid rectangle we use the fillRect () method with the same arguments

Example Rectangles public void paint(Graphics g) { g.setColor(Color.red); g.drawRect(10,20,50,30); g.setColor(Color.blue); g.fillRect(20,30,75,30); } (10,20) 50 Wide75 Wide 30 Tall

Squares & Negative Numbers Square –Width and the height are the same size. –Aspect ratio? It is possible for the width or the height to be negative. –This makes the point you specify with first two arguments lie on some other corner than the upper left. –Most browsers will not draw this

Drawing Ellipses void drawOval(int x, // upper left x coordinate int y, // upper left y coordinate int width, // bounding box width int height) // bounding box height This draws an outline of an ellipse bounded by the rectangle whose upper left hand corner is defined at the point ( x,y), and is width wide, and height tall. –Note that the point (x,y) does not actually fall on our arc.

Example Ellipses public void paint(Graphics g) { g.setColor(Color.red); g.drawOval(10,20,60,30); g.setColor(Color.blue); g.fillOval(100,20,30,30); } (10,20) (100,20) Width 60Width 30

Circles, & Fills The JDK does not provide a method to draw a circle. To do that we must set our bounding box to be a square, (the width and the height are the same size). To draw a solid, we use the fillOval() method. The fillOval() takes exactly the same arguments as the drawOval().

Angle Measurements in Java Angles are measured counter clockwise from the horizontal x axis. –In Java, 0 degrees is at the 3-o'clock position. –Positive arc angles indicate counter-clockwise rotations, negative arc angles are drawn clockwise. 120º 30º X Y

Drawing Arcs We can draw smooth curves as a portion of an ellipse –This does not have a filled component The drawArc() command draws an arc bounded by the rectangle whose upper left hand corner is defined at the point ( x,y), and is width wide, and height tall. If we were to draw a complete circle, it would touch the centers (midpoints) of each of the sides of our bonding box. –However, we define our curve to be drawn from startAngle to an arc of sweepAngle degrees.

The drawArc() method void drawArc(int x,// bounding box, x // upper left corner int y,// bounding box, y // upper left corner int width,// width of // bounding box int height,// height of // bounding box int startAngle,// in degrees int sweepAngle)// extent

Example drawArc() public void paint(Graphics g) { g.drawArc(10,20,30,40,30,80); } (10,20) Height 40 Width 30 Sweep Angle 80 Start Angle 30

Rectangles with Rounded Corners We can combine our approach for drawing rectangles and drawing ellipses into a a shape which is a rectangle with rounded corners or curved ends. The Java API command drawRoundRect() combines the complexity of rectangles with the difficulty of ellipses.

drawRoundRect() command void drawRoundRect(int x,// First four int y,// parameters int width,//are as in a int height,// rectangle int arcWidth, int arcHeight) //horizontal and vertical // ‘diameter’ at the // at the corners

More rounded rectangle stuff The first four parameters are exactly the same as those for the rectangle. –If you put zeros in for the last two parameters, you will have a rectangle drawn exactly as before. We use the fifth argument in the drawRoundRect() method as we do the third argument in the drawOval() method. –This describes the horizontal 'diameter' of the arc. The sixth argument in the drawRoundRect() method corresponds to the fourth argument in the drawOval() method. –This describes the vertical 'diameter' of the arc. Of course, there is a fillRoundRect () for filling in our rectangle with a solid color.

Example Rounded Rectangles public void paint(Graphics g) { g.fillRoundRect(10,20,60,30,5,5); g.fillRoundRect(40,30,60,30,15,15); }

Polygons Like the polyline command, this accepts an array of x's and y's. –However, unlike the polyline method, the polygon will create the line that connects the last point in polygon to the first point in the polygon. As in the polyline, you must be certain that the number of points specified is equal to or smaller than the smallest number of points in ether of the arrays.

drawPolygon() One form of the drawpoly command : void drawPolygon(Polygon p) Uses a polygon object: –We must first create a polygon object: ( p, above). –To do this we would declare and instantiate a polygon: Polygon p = new polygon; –We then add points to the polygon: p.addPoint(x,y);

Example Polygon public void paint(Graphics g) { Polygon poly = new Polygon(); poly.addpoint(10,10); poly.addpoint(20,20); poly.addpoint(30,10); poly.addpoint(10,20); g.drawPolygon(poly); } 0: (10,10) 3: (10,20) 1: (20,20) 2: (30,10)

So What's Up With Java 2? In Java 1.1 (and earlier) –Every drawing command needed to be provided by the AWT Or built up of know AWT drawing commands In Java 2 –Shape class objects know how to draw and fill themselves –Any new shape we make must do so as well So we'll use the existing shape and drawing stuff

The Handout The left side is the Java 2 version –From your textbook The right side is the Java 1.1 version –Same # of draw commands –Not very Object Oriented Consider drawing the rectangle –Why create a rectangle »then have to specify the data elements as input to draw it? –Why can't draw know how to draw rectangles? »Better: Why can't rectangles draw themselves?

Summary / To Do Java Graphics –1.1 vs. 1.2 differences Drawing Lines drawLine(), drawArc() Drawing shapes rectangles, ellipses, polygons, & rectangles with round corners Filling Shapes Upcoming Events: –Monday 1/31: HW #4 Pizza Feed –Wednesday 2/2: Quiz #3 –Friday 2/4: Program #2 Guest Lecture No Office Hours –Monday 2/7: Midterm