Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.

Slides:



Advertisements
Similar presentations
Applets and Graphics.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
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 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?
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
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.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Swing II.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
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.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
©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.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
A Simple Applet.
Chapter 18 Swing II Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
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.
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.
// Java0601.java // This program demonstrates that the methods of a class are not always // accessible, like they were with the class. In this case an.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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.
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.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
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.
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.
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.
Intro to Graphics from Chapter 2 of Java Software Solutions
12 Graphics and Java 2D™.
Chapter 8 Graphics.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Building Java Programs
Chapter 18 Swing II Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
Chapter 8 Graphics.
Presentation transcript:

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 2 l Earlier chapters explain how to make graphical user interfaces (GUIs). l This chapter explains a different way of using graphics in Java: drawing pictures. l We will learn how to basic figures such as lines, ovals, and rectangles. l Basic figures can be combined to create elaborate pictures. l An optional section explains how to draw polygons. Basic Figures

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Screen Coordinate System (0, 0) origin Y-coordinate is positive and increasing in the downward direction. X-coordinate is positive and increasing to the right. All coordinates are normally positive.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 4 Screen Coordinate System (100, 75) (0, 0) origin X-coordinate: 100 pixels from left edge of screen Y-coordinate: 75 pixels from top edge of screen Location of a rectangle is specified by coordinates of upper left corner.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Screen Coordinate System (100, 75) (0, 0) origin Location of an oval is specified by a tightly fitting rectangle.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 6 The paint Method Most Swing and Swing related components have a method named paint. The paint method draws the component on the screen. To draw basic figures such as ovals and rectangles, you need to redefine the paint method. The paint method is called automatically and should not be invoked in the programmer’s code.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 7 public void paint(Graphics g) { super.paint(g); g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER); //Draw Nose: g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER); //Draw Eyes: g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT); g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT); //Draw eyebrows: g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW, X2_LEFT_BROW, Y2_LEFT_BROW); g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW, X2_RIGHT_BROW, Y2_RIGHT_BROW); //Draw Mouth: g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP); } The complete paint method from Madeleine.java The program draws this picture.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 8 public void paint(Graphics g) { super.paint(g); g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER); //Draw Nose: g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER); //Draw Eyes: g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT); g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT); //Draw eyebrows: g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW, X2_LEFT_BROW, Y2_LEFT_BROW); g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW, X2_RIGHT_BROW, Y2_RIGHT_BROW); //Draw Mouth: g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP); } The face, nose, and eyes are ovals. The eyebrows are lines, and the mouth is an arc. The method for drawing each shape has numeric parameters telling where to draw the shape and how big to make it.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 9 public void paint(Graphics g) { super.paint(g); g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER); //Draw Nose: g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER); //Draw Eyes: g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT); g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT); //Draw eyebrows: g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW, X2_LEFT_BROW, Y2_LEFT_BROW); g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW, X2_RIGHT_BROW, Y2_RIGHT_BROW); //Draw Mouth: g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP); } The paint method receives one parameter, which is an object of the Graphics class. The Graphics parameter is the calling object for all of the methods that draw lines and shapes. Calling the paint method of the parent class is a good programming practice.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 10 The Graphics class l Contains methods used to draw basic shapes and lines l Is part of the AWT package (or library) so the AWT must be imported: import java.awt.*; l Most methods for painting shapes have two versions: »A draw version which only draws the outline of the shape drawOval »A fill version which fills in the shape fillOval

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Drawing Rectangles and Ovals (100, 120) (0, 0) g.fillOval(100, 120, 40, 20); (100, 50) width height Graphics object g.fillRect(100, 50, 40, 20); X and Y coordinates of upper left corner

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 12 Drawing Arcs l An arc is specified by giving an oval and then specifying what portion of the oval will be used for the arc. l To tell which part of the oval will be used, specify the beginning angle and the degrees of the sweep. positive direction 0 degrees width height (x,y) gdrawArc(x, y, width, height, 0, 360);

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 13 Arc Examples positive direction 0 degrees negative direction 0 degrees 180 degrees gdrawArc(x, y, width, height, 0, -90); gdrawArc(x, y, width, height, 0, 360); gdrawArc(x, y, width, height, 0, 90); gdrawArc(x, y, width, height, 180, 90); plus 90 degrees

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 14 Round Rectangles l Round rectangle is a rectangle where corners have been replaced with arcs. l Specify by giving rectangle information and then height and width of corner arcs. arcWidth width height (x,y) gdrawRoundRect(x, y, width, height, arcWidth, arcHeight); arcHeight

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Polygons drawPolygon allows a program to draw shapes with any number of sides. public void drawPolygon(int[] x, int[] y, int point) l Each point in the polygon will have an x coordinate from the first parameter and a y coordinate from the corresponding element of the second parameter. l The third parameter tells how many points the polygon will have. l Always draws a closed polygon. If first and last points are not equal, draws a line from last to first. drawPolyline is similar but can draw an open figure. fillPolygon is similar but fills with color instead of drawing outline.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 16 Action Drawings and repaint The SadMadeleine program demonstrates how a program can change a picture. l In the original picture, the face has a frown. l When the user clicks on the button, the picture changes to a smiling face and moves up on the screen.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 17 Action Drawings and repaint The actionPerformed method changes the smile variable that the paint method uses. When the smile variable is changed to true, the paint method knows to draw a smile instead of a frown. public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Click for a Smile.")) smile = true; else System.out.println("Error in button interface."); repaint(); }

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 18 Action Drawings and repaint Unless the repaint method is called, the change will not be shown on screen. The repaint method calls paint to update the picture. The paint methd should not be called directly. public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Click for a Smile.")) smile = true; else System.out.println("Error in button interface."); repaint(); }

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 19 ColorsColors Draw and fill methods like fillOval will use the current color. You can use the setColor method of a Graphics object to change the current color. l To draw a red mouth you could change the paint method of the Madeleine program to include these lines: //Draw Mouth: g.setColor(Color.red); g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP);

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 20 Defining Colors The Color class mixes amounts of red, green, and blue to produce a new color. l Since colors are made by combing different amounts of red, green, and blue, this is called the RGB color system. The parameters to the Color constructor must be type int or float. When integers are used as parameters to the Color constructor they should be in the range 0 to 255. When floats are used as parameters to the Color constructor they should be in the range 0.0 to 1.0. Color brown = new Color(204, 102, 0);

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 21 Fonts and Other Text Details l A font is a style of text. A program can use the drawString method to display text on the screen. l The first parameter tells which characters to display. The last two parameters to drawString are coordinates that tell where on the screen to put the text. g.drawString(theText, X_START, Y_START);

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 22 Setting the Font The method setFont in a Graphics object can be used to change the current font. l Java guarantees that at least three fonts will be available: »Monospaced »SanSerif »Serif

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 23 Using the Font Constructor To specify a font with a particular name, style, and size, use the Font constructor. Size of the font is specified in points. Each point is 1/72 of an inch, but point sizes are only approximate. Font f = new Font(“Serif”, Font.BOLD|Font.ITALIC, POINT_SIZE); g.setFont(f); Name of the font Style of the font: bold and italic Size of the font in points (an integer)

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 24 Summary You can draw figures such as lines, ovals, and rectangles using methods in the class Graphics. You can specify the color of each figure drawn using the method setColor of the Graphics class. You can define your own colors using the class Color. l Colors are defined using the RGB (Red/Green/Blue) system. You can add text to a a graphics drawing by using the Graphics method drawString. You can set the font and point size for a text written using drawString with the Graphics method setFont.