Coding untuk bentuk hasil seperti ini :
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.PixelGrabber;
public class NgeGrabPixel2a 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(), "kucing.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("pakai drawLine",lebar+100,40);
g.drawString("pakai drawOval",lebar+100+lebar+50,40);
g.drawString("pakai drawRect",lebar+100,185);
g.drawString("abu-abu", lebar+100+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;
g.setColor(new Color(red, green, blue));
g.drawLine(i+lebar+100,j+50, i+lebar+100, j+50);
g.drawLine(i+lebar+100+lebar+50,j+50,i+lebar+100+lebar+50,j+50);
g.drawLine(i+lebar+100,j+50+tinggi+50,i+lebar+100,j+50+tinggi+50);
abuabu = (red+green+blue) / 3;
g.setColor(new Color(abuabu, abuabu, abuabu));
g.drawLine(i+lebar+100+lebar+50,j+50+tinggi+50,i+lebar+100+lebar+50,j+50+tinggi+50);
}
}
}
public void repaint (Graphics g)
{
//kosongkan saja
}
}
source code untuk file html sama seperti sebelumnya hanya beda pada nama file classnya yang sama dengan file java.
tampilan kedua :
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.PixelGrabber;
public class NgeGrabPixel2b 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(), "kucing.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("pakai drawLine",lebar+100,40);
g.drawString("pakai drawOval",lebar+100,190);
g.drawString("pakai drawRect",lebar+100,340);
g.drawString("abu-abu", lebar+100,490);
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;
g.setColor(new Color(red, green, blue));
g.drawLine(i+lebar+100,j+50, i+lebar+100, j+50);
g.drawLine(i+lebar+100,j+50+lebar+50,i+lebar+100,j+50+lebar+50);
g.drawLine(i+lebar+100,j+50+lebar+50+lebar+50,i+lebar+100,j+50+lebar+50+lebar+50);
abuabu = (red+green+blue) / 3;
g.setColor(new Color(abuabu, abuabu, abuabu));
g.drawLine(i+lebar+100,j+50+lebar+50+lebar+50+lebar+50,i+lebar+100,j+50+lebar+50+lebar+50+lebar+50);
}
}
}
public void repaint (Graphics g)
{
//kosongkan saja
}
}
Senin, 12 September 2016
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar