Kuliah 8 - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Kuliah 8

Description:

This method set a clipping path. Only the portions of a Shape or Image object that lies within the clipping path are displayed. ... – PowerPoint PPT presentation

Number of Views:150
Avg rating:3.0/5.0
Slides: 36
Provided by: nurulfa
Category:
Tags: clipping | kuliah

less

Transcript and Presenter's Notes

Title: Kuliah 8


1
Kuliah 8
  • Java 2D API

2
Java 2D API
  • The Java 2D API provides enhanced
    two-dimensional graphics, text, and imaging
    capabilities for programmers who required
    detailed and complex graphical manipulations.
  • Java 2D API provides the following general
    capabilities
  • General graphics transformations (such as
    rotation, scaling, and translation) are
    supported.
  • Image processing and display - Advanced image
    creation, filtering, composition, and display.
  • Advanced text rendering and font support.

3
Java 2D API
  • Mechanisms for performing hit detection on
    shapes, text, and images.
  • A uniform rendering model for display devices and
    printers.
  • Advanced color definition.
  • The Java 2D API extends the Graphics class with
    the Graphics2D class.
  • Moving from Graphics to Graphics2D
  • public void Paint (Graphics g)
  • Graphics2D g2 (Graphics2D) g ...

4
Java 2D ShapesLine 2D
  • Kelas java.awt.geom.Line2D dapat mewakili objek
    garis 2D.
  • 2 kelas yang dapat cipta objek garis ialah
    Line2D.Float dan Line2D.Double
  • Perbezaan kedua kelas ini ialah jenis data yang
    digunakan.

5
  • public Line2D.Double(double x1, double y1,
    double x2, double y2)
  • public Line2D.Float( float x1, float y1, float
    x2, float y2)

6
Contoh
  • Cipta objek garis daripada titik (10,10) hingga
    (100, 70)
  • Graphics2D g2D (Graphics2D) g
  • Line2D L2D new Line2D.Float (10F,10F, 100F,
    70F)
  • g2D.draw ( L2D)
  • or
  • Line2D L2D1 new Line2D.Double (10,10, 100, 70)
  • g2D.draw ( L2D)

7
Java 2D Shapes
  • The three major operations that can be performed
    on a shape are drawing, filling and clipping.
  • Stroking
  • BasicStroke(float width, int cap, int join,
    float dash, float dashphase)
  • This class specify the width of line (width),
    how the line end (cap) eg. Cap_BUTT, CAP_ROUND,
    CAP_SQUARE, how line join together (join) and
    dash attributes of the line.
  • setStroke(new BasicStroke())

