Thursday, March 5, 2015

Program menampilkan gambar di java netbeans



 A.      Kode Program
memasukan gambar:
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

public class tugasfoto extends javax.swing.JFrame {
String one="src/foto/gambar1.jpg";
Image gb1=Toolkit.getDefaultToolkit().getImage(one);
String two="src/foto/gambar2.jpg";
Image gb2=Toolkit.getDefaultToolkit().getImage(two);
String three="src/foto/gambar3.jpg";
Image gb3=Toolkit.getDefaultToolkit().getImage(three);
int l=100;
int t=80;
Image satu;






1.       Tombol Besarkan
private void besarActionPerformed(java.awt.event.ActionEvent evt) {                                     
 Graphics g=canvas1.getGraphics();
        g.setColor(canvas1.getBackground());
        g.fillRect(1,1,canvas1.getWidth()-1,canvas1.getHeight()-1);
        g.drawImage(img,10,10,l,t,this);
        l=l+55;
        t=t+44;
        // TODO add your handling code here:
    }                
                   
2.       Tombol Kecilkan

private void kecilActionPerformed(java.awt.event.ActionEvent evt) {                                     
  Graphics g=canvas1.getGraphics();
        g.setColor(canvas1.getBackground());
        g.fillRect(1,1,canvas1.getWidth()-1,canvas1.getHeight()-1);
g.drawImage(img,10,10,l,t,this);
        l=l-55;
        t=t-44;
        // TODO add your handling code here:
    }                                    

3.       Tombol tampil

private void tampilActionPerformed(java.awt.event.ActionEvent evt) {                                       
   Graphics g=canvas1.getGraphics();
     g.setColor(canvas1.getBackground());
     if(pilihan.getSelectedItem()== "gambar 1")
     {img=gb;
      g.fillRect(1,1,canvas1.getWidth()-1,canvas1.getHeight()-1);
     g.drawImage(gb,0,0,this);
     }
else
      if(pilihan.getSelectedItem()== "gambar 2")
     {img=gb2;
      g.fillRect(1,1,canvas1.getWidth()-1,canvas1.getHeight()-1);
     g.drawImage(gb2,0,0,this);
     }
else
      if(pilihan.getSelectedItem()== "gambar 3")
     {img=gb3;
      g.fillRect(1,1,canvas1.getWidth()-1,canvas1.getHeight()-1);
     g.drawImage(gb3,0,0,this);         
     }
        // TODO add your handling code here:
    } 
4.       Tombol Sembunyi
private void sembunyiActionPerformed(java.awt.event.ActionEvent evt) {                                         
 Graphics g=canvas1.getGraphics();
        g.setColor(canvas1.getBackground());
        g.fillRect(1,1,canvas1.getWidth()-1,canvas1.getHeight()-1);
        // TODO add your handling code here:
    } 

5.       Combo Box
private void pilihanActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
    }   


hasil runing :


























1 comment: