CSC1401 Drawing in Java - 1. Goals Understand at a conceptual and practical level How to use the Turtle class to draw on a picture How to use the java.awt.Graphics.

Slides:



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

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.
Introduction to Programming
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.
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);
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
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.
©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.
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 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
Georgia Institute of Technology Drawing in Java part 1 Barb Ericson Georgia Institute of Technology September 2006.
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.
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).
2D Graphics: Rendering Details
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Drawing-Turtle-and-AWT1 Drawing in Java Barb Ericson Georgia Institute of Technology Sept 21, 2009.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Miscellaneous Topics #4 Fonts.
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.
CSC1401. Learning Goals Understand at a conceptual level What is media computation? How does color vision work? How can you make colors with red, green,
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
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.
Georgia Institute of Technology Drawing in Java Barb Ericson Georgia Institute of Technology August 2005.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
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.
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)
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
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.
Georgia Institute of Technology Drawing in Java – part 1 Barb Ericson Georgia Institute of Technology September 2005.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
Barb Ericson Georgia Institute of Technology September 2005
Intro to Graphics from Chapter 2 of Java Software Solutions
Chapter 8 Graphics.
Building Java Programs
CSC 1051 – Data Structures and Algorithms I
Barb Ericson Georgia Institute of Technology September 2005
Building Java Programs
Building Java Programs
Building Java Programs
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
Chapter 10 Graphics.
Basic Graphics Drawing Shapes 1.
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
Workshop for Programming And Systems Management Teachers
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
Workshop for Programming And Systems Management Teachers
Building Java Programs
Chapter 8 Graphics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

CSC1401 Drawing in Java - 1

Goals Understand at a conceptual and practical level How to use the Turtle class to draw on a picture How to use the java.awt.Graphics class to do simple drawing How to draw simple shapes How to set the color How to draw text

Drawing on a Picture What if we want to draw something on a picture? How about drawing a grid of lines on top of a picture? We could use a Turtle object to draw the lines Create the Turtle on a Picture object Picture p = new Picture(FileChooser.pickAFile()); Turtle turtle1 = new Turtle(p); Using the methods: moveTo(x,y), penUp(), penDown(), forward(), forward(amount), turnRight(), turnLeft(), turn(amount), etc.

Drawing Lines Exercise Write a method drawGrid in Picture.java to draw horizontal and vertical lines on the current picture, using a Turtle Draw 3 lines in x and 3 in y To test it: String file = FileChooser.getMediaPath(“barbara.jpg”); Picture p = new Picture(file); p.drawGrid(); p.show(); // show is invoked after drawing // on the picture

Drawing Other Shapes How would you draw a circle on a picture? How would you draw a string of characters? Java has a class that knows how to do these things Using a Graphics object It knows how to draw and fill simple shapes and images You can draw on a picture object By getting the graphics object from it pictureObj.getGraphics();

AWT Graphics Class Methods of the Graphics class in the java.awt package let you paint Pick a color to use Draw some shapes Circles, Rectangles, Lines, Polygons, Arcs Shapes drawn on top of other shapes will cover them Set the font to use Draw some letters (strings)

Working with java.awt.Color To create a new color object new Color(redValue,greenValue,blueValue) There are predefined colors red, green, blue, black, yellow, gray, magenta, cyan, pink, orange To use these do: Color.RED or Color.red Set the current drawing color using graphicsObj.setColor(colorObj); Get the current drawing color using Color currColor = graphicsObj.getColor();

Using Classes in Packages Java organizes classes into packages Groups of related classes The full name of a class is packageName.ClassName java.awt.Color If you want to use just the class name to refer to a class in a package Use the import statement before the class definition in your class import java.awt.Color; // to allow you to use Color or import java.awt.*; // to use any class in this package

Graphics Environment 0,0 0, 0 is at the top left X increases to the right Y increases going down the page +X +Y Graphics are often positioned by their top left corner Coordinate units are measured in pixels 400,200

Drawing Circles and Ellipses gObj.drawOval(x,y,width, height) gObj.fillOval(x,y,width, height) Give the x and y of the upper left corner of the enclosing rectangle Not a point on the circle or ellipse Give the width and height of the enclosing rectangle To make a circle use the same value for the width and height x,y width height

Draw Circle Exercise Write a method to add a yellow sun to a picture Test with beach.jpg String file = FileChooser.getMediaPath(“beach.jpg”); Picture p = new Picture(file); p.drawSun(); p.show(); Save the new image with pictureObj.write(fileName);

Working with Fonts Create a font object with the font name, style, and point size Font labelFont = new Font(“TimesRoman”, Font.BOLD, 24); Font normalFont = new Font(“Helvetica”,Font.PLAIN, 12); Set the current font gObj.setFont(labelFont);

Working with Strings To draw a string gObj.drawString(“test string”,leftX,baselineY); leftX baselineY height test string descent ascent leading

Add a String to a Picture Exercise Write a method drawString that will add some text to a picture Set the color to draw with Set the font to use when drawing the string Draw a string near the bottom left of the picture If you have time create another method that takes a string and y value and centers the string in x Test with String file = FileChooser.getMediaPath(“kitten2.jpg”); Picture p = new Picture(file); p.drawString(“Barbara Ericson”); p.show();

Drawing Lines and Polygons Line g.drawLine(x1,y1,x2,y2); x1,y1 x2,y2

Drawing Lines Exercise Write a method (drawX) for adding two crossed lines to a picture Using a passed color Start one line at the top left corner of the picture End it at the bottom right corner of the picture Start the other line at the bottom left of the picture End it at the top right

Summary You can draw using Turtle objects Or you can use java.awt.Graphics to do drawing Get the Graphics object from a Picture object Graphics graphics = pictureObj.getGraphics(); Set the color graphics.setColor(Color.RED); Do some simple drawing graphics.drawLine(x1,y1,x2,y2); graphics.drawOval(x1,y1,width,height); graphics.drawString(“Steve”,leftX,baseline);

Assignment Read Media Computation Chapter 7, Sections 1-2