Graphics: Drawing Fundamentals - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Graphics: Drawing Fundamentals

Description:

... a basic graphics ... specify coordinates and to draw text, shapes, and images ... Drawing Shapes. public class ShapeSample extends JPanel{ public void ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 14
Provided by: drtimm8
Category:

less

Transcript and Presenter's Notes

Title: Graphics: Drawing Fundamentals


1
Graphics Drawing Fundamentals
  • Dr. Tim Margush
  • University of Akron

2
Goals
  • Perform simple drawing tasks in Java

3
Frames
  • A Frame is the container for an application's GUI
  • GUI Graphical User Interface
  • Frame is a class in java.awt providing a title
    bar and a border
  • JFrame is a class in javax.swing extending Frame
    adding support for JFC/Swing components

4
Instantiating JFrames
  • JFrame jf new JFrame("Sample Frame Title")
  • jf.setSize(200, 300)
  • jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • jf.setVisible(true)
  • A Frame (JFrame) has properties for its
  • title (passed via constructor or setTitle method)
  • size (width and height)
  • location (default location used, setLocation(x,y)
  • behavior (usually exits application when closed)
  • visibility (starts hidden)

5
Adding Content to JFrames
  • Graphical content is displayed in the JFrame's
    content pane
  • A drawing component is added to this pane using
    the add method
  • jf.add(someComponent)
  • .add(someComponent) adds a Component object to
    the Container.

6
Graphical Hierarchy
Component
A small subset of the hierarchy tree
Container
Button
CheckBox
JComponent
Panel
JPanel
JScrollPane
JOptionPane
Applet
JApplet
7
Drawing on Components
  • Use a JPanel for a basic graphics canvas
  • Graphics commands are executed from the
    paintComponent method
  • paintComponent is called by the graphics system
    when it appears the canvas needs to be refreshed
  • public void paintComponent(Graphics g)
  • The java.awt.Graphics object is provided by the
    system when this method is called
  • You NEVER directly call this method

8
java.awt.Graphics
  • The Graphics object represents the canvas where
    drawing can occur
  • Methods include
  • drawArc, drawOval, fillRect, drawPolygon
  • fillArc, fillOval, fillRect, fillPolygon
  • drawImage, drawLine
  • setColor
  • setFont, drawString

9
java.awt.Graphics2D
  • A subclass of Graphics adding new and improved
    methods
  • public void paintComponent(Graphics g)
  • //g is actually a Graphics2D object when
  • // supplied by a swing component
  • Graphics2D g2 (Graphics2D) g //cast
  • Graphics2D objects have methods
  • draw, drawString, fill, draw3DRect, setPaint,
    setStroke, setBackground,

10
Coordinate System
(0,0)
Increasing X coords
72 dpi
  • Coordinate system
  • Top left is (0,0)
  • X increases to the right
  • Y increases downward
  • (over, down)
  • Methods exist to alter the transform used to
    specify coordinates and to draw text, shapes, and
    images

Increasing Y coords
11
Customizing a JPanel
  • public class NameComponent extends JPanel
  • public void paintComponent(Graphics g)
  • super.paintComponent(g) //allows proper
    initialization of the canvas
  • Graphics2D g2 (Graphics2D) g //if using 2D
    extensions
  • //Add your custom drawing commands here
  • g2.setFont(new Font("SansSerif", Font.ITALIC,
    24))
  • g2.drawString("Java Jerry", 20, 40)
  • jf.add(new NameComponent())
  • jf.setVisible(true)

12
Drawing Lines
  • public class LineSample extends JPanel
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • int w getWidth() //gets the width of the
    panel itself
  • int h getHeight() //gets the height of the
    panel itself
  • //Add your custom drawing commands here
  • g.drawLine(0,0,w,h)
  • g.drawLine(0,h/2,w,h/2)
  • g.drawLine(w/2,0,w/2,h)

13
Drawing Shapes
  • public class ShapeSample extends JPanel
  • public void paintComponent(Graphics g)
  • super.paintComponent(g)
  • Graphics2D g2 (Graphics2D) g
  • //Add your custom drawing commands here
  • g2.setColor(Color.RED)
  • g2.fill(new Rectangle(20, 50, 100, 30))
  • g2.setColor(Color.BLUE)
  • g2.draw(new Rectangle(10, 75, 25, 100))
Write a Comment
User Comments (0)
About PowerShow.com