Chapter 15-Graphics objects and drawing. - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 15-Graphics objects and drawing.

Description:

To draw things with simple components like lines and circles, we have to import the java.awt. ... font name: At least 'Monospaced', 'SansSerif', and 'Serif' ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 35
Provided by: erics84
Learn more at: https://www.cs.uaf.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 15-Graphics objects and drawing.


1
Chapter 15-Graphics objects and drawing.
2
Overview
  • Basic Shapes
  • More on Colors
  • Fonts and drawing strings
  • Review

3
Basic Shapes
4
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.

5
The Canvas- The 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)
6
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)
7
Drawing components
  • Graphics class- Contains lots of methods to draw
    simple things
  • fillRect
  • fillRoundRect
  • fill3dRect
  • fillOval
  • fillArc
  • fillPolygon
  • drawLine
  • drawRect
  • drawRoundRect
  • draw3dRect
  • drawOval
  • drawArc
  • drawPolygon
  • drawPolyline
  • Draw all of them off of the Graphics object.

g.drawOval(10,20,30,40)
8
Drawing lines
  • Give 4 integers
  • beginning x coord
  • beginning y coord
  • ending x coord
  • ending y coord

9
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.

10
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)
11
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 oclock and increases in a
    counter-clockwise direction (90 degrees is at 12
    oclock, 180 degrees is a 9 oclock, 270 degrees
    is at 6 oclock, and 360 is the same as 0.

12
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.

13
Rounded rectangles
  • Draws a rectangle with rounded corners.
  • You specify size of rectangle and also the oval
    width and height for the corners.

14
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 dont match,
    while drawPolyline will not.

15
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.

16
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.

17
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?

18
Basic shapes review
  • Name 5 drawing components.
  • Which method do we override, paint or repaint?
    Which do we call?

19
More Colors
20
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
  • We get colors from the java.awt. library.

Color aColor new Color(103,100,50)
21
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.

22
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.

23
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.

24
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?

25
Fonts and text
26
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
  • This draws someText starting at the point xCoord
    and yCoord.

g.drawString(someText, xCoord, yCoord)
27
Fancy text
  • We can change the font that we want to use for
    drawing strings also. First we need to create a
    Font.
  • Once we have created the Font we can set the font
    for the graphics object.

Font f new Font(SansSerif, Font.BOLD
Font.ITALIC, 24)
g.setFont(f)
28
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.

29
All the fonts your computer has
String allFonts GraphicsEnvironment. getLoca
lGraphicsEnvironment(). getAvailableFontFamilyNam
es() for(int i 0 i lt allFonts.length
i) System.out.println(allFontsi)
30
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?

31
Review
32
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?

33
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?

34
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?
Write a Comment
User Comments (0)
About PowerShow.com