TCSS 305 Stepp - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

TCSS 305 Stepp

Description:

drawing custom shapes and pictures on the screen ... Toolkit tk = Toolkit.getDefaultToolkit(); Image img = tk.getImage('myimage.jpg' ... – PowerPoint PPT presentation

Number of Views:535
Avg rating:3.0/5.0
Slides: 23
Provided by: tacomaWa
Category:
Tags: tcss | courses | custom | stepp

less

Transcript and Presenter's Notes

Title: TCSS 305 Stepp


1
TCSS 305 (Stepp)
  • 2D Graphics
  • Horstmann Ch. 7 pp. 258-283
  • Java Tutorial Java 2D Graphics.http//java.sun.c
    om/docs/books/tutorial/2d/

2
Outline
  • drawing custom shapes and pictures on the screen
  • using panels as drawing surfaces (instead of as
    containers)
  • fonts, images, and other graphical items

3
A drawing panel
  • extend JPanel and override paintComponent method
    to use a panel as a drawing surface
  • public void paintComponent(Graphics g)
  • coordinate system (0, 0) at top-left,x-axis
    increases rightward, y-axis downward
  • panel surface is transparent unless drawn on

4
Some details
  • JPanel's paintComponent does important things
    that we don't want to lose, so call the method
    super.paintComponent first thing!
  • paintComponent's Graphics g argument represents
    "graphical context" object that can be told to
    draw things on the panel
  • Guaranteed for every JPanel that actual object
    passed in is a Graphics2D (can cast)
  • panel has small preferred size, so it will be
    tiny when put into some layouts use
    setPreferredSize(Dimension) to make it bigger!
  • refreshing panel can't call paintComponent, so
    call repaint()

5
Basic panel painting syntax
  • public class MyPanel extends JPanel
  • public void paintComponent(Graphics g)
  • super.paintComponent(g) // important!
  • Graphics2D g2 (Graphics2D) g
  • // put your painting code here

6
Quick drawing example
  • public class MyPanel extends JPanel
  • public MyPanel()
  • this.setBackground(Color.WHITE)
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • Graphics2D g2 (Graphics2D) g
  • Shape shape new Ellipse2D.Double(10,
    10, 20, 50)
  • g2.setPaint(Color.BLUE)
  • g2.fill(shape)

7
Drawing text strings
  • Graphics2D
  • public void drawString(String s, int x, int
    y)Draws given string with its first letter's
    bottom-left corner at the given location.
  • The string is drawn using the Graphics2D's
    current color and font settings.
  • Before drawing the string, you can set the font,
    color, and other attributes.(see next slides)

8
Fonts
  • Graphics2D
  • public void setFont(Font f)Sets this Graphics
    context to start writing text in the given font.
    (Forgotten at end of paintComponent call!)
  • java.awt.Font
  • Text styles used when drawing Strings on the
    panel
  • public Font(String name, int style, int size)
  • some predefined font names
  • "Serif", "SansSerif", "Monospaced"
  • font styles (can be combined with operator)
  • Font.BOLD
  • Font.ITALIC
  • Font.PLAIN

9
Colors and paints
  • java.awt.Color(a simple single-colored paint)
  • public Color(int r, int g, int b)
  • public Color(int r, int g, int b, int alpha)
  • a partially-transparent color (range 0-255,
    0transparent)
  • public Color brighter(), darker()
  • public static Color black, blue, cyan, darkGray,
    gray, green, lightGray, magenta, orange, pink,
    red, white, yellow
  • java.awt.GradientPaint(smooth transition between
    2 colors)
  • public GradientPaint(float x1, float y1, Color
    color1, float x2, float y2, Color color2)
  • java.awt.TexturePaint(use an image as a "paint"
    background)

10
Shapes (old style)
  • Graphics2D
  • public void drawLine(int x1, int y1, int x2, int
    y2)
  • public void drawOval(int x, int y, int w, int h)
  • public void fillOval(int x, int y, int w, int h)
  • public void drawPolygon(int xpoints, int
    ypoints, int len)
  • public void fillPolygon(int xpoints, int
    ypoints, int len)
  • public void drawRect(int x, int y, int w, int h)
  • public void fillRect(int x, int y, int w, int h)
  • public void drawArc(...)
  • public void fillArc(...)
  • ...
  • Drawing methods for drawing various lines and
    shapes on a Graphics context. Not recommended to
    be used
  • replaced by draw(Shape), fill(Shape) in
    Graphics2D

11
Graphics2D shape methods
  • public void draw(Shape s)Draws the outline of
    the given shape using this Graphics2D's current
    pen.
  • public void fill(Shape s)Draws outline and fills
    inside of given shape.
  • rotate, scale, translate, shear,
    transformPerform various coordinate
    transformations on the Graphics2D context.

12
Shapes (in package java.awt.geom)
  • Arc2D.Double(double x, double y, double w,
    double h, double start,
    double extent, int type)An arc, which is a
    portion of an ellipse.
  • Ellipse2D.Double(double x, double y,
    double w, double h)An ellipse, which is a
    generalization of a circle.
  • Line2D.Double(double x1, double y1, double x2,
    double y2)Line2D.Double(Point p1,
    Point p2)A line between two points.

13
Shapes
  • Rectangle2D.Double(double x, double y,
    double w, double h)A rectangle, which is
    a generalization of a square.
  • RoundRectangle2D.Double( double x, double y,
    double w, double h, double arcx, double arcy)A
    rounded rectangle.
  • GeneralPath()A customizable polygon.

14
Methods of to all shapes
  • public boolean contains(double x, double
    y)public boolean contains(Point2D p)
  • Whether the given point is contained inside the
    bounds of this shape.
  • public Rectangle getBounds()
  • A rectangle representing the bounding box around
    this shape.
  • public double getCenterX() / getCenterY()
  • public double getMinX() / getMinY()
  • public double getMaxX() / getMaxY()
  • Various corner or center coordinates within the
    shape.
  • public boolean intersects(double x, double y,
    double w, double h)
  • public boolean intersects(Rectangle2D r)
  • Whether this shape touches the given rectangular
    region.

15
Strokes (pen styles)
  • Graphics2D
  • public void setStroke(Stroke s)Sets type of
    drawing pen (color, width, style)that will be
    used by this Graphics2D.
  • java.awt.BasicStroke
  • A pen stroke for drawing outlines
  • public BasicStroke(float width)
  • public BasicStroke(float width, int cap,
    int join)
  • public BasicStroke(float width, int cap, int
    join, float miterlimit, float dash, float
    dash_phase)
  • cap CAP_BUTT, CAP_ROUND, CAP_SQUARE
  • join JOIN_BEVEL, JOIN_MITER, JOIN_ROUND

16
Drawing example 1
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • Graphics2D g2 (Graphics2D) g
  • g2.fill(new Rectangle2D.Double(10, 30, 60,
    35))
  • g2.fill(new Ellipse2D.Double(80, 40, 50,
    70))

17
Drawing example 2
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • Graphics2D g2 (Graphics2D) g
  • g2.setPaint(Color.BLACK)
  • g2.fill(new Rectangle2D.Double(10, 30, 100,
    50))
  • g2.setPaint(Color.RED)
  • g2.fill(new Ellipse2D.Double(20, 70, 20,
    20))
  • g2.fill(new Ellipse2D.Double(80, 70, 20,
    20))
  • g2.setPaint(Color.CYAN)
  • g2.fill(new Rectangle2D.Double(80, 40, 30,
    20))

18
Drawing example 3
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • Graphics2D g2 (Graphics2D) g
  • g2.setPaint(Color.BLUE)
  • for (int i 1 i lt 10 i)
  • g2.fill(new Ellipse2D.Double(15 i, 15
    i, 30, 30))
  • g2.setPaint(Color.MAGENTA)
  • for (int i 1 i lt 10 i)
  • g2.fill(new Ellipse2D.Double(15 (11 -
    i), 15 i,
  • 30, 30)

19
Drawing images
  • Graphics2D
  • public void drawImage(Image i, int x, int y,
    ImageObserver io)
  • public void drawImage(Image i, int x, int y, int
    width, int height,
    ImageObserver io)Draws given image object on
    this Graphics context with its top-left corner at
    given location (pass the panel as the
    ImageObserver).

20
Classes for imaging
  • java.awt.ToolkitGets images from disk or
    internet
  • public static Toolkit getDefaultToolkit()
  • public Image getImage(String filename)
  • public Image getImage(URL address)
  • java.awt.MediaTrackerEnsures that images are
    loaded fully
  • public MediaTracker(Component comp)
  • public void addImage(Image img, int id)
  • public void waitForAll() throws
    InterruptedException
  • java.awt.ImageRepresents a graphic image (BMP,
    GIF, ...)
  • public int getWidth(ImageObserver obs)
  • public int getHeight(ImageObserver obs)
  • java.awt.image.BufferedImageA blank graphic
    image surface onto which you can draw
  • public BufferedImage(int w, int h, int type)
  • public Graphics getGraphics()

21
Images, continued
  • Code to load an image
  • Toolkit tk Toolkit.getDefaultToolkit()
  • Image img tk.getImage("myimage.jpg")
  • MediaTracker mt new MediaTracker(this)
  • mt.addImage(img, 0) // any ID will do
  • try
  • mt.waitForAll()
  • catch (InterruptedException ie)
  • This is tedious!
  • suggest making a helper method to load and return
    one image
  • public Image loadImage(String filename)

22
Advanced anti-aliasing
  • Onscreen text and shapes can have jagged edges,
    or aliases. These can be removed by smoothing,
    or anti-aliasing, the panel.
  • Graphics2D
  • public void setRenderingHint(
    RenderingHints.Key key, Object value)
  • Exampleg2.setRenderingHint(RenderingHints.KEY_AN
    TIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
Write a Comment
User Comments (0)
About PowerShow.com