Pengolahan Citra - Pemisahan Warna RGB
coding :
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.PixelGrabber;
public class ModelWarna3 extends Applet
{
Image img;
int lebar, tinggi;
int warna, red, green, blue, alpha, abuabu, tempwarna;
String tampil;
public void init()
{
img=this.getImage(this.getDocumentBase(), "wtp.jpg");
lebar = img.getWidth(this);
}
public void paint (Graphics g)
{
//beri warna pada panel
g.setColor(Color.lightGray);
g.fillRect(0,0,2800,800);
//ambil lebar dan tinggi citra
lebar = img.getWidth(this);
tinggi = img.getHeight(this);
//tampilkan informasi lebar dan tinggi citra
g.setColor(Color.black);
tampil = String.valueOf(lebar);
g.drawString("lebar: "+tampil,10,20);
tampil = String.valueOf(tinggi);
g.drawString("tinggi: "+tampil,10,40);
//tampilkan citra dengan drawImage
g.drawImage(img,10,50,this);
//tentukan panjang array pixels
int [] pixels = new int[lebar*tinggi];
PixelGrabber pg = new PixelGrabber(img,0,0,lebar,tinggi, pixels,0,lebar);
try
{
pg.grabPixels(); //grab piksel citra ke object pg.
}
catch (InterruptedException ie)
{
System.out.println("Terjadi kesalahan saat mengambil data pixels");
ie.printStackTrace();
return;
}
g.drawString("Hasil Pengolahan",lebar+100,20);
g.drawString("Normal",lebar+100,40);
g.drawString("Channel Red",lebar+100+lebar+50,40);
g.drawString("Channel Green",lebar+100+lebar+50+lebar+50,40);
g.drawString("Channel Blue", lebar+100+lebar+50+lebar+50+lebar+50, 40);
//bawah
g.drawString("Grayscale",lebar+100,185);
g.drawString("Channel Red",lebar+100+lebar+50,185);
g.drawString("Channel Green",lebar+100+lebar+50+lebar+50,185);
g.drawString("Channel Blue",lebar+100+lebar+50+lebar+50+lebar+50,185);
g.setColor(Color.black);
for(int j=0;j<tinggi;j++)
{
for(int i=0;i<lebar;i++)
{
warna = pixels[j*lebar+i];
alpha = (warna >> 24) & 0xff;
red = (warna >> 16) & 0xff;
green = (warna >> 8) & 0xff;
blue = (warna) & 0xff;
//normal
g.setColor(new Color(red, green, blue));
g.drawLine(i+lebar+100, j+50, i+lebar+100, j+50);
//red
g.setColor(new Color(red, 0, 0));
g.drawOval(i+lebar+100+lebar+50, j+50, 1,1);
//green
g.setColor(new Color(0, green, 0));
g.drawOval(i+lebar+100+lebar+50+lebar+50, j+50, 1,1);
//blue
g.setColor(new Color(0, 0, blue));
g.drawOval(i+lebar+100+lebar+50+lebar+50+lebar+50, j+50, 1,1);
//abu-abu
abuabu = (red+green+blue) / 3;
g.setColor(new Color(abuabu, abuabu, abuabu));
g.drawLine(i+lebar+100, j+200, i+lebar+100, j+200);
//red bawah
g.setColor(new Color(abuabu, 0, 0));
g.drawOval(i+lebar+100+lebar+50, j+200, 1,1);
//green bawah
g.setColor(new Color(0, abuabu, 0));
g.drawOval(i+lebar+100+lebar+50+lebar+50, j+200, 1,1);
//blue bawah
g.setColor(new Color(0, 0, abuabu));
g.drawOval(i+lebar+100+lebar+50+lebar+50+lebar+50, j+200, 1,1);
}
}
}
public void repaint (Graphics g)
{
//kosongkan saja
}
}
html :
<html>
<head>
<title>Model Warna 3</title>
</head>
<body>
<applet code=ModelWarna3.class width=1000 height=800>
</applet>
</body>
</html>
0 komentar:
Posting Komentar