Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Advertisements

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.
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.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
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.
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.
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.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
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.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
1 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
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).
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Line Graphics Summary 1.drawLine(xStart, yStart, xEnd, yEnd); 2.drawRect(x, y, width, height); 3.drawRoundRect(x, y, width, height, cornerWidth, cornerHeight);
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
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.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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 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.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
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)
Building Java Programs Graphics Reading: Supplement 3G.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Chapter 8 Graphics.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Building Java Programs
Building Java Programs
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Topic 8 graphics "What makes the situation worse is that the highest level CS course I've ever taken is cs4, and quotes from the graphics group startup.
Building Java Programs
Building Java Programs
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Building Java Programs
Chapter 8 Graphics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

Graphics

Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics, Color, Font classes Graphics, Color, Font classes Displaying graphics Displaying graphics Graphic User Interface (GUI) Graphic User Interface (GUI) Frames and Containers Frames and Containers Applets Applets Event-driven Programs Event-driven Programs

Graphics-related Classes in Package java.awt Graphics Graphics —for drawing geometric figures —for drawing geometric figures Color Color —for background and foreground colors for text, for geometric figures —for background and foreground colors for text, for geometric figures Font Font —for text formatting, size, style, face, etc. —for text formatting, size, style, face, etc.

Java Coordinate System Draw figures on a “canvas,” such an applet or a frame. Draw figures on a “canvas,” such an applet or a frame.

Graphics Methods A Graphics object provides a handle (graphics environment) for various graphics methods, such as A Graphics object provides a handle (graphics environment) for various graphics methods, such as SetColor() SetColor() setFont() setFont() drawRect() drawRect() drawOval() drawOval() fillRect() fillRect() fillOval() fillOval()

Drawing a Rectangle The following code segments draws a rectangle with a blue outline. The following code segments draws a rectangle with a blue outline. int xStart = 10; // in pixels int yStart = 20; int width = 100; int height =50; Graphics g = new Graphics(); g.setColor(Color.blue); g.drawRect(xStart, yStart, width, height);

Drawing an Oval The following statement draws an oval which is inscribed in a rectangle of the given dimensions. (The rectangle will not be shown.) The following statement draws an oval which is inscribed in a rectangle of the given dimensions. (The rectangle will not be shown.) g.drawOval(xStart, yStart, width, height); (xStart, yStart) heightheight width

Drawing a Circle The following statements draw a circle of radius 10 (pixels), at (30, 40), and a filled circle at (50, 60). (Note that (startX, startY) refers to the corner of a circumscribed rectangle.) int r = 10; int startX = 30; int startY = 40; Graphics g = new Graphics(); g.drawOval(startX, startY, 2 * r, 2 * r); g.fillOval(startX + 20, startY + 29, 2 * r, 2 * r) The following statements draw a circle of radius 10 (pixels), at (30, 40), and a filled circle at (50, 60). (Note that (startX, startY) refers to the corner of a circumscribed rectangle.) int r = 10; int startX = 30; int startY = 40; Graphics g = new Graphics(); g.drawOval(startX, startY, 2 * r, 2 * r); g.fillOval(startX + 20, startY + 29, 2 * r, 2 * r)

Drawing an Arc The following statement draws an arc, which is defined in terms of an oval. The following statement draws an arc, which is defined in terms of an oval. g.drawArc(xStart, yStart, width, height, startAngle, arcAngle); startAngle (xStart, yStart) heightheight width arcAngle

Filling Rectangles and Ovals g.setColor(Color.blue); g.fillRect(xStart, yStart, width, height); g.fillOval(xStart, yStart + 70, width, height); g.setColor(Color.yellow); g.fillOval(xStart, yStart + 140, width, height);

Drawing a Line The following statements draw a line from (20, 30) to (120, 50). The following statements draw a line from (20, 30) to (120, 50). xStart = 20; yStart = 30; xEnd = 120; yEnd = 50; drawLine(xStart, yStart, xEnd, yEnd);

Drawing a Text The following statements draw a text starting at (20, 30). The following statements draw a text starting at (20, 30). int xText = 20; int yText = 30; g.drawString("Welcome to Hawaii.", xText, yText);

Setting Color The following statements create a yellow circle at (20, 40) and a blue square at (80, 40). The following statements create a yellow circle at (20, 40) and a blue square at (80, 40). int r = 10; g.setColor(Color.YELLOW); g.fillOval(20, 40, r, r); g.setColor(Color.BLUE); g.fillRect(80, 40, r, r);

RGB Colors Color Constant RGB ORANGE 255, 200, 0 PINK 255, 175, 175 CYAN 0, 255, 255 MAGENTA 255, 0, 255 YELLOW 255, 255, 0 BLACK 0, 0, 0 WHITE 255, 255, 255 GRAY 128, 128, 128 LIGHT_GRAY 192, 192, 192 DARK_GRAY 64, 64, 64 RED 255, 0, 0 BLUE 0, 0, 255 GREEN 0, 255, 0

Exercise: Rectangle and Line Write a Java code segment to create the following figure. (Solution) Write a Java code segment to create the following figure. (Solution)(Solution)(Solution)

Rectangle and Line int xStartRect = 0; int yStartRect = 0; int width = 100; int height = 50; g.setColor(Color.RED); g.fillRect(xStartRect, yStartRect, width, height); g.setColor(Color.BLACK); int xStartLine = xStartRect; int yStartLine = yStartRect; int xEndLine = xStartLine + width; int yEndLine = yStartLine = height; g.drawLine(xStartLine, yStartLine, xEndLine, yEndLine);

Exercise Write a code segment which will produce a circle of radius 100, centered at (150, 200), and a horizontal line segment from the center to the circle. Write a code segment which will produce a circle of radius 100, centered at (150, 200), and a horizontal line segment from the center to the circle.

