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.

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.
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?
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.
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.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
©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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
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.
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 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.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
©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.
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
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.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 14 Java Fundamentals 2D Graphics Wed. 10/11/00.
10/5: Primitives, the for loop Primitive data types –why we mention them Return to counter-controlled repetition.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
Graphics & Applets CSC 1051 – Data Structures and Algorithms I
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.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
Introductory Graphics Chapter Three. Computer Graphics §Full motion pictures (“Star Wars”) l animated films §Virtual reality §Games §Simulators (air craft.
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.
10/4: the for loop & the switch structure Primitive data types –why we mention them Return to counter-controlled repetition.
Variables and Methods Chapter 3 – Lecture Slides.
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.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
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.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Intro to Graphics from Chapter 2 of Java Software Solutions
Chapter 8 Graphics.
Object Oriented Programming
Java Applets.
Building Java Programs
Graphics Applets By Mr. Dave Clausen
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Java Applets.
Chapter 8 Graphics.
Making a Smile Face Pepper.
Presentation transcript:

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 JApplet public class DrawEx extends JApplet { public void paint( Graphics g ) { // put your code here! }

3 Graphics paint method: –basic method for showing graphics objects –base class has a paint method which does nothing –you can override the paint method by writing your own paint method Browser decides when to call paint method –When user Scrolls –When user returns to window with applet –When user Resizes We can explicitly call it repaint( );

4 Coordinates (0,0) in upper left corner Example import java.awt.*; import javax.swing.*; public class Coords extends JApplet { public void paint( Graphics g ) { g.drawString( "(0,0)", 0, 0 ); g.drawString( "(100,10)", 100, 10 ); g.drawString( "(20,50)", 20, 50 ); g.drawString( "(190,90)", 190, 90 ); } Why can’t we see “(0,0)” printed?

5 Text To draw text on the applet, we call GraphicsObj.drawString( “text string”, x, y ) import java.awt.*;// access the Graphics object import javax.swing.*;// access to JApplet public class Text1 extends JApplet { public void paint ( Graphics gr ) { gr.drawString ( "Hello Worldling", 0, 0 ); gr.drawString ( "Java rocks", 0, 50 ); gr.drawString ( "Skiing is fun", 50, 50 ); gr.drawString( "To be, or not 2 B", 50, 65 ); } Notice the 'g' from "Worldling" hanging in to view...

6 Graphics g The name for the Graphics object in the main method must match the name of the object when we call drawString public void paint( Graphics grp ) { grp.drawString( "Some text to display", x, y ); } public void paint( Graphics g ) { g.drawString( "Some text to display", x, y ); }

7 Font Don’t reference special fonts that most people don’t have installed on their systems! Standard font names: –Serif, SanSerif, Monospaced Font f = new Font( “Serif”, Font.PLAIN, 14 ); g.setFont( f ); Font.BOLD, Font.ITALIC, Font.PLAIN –How would you do bold and italic?

8 Font Example import java.awt.*; import javax.swing.*; public class TextFonts extends JApplet { public void paint ( Graphics g ) { g.drawString ("Hello World", 0,10 ); Font small = new Font( "Serif", Font.PLAIN, 8 ); g.setFont( small ); g.drawString ("Java rocks", 0, 50 ); Font big = new Font( "SanSerif", Font.BOLD + Font.ITALIC, 36 ); g.setFont( big ); g.drawString ( "Skiing is fun", 50, 50 ); }

9 Colors Java Method:.setColor( color ) color should be Color.name where name can be: –BLACK,BLUE, CYAN, YELLOW, DARKGRAY, GRAY, GREEN, LIGHTGRAY, MAGENTA, ORANGE, PINK, RED, WHITE –Example: Color.GREEN Color.BLUE

10 Colors or, make your own color –Color mine = new Color( 0, 255, 128 ); –values are the amount of red, green, and blue (RGB values) –values are between 0 and 255, where 0 is black and 255 is white –Example: red, 10 green, half-blue –Use your paint program to determine values Java statement example: g.setColor( Color.cyan ); setBackground( Color.yellow ); setForeground( new Color( 33, 200, 190 ) );

11 Custom Colors Websites and paint programs can help determine the RGB values for a color Example: Microsoft Paint Colors Edit Colors Define Custom Colors… RGB values as displayed inside MS Paint

12 Color Example import java.awt.*; import javax.swing.*; public class ColorEx extends JApplet { public void paint ( Graphics g ) { g.setColor( Color.RED ); g.drawString( "My favorite color", 30, 45 ); g.setColor( new Color( 12,34,52) ); g.drawString( "Can't wait to ski again", 30, 53 ); }

13 Drawing draw fill draw3D public void drawRect( int x, int y, int w, int h ) public void fillRect( int x, int y, int w, int h ) public void draw3DRect( int x, int y, int w, int h ) public void drawLine( int x, int y, int x2, int y2 ) public void fillOval( int x, int y, int w, int h ) public void fillArc( int x, int y, int w, int h, int stA, int arcA)

14 Drawing Example import java.awt.*; // access the Graphics object import javax.swing.*; // access to JApplet public class Shapes extends JApplet { public void paint ( Graphics g ) { g.drawOval( 0,0, 100, 10 ); g.drawRect ( 10,10, 20, 20 ); g.setColor( Color.BLUE ); g.drawRect ( 40,80, 10, 40 ); g.setColor( Color.RED ); g.fillOval ( 5,30, 77, 10 ); }

15 3DRect Example import java.awt.*; import java.swing.*; public class Rect3D extends JApplet { public void paint( Graphics g ) { // raised g.setColor( Color.RED ); g.draw3DRect( 20,50, 100, 20, true ); g.draw3DRect( 21,51, 98, 18, true ); // sunken g.draw3DRect( 20,200, 100, 20, false ); g.draw3DRect( 21,201, 98, 18, false ); } Can be hard to discern the 3-D… try different colors To make it thicker: draw multiple times change x,y, w, h

16 Arc public void fillArc( int x, int y, int w, int h, int stA, int arcA) stA = starting angle: arcA = arc angle

17 Arc Example import javax.swing.*; import java.awt.*; public class PieChart extends JApplet { public void paint( Graphics g ) { // pie g.setColor( Color.red ); g.fillArc( 20,20, 300, 300, 20, 90 ); g.setColor( Color.yellow ); g.fillArc( 20,20, 300,300, 110,45 ); g.setColor( Color.blue ); g.fillArc( 20,20, 300,300, 155, 180 ); // outline g.setColor( Color.black ); g.drawArc( 20,20, 300, 300, 0, 360 ); } drawing the outline has to be done last – Why? To overlay it on top of color pies

18 Images Call method __________ Image imageVariable = getImage( getCodeBase( ), filename ); Graphics method drawImage Needs the keyword _______ g.drawImage( imageVariable, x, y, this );

19 Image Example import javax.swing.*; import java.awt.*; public class ImageEx extends JApplet { public void paint( Graphics g ) { Image img = getImage( getCodeBase( ), "Lion.jpg" ); g.drawImage( img, 0,0, this ); }

20 Summary of drawing methods Lines drawLine(x, y, x2, y2) Rectangles drawRect(x, y, width,height) fillRect(x,y,width, height) Rounded rectangles clearRect(x,y,width,height) 3-D raised or lowered rectangles Raised: draw3DRect(x,y,width,height, true) Lowered: draw3DRect(x,y,width, height,false) Raised:fill3DRect(s,y,width,height,true) Lowered: fill3DRect(x,y,width, height,false) Ovals drawOval(x,y,width, height) Arcs drawArc(x,y,width, height, startAngle,arcAngle) fillArc(x,y,width, height,startAngle,arcAngle) Text drawSting(“text”, x, y)