Chapter 3- Flow Control

Download Report

Transcript Chapter 3- Flow Control

Chapter 15-Graphics objects and
drawing.
Overview




Basic Shapes
More on Colors
Fonts and drawing strings
Review
Basic Shapes
The libraries


To draw things with simple components
like lines and circles, we have to import
the java.awt.* libraries.
The Graphics class is the most
important class from java.awt (for
drawing), but you might as well include
them all.
The CanvasThe Coordinate System

The origin (0,0) is in the upper left hand corner.
All coordinates in in (x,y) form.
The x-coordinate increases as you go right.

The y-coordinate increases as you go down.


(0,0)
(30,0)
(0,10)
(30,10)
Basic painting- Always call the
super.



When we want to draw on a JFrame, we want
to override the paint method. If we are doing
it on a JPanel, we use paintComponent.
You are passed a Graphics object.
Always start by calling super(g). Else you
may not have a window that you draw in.
public void paint(Graphics g)
{
super(g);
…
}
Drawing components

Graphics class- Contains lots of methods to draw
simple things
–drawLine
–drawRect
–drawRoundRect
–draw3dRect
–drawOval
–drawArc
–drawPolygon
–drawPolyline

–fillRect
–fillRoundRect
–fill3dRect
–fillOval
–fillArc
–fillPolygon
Draw all of them off of the Graphics object.
g.drawOval(10,20,30,40);
Drawing lines

Give 4 integers:
– beginning x coord
– beginning y coord
– ending x coord
– ending y coord
Line practice

Give the coordinates for the following
lines:
•
•
•
•
Starts at (50,40) and goes to the right 20 pixels.
Starts at (50,40) and goes down 20 pixels.
Starts at (50,40) and goes right 20, up 30.
Starts at (0,10) and goes down 10, left 20.
Two Dimensional shapes.


Usually specify the x and y coordinate
along with the width and height of the
smallest rectangle that could contain the
object.
Often have extra info to provide.
g.drawRect(30,40,20,10);
Drawing Arcs



An arc is just a section of the outside of an
oval.
When we want to draw one we give the size
and location of the bounding box(like usual)
and we give it the angle to start drawing at
and how long Java should draw.
This angle is 0 at 3 o’clock and increases in a
counter-clockwise direction (90 degrees is at
12 o’clock, 180 degrees is a 9 o’clock, 270
degrees is at 6 o’clock, and 360 is the same
as 0.
Arc examples.

Try drawing the following arcs inside of
some bounding box:
– starts at 0 degrees, goes for 90 degrees.
– starts at 0 degrees, goes for 360 degrees.
– starts at 45 degrees, goes for -90 degrees.
– starts at -90 degrees, goes for 90 degrees.
– starts at 180 degrees, goes for 90 degrees.
Rounded rectangles


Draws a rectangle with rounded
corners.
You specify size of rectangle and also
the oval width and height for the
corners.
Polygons



We can also draw polygons of any number of
sides.
Pass the function 2 arrays, one for x
coordinates, one for y coordinates. We also
pass a number to tell how many points to use.
drawPolygon will automatically close off the
figure if the first and last points don’t match,
while drawPolyline will not.
Paint and repaint




Before we used validate() if we wanted to
force Java to update our GUI.
If we want Java to update our graphics object,
we call repaint().
We never redefine repaint, we just call it.
We never call paint, we just redefine it.
When to repaint



When a user clicks a button that should
change your painting.
In animation.
Whenever you made a change that
should affect the painting area.
Basic shapes review





What library do we import to draw graphics?
What method do we override to draw in a
JFrame? How about a JPanel?
Describe the coordinate system? Where is
the origin?
Write the code to draw a line starting at (0,10)
and going down 20 pixels and right 30 pixels.
What would be drawn by making an arc start
at 120 degrees and going 120 degrees?
Basic shapes review


Name 5 drawing components.
Which method do we override, paint or
repaint? Which do we call?
More Colors
Colors review


We have already seen how to get one
of the common colors (Color.red, etc.).
We have also seen how to make our
own colors
Color aColor = new Color(103,100,50);

We get colors from the java.awt.*
library.
More Color Methods


We can retrieve the current values of a color
by using getRed(), getBlue(), and getGreen().
These return integers between 0 and 255.
We can also get a darker or lighter version of
a current color by calling brighter() and
darker(). Multiple calls of each of them may
not return you your original color.
JColorChooser



The JColorChooser dialog is a premade
dialog that has most of the usual ways people
like to choose colors.
It will return to you the color that the user
chooses.
You give it three arguments:
– The parent component(usually this).
– A title
– A current or default color.
JColorChooser


This dialog is located in javax.swing.*
This is just one of the standard dialogs
that Java has, there are others for
common operations such as opening or
saving a file, or even a simple dialog
that takes in a small amount of user
input.
Color Review





What libraries do we need for colors
and JColorChooser?
How do we specify a common color?
How do we create our own color?
How do we get the color components of
a color object?
What do we use JColorChooser for?
Fonts and text
Fonts and text


Along with writing in labels and text
areas, we can also write fancy letterings
directly on a graphics object.
We use the drawString method
g.drawString(someText, xCoord, yCoord);

This draws someText starting at the
point xCoord and yCoord.
Fancy text

We can change the font that we want to
use for drawing strings also. First we
need to create a Font.
Font f = new Font(“SansSerif”,
Font.BOLD | Font.ITALIC, 24);

Once we have created the Font we can
set the font for the graphics object.
g.setFont(f);
Font options



font name: At least “Monospaced”,
“SansSerif”, and “Serif”
styles: Font.BOLD, Font.ITALIC,
Font.PLAIN. You can “bitwise or” these
together with a single ‘|’.
size: Any integer size you want.
All the fonts your computer has
String [] allFonts = GraphicsEnvironment.
getLocalGraphicsEnvironment().
getAvailableFontFamilyNames();
for(int i = 0; i < allFonts.length; i++)
{
System.out.println(allFonts[i]);
}
Font Review


What method do we use to set the font
for the Graphics object?
What method do we use to write text to
a Graphics object?
Review
Review





What library do we import to draw graphics?
What method do we override to draw in a
JFrame? How about a JPanel?
Describe the coordinate system? Where is
the origin?
Name 5 drawing components.
Which method do we override, paint or
repaint? Which do we call?
Review




What libraries do we need for colors?
How do we specify a common color?
How do we create our own color?
What new dialog did we learn of that
helps us choose colors?
Review


What method do we use to set the font
for the Graphics object?
What method do we use to write text to
a Graphics object?