Exercise (cont.) If the center is at (150, 200), the right-top corner is (50, 100), and the width and height of the circumscribed rectangle are both 200. If the center is at (150, 200), the right-top corner is (50, 100), and the width and height of the circumscribed rectangle are both 200. (50, 100)(150, 100) (50, 200) (150, 200) (250, 200)

Displaying Graphics Graphics objects must be drawn inside a “container” Graphics objects must be drawn inside a “container” Top-level Container (heavy-weight) Top-level Container (heavy-weight) JApplet, JFrame, JWindow JApplet, JFrame, JWindow Secondary Container: (light-weight) Secondary Container: (light-weight) JPanel, JScrollPane, JRootPane JPanel, JScrollPane, JRootPane Used to arrange layout of components like JButton, JLabel, JtextField, etc Used to arrange layout of components like JButton, JLabel, JtextField, etc

CircleDemoApplet The following demo applet shows: The following demo applet shows: The following demo applet The following demo applet Orange circle of diameter 150, centered at (200, 200). Orange circle of diameter 150, centered at (200, 200). A 150 x 30 blue rectangle, located 30 px from left edge of circle & 50 px above its bottom A 150 x 30 blue rectangle, located 30 px from left edge of circle & 50 px above its bottom A text inside the rectangle A text inside the rectangle

ColorUse Demo Applet (cont.) All drawings occur in paint() method. All drawings occur in paint() method. setBackground() --sets the applet's background color setBackground() --sets the applet's background color g.setColor()-- sets the color for the graphics context (unless it is changed, color remains the same for all subsequent figures) g.setColor()-- sets the color for the graphics context (unless it is changed, color remains the same for all subsequent figures) Color myColor = new Color(0,200,200); g.setColor(myColor); can be combined to g.setColor(new Color(0, 200, 200)); Color myColor = new Color(0,200,200); g.setColor(myColor); can be combined to g.setColor(new Color(0, 200, 200));

Font Class Name Name Specific, like “Helvetica”, “Courier New” Specific, like “Helvetica”, “Courier New” Family, like “San Serif”, “Monospaced” Family, like “San Serif”, “Monospaced” Style Style BOLD, ITALIC, PLAIN BOLD, ITALIC, PLAIN Size Size 10, 12, 18, etc., in “points” 10, 12, 18, etc., in “points” Font f = new Font("Helvetica", Font.BOLD, 24); g.setFont(f); g.drawString("Hawaii", 50, 50);

Font Demo The following code segment produces the lines of display shown below. The following code segment produces the lines of display shown below.following code segment following code segment

Example: Greeting Draw the image consisting of Draw the image consisting of 1. Background in cyan 2. 6 ovals at the top 3. 1 yellow circle 4. 6 rectangles at bottom 5. A text message across the circle Solution

Greeting import javax.swing.*; import java.awt.*; public class Greeting extends JApplet { // declarations final int APPLET_W = 350; //applet size final int APPLET_H = 250; int xCircle, yCircle; //circle pos. int radCircle; //circle size int xText, yText; //text pos. int xTopRow, yTopRow; //top row pos. int wOval, hOval; //oval size int gapOval; //gap betw. Ovals int xBotRow, yBotRow; //bottom row pos int xRect, yRect; //rectangle pos. int wRect, hRect; //rectangle size size int gapRect; //gap betw. Recs.

public void init() { // set dimensions and locations of obj. xTopRow = 40; yTopRow = 20; xCircle = xTopRow + 50; yCircle = yTopRow + 50; radCircle = 120; xText = xCircle - 50; yText = yCircle + radCircle - 30; wOval = 40; hOval = 15; gapOval = 50; wRect = wOval; hRect = hOval; gapRect = gapOval; xBotRow = xTopRow; yBotRow = APPLET_H - (hRect + 10); }

public void paint(Graphics g){ // draw filled circle setBackground(Color.CYAN); g.setColor(Color.YELLOW); g.fillOval(xCircle, yCircle, radCircle, radCircle); // draw message text g.setColor(Color.BLUE); Font f = new Font("Brush Script MT", Font.PLAIN, 32); g.setFont(f); g.drawString("Welcome to Hawaii.", xText, yText);

// draw 6 ovals at top g.setColor(Color.MAGENTA); int xOval = xTopRow; int yOval = yTopRow; for (int i = 1; i <= 6; i++) { g.drawOval(xOval, yOval, wOval, hOval); xOval += gapOval; } // draw 6 rectangles at bottom g.setColor(Color.RED); xRect = xBotRow; yRect = yBotRow; for (int i = 1; i <= 6; i++) { g.drawRect(xRect, yRect, wRect, hRect); xRect += gapRect; } } }

Example: Greeting (cont.) Note: Note: Various graphics elements are declared as instance variables, so that they can be accessed from all methods in the class Various graphics elements are declared as instance variables, so that they can be accessed from all methods in the class They are initialized in paint() They are initialized in paint() Variables are defined in terms of previously defined variables. E.g., Variables are defined in terms of previously defined variables. E.g., final int APPLET_H = 250; hOval = 15; hRect = hOval; yBotRow = APPLET_H - (hRect + 10);

Example: House Applet House uses geometric shapes with colors can be used to paint a simple scenery. Applet House uses geometric shapes with colors can be used to paint a simple scenery. Applet House Applet House Here is the source code for House.java. Here is the source code for House.java.source code for House.javasource code for House.java

Example: Smiley Applet Smiley is another example to illustrate the use of Graphics objects. Applet Smiley is another example to illustrate the use of Graphics objects. Applet Smiley Applet Smiley Here is the source code for Smiley.java. Here is the source code for Smiley.java.source code for Smiley.java.source code for Smiley.java.