Sei sulla pagina 1di 17

Drawing in Java

CS 141 Introduction to CS

Drawing in Java
Sunday, November 4, 2012 1

Drawing on a Picture

What if we want to draw something on a picture? How about drawing a grid of lines on top of the picture?

Drawing in Java
Sunday, November 4, 2012 2

Drawing Lines Exercise


Write a method drawGrid() to draw horizontal and vertical lines on the current picture
Lines every 20 pixels in x and y directions How would we do this?

Drawing in Java
Sunday, November 4, 2012 3

Drawing Other Shapes

How would you draw a circle on a picture? How would you draw a string of characters? You still would need to set the pixel colors of certain pixels
Which pixels?

Drawing in Java
Sunday, November 4, 2012 4

The java.awt.Graphics Class


Java has a way of doing 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(); for example Picture pict = new Picture(); Graphics g = pict.getGraphics();
Drawing in Java
Sunday, November 4, 2012

5
5

Drawing Circles and Ellipses


g.drawOval(x, y, width,height) g.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

x,y

height

Give the width and height of the enclosing rectangle


To make a circle use the same value for the width and height

width

Drawing in Java
Sunday, November 4, 2012 6

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 p.write(fileName);

Drawing in Java
Sunday, November 4, 2012 7

Drawing Lines and Polygons


Line
g.drawLine(x1, y1, x2, y2);
x1,y1 x2,y2

Polygon
Outlined Polygon
g.drawPolygon(xArray, yArray, numPoints); g.drawPolygon(currPolygon);

Filled Polygon
g.fillPolygon(xArray, yArray, numPoints); g.fillPolygon(currPolygon);

Drawing in Java
Sunday, November 4, 2012 8

Drawing Lines

g.drawLine(x1,y1,x2,y2);

x1,y1 x2,y2

Drawing in Java
Sunday, November 4, 2012 9

Drawing Arcs
Arcs
Outlined Arc
g.drawArc(topLeftX, topLeftY, width, height, startAngle, arcAngle);

Filled Arc
g.fillArc((topLeftX, topLeftY, width, height, startAngle, arcAngle);

Drawing in Java
Sunday, November 4, 2012 10

Drawing Rectangles
Outlined Rectangle
g.drawRect(topLeftX, topLeftY, width, height);

Filled Rectangle
g.fillRect(topLeftX,topLeftY,width,height);

Outlined Rounded Rectangle


g.drawRoundRect (topLeftX,topLeftY,width,height,arcWidth,arcHeight);

Filled Rounded Rectangle


g.fillRoundRect (topLeftX,topLeftY,width,height,arcWidth,arcHeight);
Drawing in Java
Sunday, November 4, 2012 11

Draw a Picture Exercise


Create a method that will draw a simple picture
Use at least one rectangle Use at least one polygon how to draw the polygon? Use at least one oval Use at least one arc

Drawing in Java
Sunday, November 4, 2012 12

Writing a Picture out to a file


The Picture class has a built-in method to do this
pict.write(filename.jpg); but pict.write(C:\\Documents\\filename.jpg); or both work better.

pict.write(/Users/jfdooley/Desktop/filename.jpg);

Drawing in Java
Sunday, November 4, 2012

13
13

Precision Drawings
How would you draw a stack of filled rectangles starting from the lightest one at the bottom right and the darkest one at the top left.
With 10 pixels between each Not easy with drawing packages

Drawing in Java
Sunday, November 4, 2012 14

Draw outlines of Rectangles


or any other figure Whats too restrictive about drawFilledRectangles()? all rectangles start at (0, 0)
limited to 25 rectangles

So lets create
drawRectangles(int x, int y, int number);

Drawing in Java
Sunday, November 4, 2012

15
15

Working with Fonts


Create a font object with the font name, style, and point size (Font class is in java.awt)
Font labelFont = new Font(TimesRoman, Font.BOLD, 24); Font normalFont = new Font(Helvetica,Font.PLAIN, 12);

Set the current font


g.setFont(labelFont);

Get font information


g.getStyle() g.getSize() g.getName() g.getFamily()

Drawing in Java
Sunday, November 4, 2012 16

Working with Strings


To draw a string
g.drawString(String to Draw, leftX, baselineY); in this case (leftX, baselineY) is the lower left-hand corner of the text (go figure)

Drawing in Java
Sunday, November 4, 2012 17

Potrebbero piacerti anche