2D Graphics in Java - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

2D Graphics in Java

Description:

runs in JVM directly under the OS. Java applets are browser programs ... draw text: g.drawString('Print This!', 20, 40) string to draw, (x1,y1) top left postion ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 11
Provided by: mdoh2
Category:

less

Transcript and Presenter's Notes

Title: 2D Graphics in Java


1
2D Graphics in Java
  • COMP53
  • Nov 14, 2007

2
Applets and Applications
  • Java applications are stand-alone programs
  • start execution with main()
  • runs in JVM directly under the OS
  • Java applets are browser programs
  • start in class derived from JApplet
  • event driven
  • runs in JVM within the browser

3
Basic Applet
// Java core packages import java.awt. // Java
extension packages import javax.swing. public
class BlankGraphicsApplet extends JApplet
public void paint(Graphics g) // call
superclass's paint method (paints background)
super.paint(g) // Here's where we'd put
// application specific drawing instructions

4
Embedding Applet in Web Page
lthtmlgt ltapplet code"BlankGraphicsApplet.class"
width"600" height"400"gt lt/appletgt lt/htmlgt
5
Basic Graphics Application
import java.awt. import java.awt.event. import
javax.swing. public class BlankGraphicsFrame
extends JFrame public BlankGraphicsFrame()
super("Blank Graphics Frame")
setSize(600, 400) show() public void
paint(Graphics g) super.paint(g) //
Here's where we'd put application specific
drawing instructions public static void
main(String args) BlankGraphicsFrame
application new BlankGraphicsFrame()
application.setDefaultCloseOperation(JFrame.EXIT_O
N_CLOSE )
6
When to paint()?
  • System calls paint, usually for one of the
    following reasons
  • The component is first made visible on the
    screen.
  • The component is resized.
  • The component has damage that needs to be
    repaired. For example, a covering window has been
    moved.
  • The application calls paint, because its
    internal state has changed.
  • a button press triggers change in graphics
  • animated objects move or change

7
Basics of Drawing
  • Drawing commands are methods of the Graphics
    object passed to paint()
  • Coordinates are from top left of window

x increase to right
y increase downward
8
Selecting a Drawing Color
  • predefined color
  • g.setColor(Color.red)
  • orange, pink, cyan, magenta, yellow, black,
    white, gray, lightGray, darkGray, red, green,
    blue
  • RGB defined color
  • g.setColor(new Color(200,20,250))
  • red, green and blue in range 0 to 255
  • RGB defined color
  • g.setColor(new Color(0.7,0.1,0.8))
  • red, green and blue in range 0.0 to 1.0

9
Basic Shapes
  • rectangles and ovals
  • (x1,y1) is upper left corner of shape
  • g.drawRect(x1,y1,width,height)rectangle outline
  • g.fillRect(x1,y1,width, height)solid rectangle
  • g.drawOval(x1,y1,width,height)oval outline
  • g.fillOval(x1,y1,width, height)solid oval
  • g.drawLine(x1,y1,x2,y2)line from (x1, y1) to
    (x2,y2)

10
Text
  • select fontg.setFont(new Font(Arial,
    Font.BOLD, 12))font name, font attributes, font
    size
  • draw textg.drawString(Print This!, 20,
    40)string to draw, (x1,y1) top left postion
Write a Comment
User Comments (0)
About PowerShow.com