Drawing and Filling Geometric Shapes. Java comes with more than just points and lines. Within the Graphics2D class, there are also methods for drawing.

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 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
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.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
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 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.
©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.
May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS Lecture 7-1 A picture's worth a thousand words.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension.
Graphics Images – PictureBox control Drawing graphics - Graphics object Multimedia controls PictureBox control Image property – select image Choose how.
CS324e - Elements of Graphics and Visualization Java2D Graphics.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
2D Graphics: Rendering Details
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
© 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.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Chapter 4 Fundamental Data Types. Chapter Goals To be able to write simple applets To display graphical shapes such as lines and ellipses To use colors.
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.
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 Chapter 3 2D Graphics: Rendering Details  Color spaces, paints stroke types  Affine transforms including translation, rotation, scaling, shearing,
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.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
Georgia Institute of Technology Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
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.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
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.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
(PART II) Graphics and Multimedia. Font Control Font s  After a Font is created, its properties cannot be modified  Programmers must create a new Font.
(PART II) Graphics and Multimedia 11/02/1437 Lect6 (Part 2)
Barb Ericson Georgia Institute of Technology September 2005
User Interface Programming In Java
12 Graphics and Java 2D™.
Chapter 3 Syntax, Errors, and Debugging
Object Oriented Programming
Barb Ericson Georgia Institute of Technology September 2005
Java Applets.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Java Applets.
Graphics and Multimedia
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

Drawing and Filling Geometric Shapes

Java comes with more than just points and lines. Within the Graphics2D class, there are also methods for drawing rectangles, circles, and ellipses.

Graphics2D g2 = (Graphics2D) g; Rectangle2D.Double myRectangle = new Rectangle2D.Double(10, 20, 30, 40); g2.draw(myRectangle); myRectangle = new Rectangle2D.Double(50, 20, 30, 40); g2.fill(myRectangle);

Rectangle2D and Ellipse2D Rectangle2D is a class from which rectangle shaped objects derive from. They are constructed like so: Rectangle2D.Double mRect = new Rectangle2D.Double(x, y, width, height); X and Y refer to the upper-left corner of the rectangle. Ellipse2D also works the same way, except only its Double and Float classes can be used: Ellipse2D.Double mEllipse = new Ellipse2D.Double(x, y, width, height); There is no ‘Ellipse’ class that derives from Ellipse2D. Also note that there is no way to define the ‘center’ of the ellipse. Only the upper-left hand corner.

Rectangle2D and Inheritance Rectangle2D works similarly to Point2D. There are Double and Float classes for it, accessed by using Rectangle2D followed by a dot. There is also a Rectangle class that extends the Rectangle2D class.

Draw and fill The previous example used two methods of drawing. g2.draw(mRectangle); g2.fill(mRectangle); ‘Draw’ methods typically draw the outline of a shape, while ‘fill’ methods will fill the entire body of the shape. Graphics2D also has methods for drawing shapes without defining the rectangle in its own variable. G2.drawRect(x, y, width, height); G2.fillRect(x, y, width, height);

Colors

Much Excite! Tired of dull and boring shape drawings? The Java API has a built-in Color class, too! Include it in your project today with: import java.awt.Color;

Color doesn’t follow naming conventions!? There is a way to fetch pre-defined colors from the Color class. There are constants built into Color, named according to the color they should represent. Examples: Color.red, Color.blue, Color.magenta, Color.white… Notice that even though they are constants, they are completely spelled with lower case letters.

Creating your own colors Colors can be constructed like: Color myCol = new Color(float r, float g, float b); r, g, and b are values between 0.0 and 1.0, with 0.0 being no color, and 1.0 being the maximum amount of one color that can be used. Color myCol = new Color(0.75F, 0.0F, 0.25F); Notice that we are using floats instead of doubles for this constructor. It is important to remember that all ‘float’ values end with an ‘F’. This denotes that it is a float value, and not a double value. Without it, there would be a compiler error.

Using Colors This involves our Graphics2D object, g2. As already noted, you can think of Graphics as a pen. Before you draw a shape, there is a method called ‘setColor()’ which you can call to change the color of what you’re drawing. g2.setColor(Color.red); g2.draw(myRectangle); This would draw a red rectangle.

Text

Words!! Of course, it’s also possible to draw text onto graphical output, as well as defining fonts for drawing. Graphics2D’s drawString method: G2.drawString(String text, int x, int y); When defining x and y, you are defining where the text’s left-hand base line is (typically, the bottom of the text), and not the upper-left hand corner of where text is being drawn.

Fonts!! You may want to change the font that we use in drawing. Import this class: import java.awt.Font; Among the system supplied fonts in Java are: Serif SansSerif Monospaced Dialog DialogInput

Fonts!! The fonts can also be set to different sizes, which are given in measurements of ‘points’. Fonts can also be given different characteristics such as bold and italic.

Fonts in Action!! Font myFont = new Font(“Serif”, Font.BOLD, 16); g2.setFont(myFont); g2.drawString(“My text”, 20, 30); The constructor for Font takes in a string containing the name of the font, an integer constant defined in the Font class, and the point size as an integer. Just like with colors, we are calling the setFont() method to set the font before we draw the text. Finally, we draw the string like usual.

Text Positioning Unfortunately, it will be difficult to determine where exactly text will go when you define its position. Getting it in the right place may require trial and error. Just keep adjusting the position until it looks right.

Final Example An example applet of the graphical techniques covered is on our website at math.uaa.alaska.edu/~android/java/unit5. You could try compiling it yourself to see how all of this graphical wizardry works.

Things to try doing: Change the positioning, width, and height of the shapes. Changing the colors. Changing colors at different points within the code. Change the font, point size, or characteristics of the text drawn