8
Contoh
  • 1. g2D.setPaint(Color.red)
  • g2D.setStroke(new BasicStroke( 4,
    BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
    10, 0)
  • g2D.draw ( new Line2D.Double ( 395, 30,
    320, 150)
  • 2. g2D.setStroke ( new BasicStroke ( 6.0f) )
  • g2D.draw ( new Line2D.Double ( 395, 30, 320,
    150)

9
Latihan
  • Lukiskan satu garisan 2 D yang berwarna merah
    dengan tebal garisan 4 piksel. Garisan dari
    koordinat ( 395, 30) ke (320, 150).

10
Jawapan
  • g2D.setPaint ( Color.red)
  • g2D.setStroke ( new BasicStroke ( 4.0f))
  • g2D.draw ( new Line2D.Double ( 395, 30, 320, 150)

11
Rectangle 2D
  • Objek segiempat dicipta dari kelas
    java.awt.geom.Rectangle2D.
  • 2 kelas yang digunakan ialah Rectangle2D.Float
    dan Rectangle2D.Double.
  • Metod pembina adalah
  • public Rectangle2D.Double ( double x, double y,
    double lebar, double tinggi)
  • public Rectangle2D.Float ( float x, float y,
    float lebar, float tinggi)

12
Contoh
  • Rectangle2D s4 new Rectangle2D.Double (10, 30,
    70, 40)
  • Melukis segiempat 2D
  • g2D.draw(s4)
  • Mewarna segiempat 2D
  • g2D.fill(s4)

13
Latihan
  • Lukiskan satu segiempat 2D yang mempunyai garisan
    tepi berwarna kuning pada titik koordinat (80,
    30), lebar 65 dan tinggi 100. Lebar garisan
    adalah 10 piksel.

14
Jawapan
  • g2D.setPaint ( Color.yellow)
  • g2D.setStroke ( new BasicStroke( 10.0f))
  • g2D.draw( new Rectangle2D.Double ( 80, 30, 65,
    100)

15
Latihan
  • Lukiskan satu segiempat 2D yang berwarna biru
    pada titik koordinat (80, 30), lebar 65 dan
    tinggi 100.

16
Jawapan
  • setColor( Color.blue)
  • g2D.fill( new Rectangle2D.Double ( 80, 30, 65,
    100)

17
Round Rectangle
  • public RoundRectangle2D.Double (double x, double
    y, double lebar, double tinggi, double
    lebarLengkuk, double tinggiLengkuk)
  • public RoundRectangle2D.float (float x, float y,
    float lebar, float tinggi, float lebarLengkuk,
    float tinggiLengkuk)

18
Contoh
  • g2d.draw ( new RoundRectangle2D.Double( 155, 30,
    75, 100, 50, 50))
  • g2D.setColor( Color.green)
  • g2d.fill( new RoundRectangle2D.Double( 155, 30,
    75, 100, 50, 50))

19
Ellipse 2D
  • public Ellipse2D.Float (float x, float y, float
    lebar, float tinggi)
  • public Ellipse2D.Double (double x, double y,
    double lebar, double tinggi)

(x,y)
tinggi
lebar
20
Contoh
  • g2D.draw ( new Ellipse2D.Double (5, 30, 65, 100))

21
Arc2D
  • public Arc2D.Double(double x, double y, double
    lebar, double, tinggi, double sudutMula,double
    sudutLengkuk, int jenisLengkuk)
  • Jenis lengkuk

Arc2D.CHORD
Arc2D.PIE
Arc2D.OPEN
22
Contoh
  • g2D.draw( new Arc2D.Double ( 240, 30, 75, 100, 0,
    270, Arc2D.PIE))

23
Java 2D Shapes
  • Filling
  • GradientPaint(float x1, float y1, Color color1,
    float x2, float y2, Color color2, boolean cyclic)
  • - This class draw a shape in a gradually changing
    colors.
  • setPaint(new GradientPaint())

24
Contoh
  • g2d.setPaint(
  • new GradientPaint (5, 30, //x1,y1
  • Color.blue,// initial color
  • 35,100, //x2,y2
  • Color.yellow,//end color
  • true)) // cyclic

25
Latihan
  • Lukiskan satu segiempat 2D bucu melengkung yang
    berwarna gradian merah ke biru. Titik permulaan
    gradian adalah ( 155, 30) dan koordinat tamat
    gradian adalah ( 80, 100).

26
  • g2d.setPaint(
  • new GradientPaint (155, 30, //x1,y1
  • Color.red,// initial color
  • 80,100, //x2,y2
  • Color.blue,//end color
  • false)) // cyclic
  • g2d.fill ( new RoundRectangle2D.Double( 155, 30,
    75, 100, 50, 50))

27
Image
  • BufferedImage(int wide, int tall, int imagetype)
  • - The imagetype can be color or in gray scale
  • CreateGraphics()
  • - create Graphics2D object for BufferedImage.

28
Java 2D Shapes
  • TexturePaint(Buffered image, Rectangle control)
  • - This class use the Buffered Image as the fill
    texture for the filled in shape.
  • Clipping
  • setClip(shape object)
  • In some cases, you may not want to display an
    entire shape. This method set a clipping path.
    Only the portions of a Shape or Image object that
    lies within the clipping path are displayed.

29
Contoh
  • BufferedImage buffImage new BufferedImage (
    10,10, BufferedImage.TYPE_INT_RGB)
  • Graphics2D gg buffImage.createGraphics()
  • gg.setColor( Color.yellow)
  • gg.fillRect ( 0, 0, 10, 10)
  • gg.setColor( Color.black)
  • gg.drawRect ( 1, 1, 6, 6)

30
  • gg.setColor( Color.blue)
  • gg.fillRect ( 1, 1, 3, 3)
  • gg.setColor( Color.red)
  • gg.fillRect ( 4,4, 3, 3)
  • g2D. setPaint( new TexturePaint (
  • buffImage, new Rectangle ( 10, 10)))
  • g2D.fill ( new RoundRect2D.Double( 155, 30, 75,
    100, 50, 50)

31
Java 2D Shapes
  • A general path is a shape constructed from
    straight lines and complex curves. Class
    GeneralPath ( package java.awt.geom).
  • void moveTo (float x, float y)
  • Moves to specified point.
  • void lineTo(float x, float y)
  • Drawing a line segment to the specified point.
  • void closepath()
  • Drawing a line from the last point to the last
    call of moveTo

32
Java 2D Shapes
  • void translate(float x, float y)
  • Move the drawing origin to new location.
  • void rotate(float radians)
  • Rotate the next displayed shape in radians.

33
Contoh
  • import java.awt.
  • import javax.swing.
  • import java.awt.geom.
  • public class Poligon extends JApplet
  • Graphic2D g2d (Graphics2D) g
  • int x 90, 85, 140, 185, 200, 200, 185,
    140, 85
  • int y 90, 85, 30, 75, 60, 120, 105, 150,
    95

34
  • GeneralPath poli new GeneralPath ()
  • poli.moveTo(x0, y 0)
  • for (int i 1 i lt x.length i )
  • poli.lineTo (xi, yi)
  • poli.closePath()
  • g2d.draw(poli)

35
output
Write a Comment
User Comments (0)
About PowerShow.com