Menaruh Gambar pada JPanel


langsung aja gimana cara meletakan/menaruh gambar di atas JPanel.

  • buat project dan class baru lalu namai class baru dengan PanelImages
  • kemudian ubah class PanelImages seperti berikut :

public class PanelImages extends JPanel {

private Image image;

public PanelImages() {
image = new ImageIcon(getClass().getResource("/Images/bug.png")).getImage();
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
  • buat lagi class baru lalu namai class Main lalu ubah kode classnya menjadi :

public class Main {

public static void main(String[] args) {
JFrame frame = new JFrame("Images");
PanelImages panelImages = new PanelImages();
frame.add(panelImages);
frame.setPreferredSize(new Dimension(400, 300));
frame.pack();
frame.setVisible(true);
}
}

kemudian jalankan programnya. cara lain bisa dengan men-drag and drop class PanelImages ke dalam JFrame..

Selamat Mencoba!! 😀

Advertisements

One thought on “Menaruh Gambar pada JPanel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s