Anda di halaman 1dari 6

PRAKTIKUM 11

TEKS DAN AREA FILLING

11.1. Text
Text grafik terdiri dari 3 jenis:
1. Hadware text:
• Character yang tersimpan dalam terminal grafik.
• Digambar dengan cepat.
• Umumnya hanya tersedia satu ukuran dan terbatas.
2. Vector text
• Menghasilkan kualitas huruf yang tinggi / baik
• Setiap huruf terdiri dari sekumpulan polyline (stroke font)
• Ada yang dibuat dengan kurva yang kompleks seperti spline (true type font)
• Dapat di scaled, rotated , dll.
3. Bitmap Text.
• Setiap huruf tersimpan sebagai bitmap.
• Scaling menghasilkan huruf yang kasar.

1 2 3 4 5 6 7 8

1 1 1 1 1 1 0 0
0 1 1 0 0 1 1 0
0 1 1 0 0 1 1 0
0 1 1 1 1 1 0 0
0 1 1 0 0 1 1 0
0 1 1 0 0 1 1 0
1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0

Gambar 11.1. Pola Bit Teks


11.2. Area Filling
Adalah metode untuk memberikan warna/pola tertentu pada area dalam objek grafik yang
membentuk garis tertutup.

11.2.1. Boundary Fill (Flood Fill)


Kareteristik:
- Sederhana, mudah
- Prosedur rekursif, kemungkinan terjadi ‘stack overflow’
- Tidak dapat diterapkan pada display selain raster grafik. Misal pada Plotter.
- Bila titik awal di luar window, proses Fill tidak berfungsi.

4 – Titik tetangga 8 – Titik tetangga


(lebih baik)

= Titik Awal

= Titik Yang diperiksa

Gambar 11.2. Penentuan titik awal dan titik-titik tetangga

Algorithma :

Proc boundary_fill( x, y, fill_col, bound_col:integer);


Var
Pix_col : integer;
Begin
Pix_col := getcolor (x, y);
If (pix_col <> bound_col) and (pix_col <> fill_col) then
Begin
Putpixel(x, y, fill_col);
Boundary_fill( x+1, y, fill_col, bound_col);
.
.
.
end;
end;

11.2.2. Run Píxel

- Lebih baik dari metode Flood Fill ( Stack overflow kemungkinan lebih kecil)

Algorithma:
Push address of seed pixel on the stack;
While stack not empty Do
Begin
Pop the stack to provide the next seed;
Fill in the run defined by the seed;
Examine the row above for run reachable from this run;
Push the address of the right most pixels of each such run;
Do the same for row below the current run;
End;

Algorithma Run Piksel:


For each scan line do
Begin
Find the intersection of the scan line with all edges of the polygon.
Sort the intersections by increasing x – value
Fill pixels between consecutive pairs of intersections.
End.

Contoh: e6 e7

e1
e5
e4
3
e3 e2

0 1 2 7 9

Gambar 11.3. Contoh Run pixel dengan metode Scanline

- Sca line pada y=3 berpotongan dengan empat sisi e2, e3, e4, dan e5.
- Titik perpotongan pada x=1, 2, 7, 9.
- Fill : 1 ke 2, 7 ke 9.
- Poligon tersebut kemudian di scanline 1 s/d 9

Langkah Praktikum :
1) Aktifkan program Java Netbeans.
2) Pilih Project GrafikaKomputer dan klik kanan pada subfolder source package
GrafikaKomputer dan Pilih New dan Java Main Class baru seperti gambar 11.4.
berikut:
Gambar 11.4. Menambahkan Java main Class baru pada project

3) Ketik nama Class Aplikasi dengan Praktikum10, lalu klik Finish seperti gambar
11.5. berikut:

Gambar 11.5. Membuah Klas JFrame Form Baru pada project

4) Tambahkan kode berikut untuk Menggambar Text dan area filling , pada Frame
dengan ukuran 500 X 400 Piksel, sehingga code secara keseluluhan sebagai
berikut:

package grafikkomputer;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.geom.*;

public class Praktikum11 extends JApplet {

public static void main(String[] args) {


JFrame frame = new JFrame();
frame.setTitle("Text Grafik dan Filling");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new Praktikum11();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}
public void init() {
JPanel panel = new Panel2D();
getContentPane().add(panel);
}
class Panel2D extends JPanel{
public Panel2D() {
setPreferredSize(new Dimension(500, 400));
setBackground(Color.white);
}

public void paintComponent(Graphics g) {


super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;

// draw an ellipse
Shape ellipse = new Ellipse2D.Double(150, 100, 200, 200);
GradientPaint paint =
new GradientPaint(100,100, Color.white, 400, 400, Color.gray);
g2.setPaint(paint);
g2.fill(ellipse);

// set transparency
AlphaComposite ac =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
g2.setComposite(ac);
g2.setColor(Color.blue);

// draw transparent text


Font font = new Font("Serif", Font.BOLD, 120);
g2.setFont(font);
g2.drawString("Java", 120, 200);

// get outline of text glyph


FontRenderContext frc = g2.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, "Text");
Shape glyph = gv.getOutline(150,300);

// draw rotated glyph


g2.rotate(Math.PI/6, 200, 300);
g2.fill(glyph);
}

}
}
5) Jalankan Program sehingga hasilnya akan tampak seperti gambar 11.6. berikut:

Gambar 11.6. Hasil Penggambaran Text dan Area Filling

TUGAS:
Lakukan modifikasi program aplikasi area filling untuk bangun pentagonal dengan
warna fill merah seperti berikut.

==== Selamat Praktikum ===

Anda mungkin juga menyukai