program project khusus

127
Npm : 201243501128 Nama : Kiuntoro Kelas : R7H Mata Kuliah : Komputer Grafik Dosen : Nahot Frastian , M.Kom Program Studi : Teknik Informatika Universitas : Universitas Indraprasta PGRI 1. Aplikasi Program Project Khusus Saya menggunakan 14 kelas dalam membuat aplikasi ini 1) Menu 5) oval 9) Lukisan 13) Image zoomer 2) Utama 6) rect 10) Catur 14) Image zoomer Frame 3) Design 7) roundrect 11) empat jam

Upload: kiuntoro

Post on 07-Jan-2017

93 views

Category:

Technology


0 download

TRANSCRIPT

Npm : 201243501128

Nama : Kiuntoro

Kelas : R7H

Mata Kuliah : Komputer Grafik

Dosen : Nahot Frastian , M.Kom

Program Studi : Teknik Informatika

Universitas : Universitas Indraprasta PGRI

1. Aplikasi Program Project Khusus

Saya menggunakan 14 kelas dalam membuat aplikasi ini

1) Menu 5) oval 9) Lukisan 13) Image zoomer

2) Utama 6) rect 10) Catur 14) Image zoomer Frame

3) Design 7) roundrect 11) empat jam

4) arc 8) orang 12) Grafik gempa

Program Komputer Grafik :

1) Program Menu

Input:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.text.*;

import javax.swing.Timer;

import javax.swing.ImageIcon;

public class menu extends JFrame implements ActionListener {

private static Color rect = new Color (51,51,255) ; private static

Color roundrect = new Color (255,51,51) ; private static Color oval = new

Color (255,102,255) ;

private static Color arc = new Color (51,255,51) ; private static Color

usb = new Color (102,255,51) ; private static Color putih = new Color

(255,255,255) ;

JPanel ToolbarPanel1 = new JPanel(new

FlowLayout(FlowLayout.LEFT));

JMenuBar mb = new JMenuBar () ;

JMenu about = new JMenu (" About ") ;

JDesktopPane pane = new JDesktopPane();

JButton btnrect = new JButton (" Rectangle ");

JButton btnroundrect = new JButton (" Round Rectangle ");

JButton btnoval = new JButton (" Oval ");

JButton btnarc = new JButton (" Arc ");

JButton btnorang = new JButton (" Orang (kreatif 2D) ");

JButton btnlukisan = new JButton (" Lukisan ");

JButton btncatur = new JButton (" Catur ");

JToolBar tb = new JToolBar ();

JTextField nama = new JTextField ();

menu () {

setTitle (" Program Komputer Grafik " );

setSize (1370,730);

// setLayout(null);

pane = new utama ();

addWindowListener (new WindowAdapter () {

public void windowClosing (WindowEvent we) {

Keluar ();

}

});

setJMenuBar (mb);

mb.add(about);

// add(pane);

ToolbarPanel1.add (btnrect);

ToolbarPanel1.add (btnroundrect);

ToolbarPanel1.add (btnoval);

ToolbarPanel1.add (btnarc);

ToolbarPanel1.add (btnorang);

ToolbarPanel1.add (btnlukisan);

ToolbarPanel1.add (btncatur);

tb.add (ToolbarPanel1);

pane.setBackground(Color.gray);

getContentPane().add (tb, BorderLayout.NORTH);

getContentPane().add (pane, BorderLayout.CENTER);

btnrect.setToolTipText ("Rectangle");

btnroundrect.setToolTipText ("Round Rectangle");

btnoval.setToolTipText ("Oval");

btnarc.setToolTipText ("Arc");

btnorang.setToolTipText ("");

btnlukisan.setToolTipText ("Lukisan");

btncatur.setToolTipText (" Catur ");

btnrect.addActionListener (this);

btnroundrect.addActionListener (this);

btnoval.addActionListener (this);

btnarc.addActionListener (this);

btnorang.addActionListener (this);

btnlukisan.addActionListener (this);

btncatur.addActionListener (this);

about.addActionListener (this);

// warna background

btnrect.setBackground (rect); btnroundrect.setBackground

(roundrect); btnoval.setBackground (oval); btnarc.setBackground (arc);

btnorang.setBackground (usb); btnlukisan.setBackground (rect);

btncatur.setBackground (roundrect);

//warna foreground

btnrect.setForeground (putih); btnroundrect.setForeground (putih);

btnoval.setForeground (putih); btnarc.setForeground (putih);

btnorang.setForeground (putih); btnlukisan.setForeground (putih);

btncatur.setForeground (putih);

btnrect.setFont(new Font("Times New Roman",Font.PLAIN,14));

btnroundrect.setFont(new Font("Times New Roman",Font.PLAIN,14));

btnoval.setFont(new Font("Times New Roman",Font.PLAIN,14));

btnarc.setFont(new Font("Times New Roman",Font.PLAIN,14));

btnorang.setFont(new Font("Times New

Roman",Font.PLAIN,14)); btnlukisan.setFont(new Font("Times New

Roman",Font.PLAIN,14));

btncatur.setFont(new Font("Times New

Roman",Font.PLAIN,14));

setVisible(true);

}

private void Keluar () {

try {

int reply = JOptionPane.showConfirmDialog (this,

"Yakin Mau Keluar?",

"Terima Kasih ", JOptionPane.YES_NO_OPTION,

JOptionPane.PLAIN_MESSAGE);

if (reply == JOptionPane.YES_OPTION) {

System.exit(0);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

catch (Exception e) {

}

}

public static void main ( String [] args ) {

new menu();

}

public void actionPerformed (ActionEvent evt) {

try{

if (evt.getSource()==btnrect) {

design kg = new design () ;

kg.getTxtx1().setVisible (true); kg.getTxtx2().setVisible

(true);kg.getTxtwidth().setVisible (true);

kg.getTxtheight().setVisible (true);

kg.getTxtstatus().setText("1");

kg.getBtnproses().setVisible (true) ;

kg.getBtnproses().setEnabled (true) ;

kg.getLblx1().setVisible (true); kg.getLblx2().setVisible

(true);kg.getLblwidth().setVisible (true);

kg.getLblheight().setVisible (true); kg.setTitle (" Komputer

Grafik > Rectangle ");

}

if (evt.getSource()==btnroundrect) {

design kg = new design () ;

kg.getTxtx1().setVisible (true); kg.getTxtx2().setVisible

(true);kg.getTxtwidth().setVisible (true);

kg.getTxtheight().setVisible (true); kg.getTxtxdiam().setVisible

(true); kg.getTxtydiam().setVisible (true); kg.getTxtstatus().setText("2");

kg.getBtnproses().setVisible (true) ;

kg.getBtnproses().setEnabled (true ) ;

kg.getLblx1().setVisible (true); kg.getLblx2().setVisible

(true);kg.getLblwidth().setVisible (true);

kg.getLblheight().setVisible (true); kg.getLblxdiam().setVisible

(true); kg.getLblydiam().setVisible (true);

kg.setTitle (" Komputer Grafik > Round Rectangle ");

}

if (evt.getSource()==btnoval) {

design kg = new design () ;

kg.getTxtx1().setVisible (true); kg.getTxtx2().setVisible

(true);kg.getTxtwidth().setVisible (true);

kg.getTxtheight().setVisible (true);

kg.getTxtstatus().setText("3");

kg.getBtnproses().setVisible (true) ;

kg.getBtnproses().setEnabled (true) ;

kg.getLblx1().setVisible (true); kg.getLblx2().setVisible

(true);kg.getLblwidth().setVisible (true);

kg.getLblheight().setVisible (true); kg.setTitle (" Komputer

Grafik > Oval ");

}

if (evt.getSource()==btnarc) {

design kg = new design () ;

kg.getTxtx1().setVisible (true); kg.getTxtx2().setVisible

(true);kg.getTxtwidth().setVisible (true);

kg.getTxtheight().setVisible (true); kg.getTxtxdiam().setVisible

(true); kg.getTxtydiam().setVisible (true); kg.getTxtstatus().setText("4");

kg.getBtnproses().setVisible (true) ;

kg.getBtnproses().setEnabled (true ) ;

kg.getLblx1().setVisible (true); kg.getLblx2().setVisible

(true);kg.getLblwidth().setVisible (true);

kg.getLblheight().setVisible (true); kg.getLblxdiam().setVisible

(true); kg.getLblydiam().setVisible (true);

kg.getLblxdiam().setText("StartAngle");

kg.getLblydiam().setText("arcAngle"); kg.setTitle (" Komputer Grafik >

Arc ");

}

if (evt.getSource()==btnorang) {

JFrame frame = new JFrame();frame.setTitle("Usb");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JApplet applet = new orang(); applet.init();

frame.getContentPane().add(applet); frame.pack(); frame.setVisible(true);

}

if (evt.getSource()==btnlukisan) {

JFrame frame = new JFrame(); frame.setTitle("Catur");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JApplet applet = new lukisan(); applet.init();

frame.getContentPane().add(applet);frame.pack(); frame.setVisible(true);

}

if (evt.getSource()==btncatur) {

JFrame frame = new JFrame(); frame.setTitle(" Grafik Garis ");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JApplet applet = new grafikgaris(); applet.init();

frame.getContentPane().add(applet);frame.pack(); frame.setVisible(true);

}

} catch(Exception e) {

System.err.println("error"+e.getMessage());

}

}

}

Proses:

- Pada program menu, berfungsi sebagai penghubung program arc, rect,

round rect, oval, orang, lukisan, dan catur.

Output:

2) Program Utama

Input:

import javax.swing.JDesktopPane;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Image;

import javax.swing.ImageIcon;

public class utama extends JDesktopPane {

protected void paintComponent(Graphics g) {

try {

Graphics2D graphics = (Graphics2D) g.create();

Image img = new

ImageIcon(getClass().getResource("penguins.jpg")).getImage();

graphics.drawImage(img, 0, 0, getWidth(), getHeight(), null);

graphics.dispose();

}catch ( Exception e ){

System.exit(0);

}

}

public static void main ( String [] args ) {

try {

new menu();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Proses:

- Pada program utama, kita bisa mendesign background didalam dengan

memanggil gambar yang telah kita simpan di dalam sebuah folder yang

sama dengan program kita.

- Terhubungan dengan program menu

Output:

3) Program Design

Input:

import javax.swing.*;

import java.awt.event.*;

import java.sql.*;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import javax.swing.table.*;

import java.awt.*;

import java.util.Date;

import javax.swing.border.EmptyBorder;

import java.awt.Graphics ;

public class design extends JFrame implements ActionListener {

// JLabel info = new JLabel ( " * cari : case sensitive");

JPanel panel = new JPanel ();

JLabel lblx1 = new JLabel (" x1 ");

JLabel lblx2 = new JLabel (" x2 ");

JLabel lblwidth = new JLabel (" width ");

JLabel lblheight = new JLabel (" height ");

JLabel lblxdiam = new JLabel (" xdiam ");

JLabel lblydiam = new JLabel (" ydiam ");

JLabel lblWall = new JLabel ();

JTextField txtstatus = new JTextField ();

JTextField txtx1 = new JTextField ();

JTextField txtx2 = new JTextField ();

JTextField txtwidth = new JTextField ();

JTextField txtheight = new JTextField ();

// JTextField txtjabatan = new JTextField ();

JTextField txtxdiam = new JTextField ();

JTextField txtydiam = new JTextField ();

JButton btnproses = new JButton (" Proses ");

public design(){

super (" Komputer Grafik ");

setSize (430,425);

panel.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(panel);

panel.setLayout(null);

// info.setBounds (400,50,150,25);

lblx1.setBounds (50,50,75,25);

lblx2.setBounds (50,100,75,25);

lblwidth.setBounds (50,150,75,25);

lblheight.setBounds (50,200,75,25);

lblxdiam.setBounds (50,250,75,25);

lblydiam.setBounds (50,300,75,25);

txtx1.setBounds (110,50,175,25);

txtx2.setBounds (110,100,175,25);

txtwidth.setBounds (110,150,175,25);

txtheight.setBounds (110,200,175,25);

txtxdiam.setBounds (110,250,175,25);

txtydiam.setBounds (110,300,175,25);

btnproses.setBounds (300,50,100,25);

add(lblx1);

add(lblx2);

add(lblwidth);

add(lblheight);

add(lblxdiam);

add(lblydiam);

add(txtx1);

add(txtx2);

add(txtwidth);

add(txtheight);

add(txtxdiam);

add(txtydiam);

add(btnproses);

lblx1.setForeground(Color.white);

lblx2.setForeground(Color.white);

lblwidth.setForeground(Color.white);

lblheight.setForeground(Color.white);

lblxdiam.setForeground(Color.white);

lblydiam.setForeground(Color.white);

lblWall.setIcon(new

ImageIcon(design.class.getResource("bg.jpg")));

lblWall.setBounds(0, 0, 430, 500);

panel.add(lblWall);

setLocationRelativeTo(null);

setVisible(true);

btnproses.addActionListener(this);

txtx1.setVisible(false); txtx2.setVisible(false);

txtwidth.setVisible(false);

txtheight.setVisible(false); txtxdiam.setVisible(false);

txtydiam.setVisible(false);

btnproses.setVisible(false) ;

lblx1.setVisible(false); lblx2.setVisible(false);

lblwidth.setVisible(false);

lblheight.setVisible(false); lblxdiam.setVisible(false);

lblydiam.setVisible(false);

}

public void setTxtstatus ( JTextField txtstatus ) {

this.txtstatus = txtstatus ;

}

public JTextField getTxtstatus () {

return txtstatus ;

}

public void setTxtx1 ( JTextField txtx1 ) {

this.txtx1 = txtx1 ;

}

public JTextField getTxtx1 () {

return txtx1 ;

}

public void setTxtx2( JTextField txtx2 ) {

this.txtx2 = txtx2 ;

}

public JTextField getTxtx2 () {

return txtx2 ;

}

public void setTxtwidth ( JTextField txtwidth ) {

this.txtwidth = txtwidth ;

}

public JTextField getTxtwidth () {

return txtwidth ;

}

public void setTxtheight ( JTextField txtheight ) {

this.txtheight = txtheight ;

}

public JTextField getTxtheight () {

return txtheight ;

}

public void setTxtxdiam ( JTextField txtxdiam ) {

this.txtxdiam = txtxdiam ;

}

public JTextField getTxtxdiam () {

return txtxdiam ;

}

public void setTxtydiam ( JTextField txtydiam ) {

this.txtydiam = txtydiam ;

}

public JTextField getTxtydiam () {

return txtydiam ;

}

// label

public void setLblx1 ( JLabel lblx1 ) {

this.lblx1 = lblx1 ;

}

public JLabel getLblx1 () {

return lblx1 ;

}

public void setLblx2 ( JLabel lblx2 ) {

this.lblx2 = lblx2 ;

}

public JLabel getLblx2 () {

return lblx2 ;

}

public void setLblwidth ( JLabel lblwidth ) {

this.lblwidth = lblwidth ;

}

public JLabel getLblwidth () {

return lblwidth ;

}

public void setLblheight ( JLabel lblheight ) {

this.lblheight = lblheight ;

}

public JLabel getLblheight () {

return lblheight ;

}

public void setLblxdiam ( JLabel lblxdiam ) {

this.lblxdiam = lblxdiam ;

}

public JLabel getLblxdiam () {

return lblxdiam ;

}

public void setLblydiam ( JLabel lblydiam ) {

this.lblydiam = lblydiam ;

}

public JLabel getLblydiam () {

return lblydiam ;

}

public void setBtnproses ( JButton btnproses ) {

this.btnproses = btnproses ;

}

public JButton getBtnproses () {

return btnproses ;

}

public static void main (String[]kiuntoro) {

new design();

}

private void bersih() {

txtx1.setText(""); txtx2.setText("");

txtwidth.setText(""); txtheight.setText("");

txtxdiam.setText(""); txtydiam.setText("");

}

public void actionPerformed (ActionEvent evt) {

String xx1 = txtx1.getText(); int xxx1 = Integer.parseInt(xx1);

String yy2 = txtx2.getText(); int yyy2 = Integer.parseInt(yy2);

String width1 = txtwidth.getText(); int width11 =

Integer.parseInt(width1); String height1 = txtheight.getText(); int height11

= Integer.parseInt(height1);

// String xdiam1 = txtxdiam.getText(); int xdiam11 =

Integer.parseInt(xdiam1); String ydiam1 = txtydiam.getText(); int

ydiam11 = Integer.parseInt(ydiam1);

try{

if(evt.getSource()==btnproses) {

if (txtstatus.getText().equals("1")) {

rect re = new rect (xxx1,yyy2,width11,height11);

re.setTitle ( " Komputer Grafik > Rectangle " ) ;

re.setVisible(true);

} else if (txtstatus.getText().equals("2")) {

String xdiam1 = txtxdiam.getText(); int xdiam11 =

Integer.parseInt(xdiam1); String ydiam1 = txtydiam.getText(); int

ydiam11 = Integer.parseInt(ydiam1);

roundrect rr = new

roundrect(xxx1,yyy2,width11,height11,xdiam11,ydiam11);

rr.setTitle ( " Komputer Grafik > Round Rect " ) ;

rr.setVisible (true);

} else if (txtstatus.getText().equals("3")) {

oval ov = new oval (xxx1,yyy2,width11,height11);

ov.setTitle ( " Komputer Grafik > Oval " ) ;

ov.setVisible(true);

}else if (txtstatus.getText().equals("4")) {

String xdiam1 = txtxdiam.getText(); int xdiam11 =

Integer.parseInt(xdiam1); String ydiam1 = txtydiam.getText(); int

ydiam11 = Integer.parseInt(ydiam1);

arc ar = new

arc(xxx1,yyy2,width11,height11,xdiam11,ydiam11);

ar.setTitle ( " Komputer Grafik > Arc " ) ;

ar.setVisible (true);

}

}

}catch(Exception e) {System.err.println("error"+e.getMessage());}

}

}

Proses:

- Pada program Design, kita bisa mendesign tombol button, layout, ukuran

yang di perlukan.

- Terhubungan dengan program menu

Output:

4) Program Arc

Input:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class arc extends JFrame {

int x1 ; int y1 ; int width ; int height ; int xdiam ; int ydiam ;

public arc (int x1 , int y1 , int width ,int height , int xdiam , int

ydiam ) {

setSize (1370,730);

this.x1 = x1 ; this.y1 = y1 ; this.width = width ; this.height = height ;

this.xdiam = xdiam ; this.ydiam = ydiam ;

}

public void paint(Graphics g) {

g.drawArc ( x1,y1,width,height,xdiam,ydiam ) ;

}

}

Proses:

- Kita harus menginput angka pada x1, x2, width, height,start angle, dan arc

angle, agar kita mendapatkan hasil gambar yang kita inginkan.

- drawArc dengan x1,x2, width, height,start angle, dan arc angle yang sudah

kita input.

- Ketika sudah menginput kita menekan proses dan kemudian kita dapat

melihat hasilnya.

- Terhubung dengan program menu.

Output:

5) Program Oval

Input:

import java.awt.Graphics ;

import javax.swing.JFrame ;

import javax.swing.* ;

public class oval extends JFrame {

int x1 ; int y1 ; int width ; int height ;

public oval (int x1 , int y1 , int width ,int height) {

setSize (1370,730);

this.x1 = x1 ; this.y1 = y1 ; this.width = width ; this.height =

height ;

}

public void paint ( Graphics g ) {

g.drawOval ( x1,y1,width,height ) ;

}

}

Proses:

- Kita harus menginput angka pada x1, x2, width, dan height, agar kita

mendapatkan hasil gambar yang kita inginkan.

- drawOval dengan x1,x2, width, dan height yang sudah kita input.

- Ketika sudah menginput kita menekan proses dan kemudian kita dapat

melihat hasilnya.

- Terhubung dengan program menu.

Output:

6) Program RoundRect

Input:

import java.awt.Graphics ;

import javax.swing.JFrame ;

import javax.swing.* ;

public class roundrect extends JFrame {

int x1 ; int y1 ; int width ; int height ; int xdiam ; int ydiam ;

public roundrect (int x1 , int y1 , int width ,int height , int xdiam , int

ydiam ) {

setSize (1370,730);

this.x1 = x1 ; this.y1 = y1 ; this.width = width ; this.height = height ;

this.xdiam = xdiam ; this.ydiam = ydiam ;

}

public void paint ( Graphics g ) {

g.drawRoundRect ( x1,y1,width,height,xdiam,ydiam ) ;

}

}

Proses:

- Kita harus menginput angka pada x1, x2, width, height, xdiam, ydiam agar

kita mendapatkan hasil gambar yang kita inginkan.

- DrawRoundRect dengan x1,x2, width, height, xdiam, ydiam yang sudah

kita input.

- Ketika sudah menginput kita menekan proses dan kemudian kita dapat

melihat hasilnya.

- Terhubung dengan program menu.

Output:

7) Program Rect

Input:

import java.awt.Graphics ;

import javax.swing.JFrame ;

import javax.swing.* ;

public class rect extends JFrame {

int x1 ; int y1 ; int width ; int height ;

public rect (int x1 , int y1 , int width ,int height) {

setSize (1370,730);

this.x1 = x1 ; this.y1 = y1 ; this.width = width ; this.height =

height ;

}

public void paint ( Graphics g ) {

g.drawRect ( x1,y1,width,height ) ;

}

}

Proses:

- Kita harus menginput angka pada x1, x2, width, height, agar kita

mendapatkan hasil gambar yang kita inginkan.

- DrawRoundRect dengan x1,x2, width, height, yang sudah kita input.

- Ketika sudah menginput kita menekan proses dan kemudian kita dapat

melihat hasilnya.

- Terhubung dengan program menu.

Output:

8) Program Orang (Kreatif 2D)

Input:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class orang extends JApplet {

public static void main(String s[]) {

JFrame frame= new JFrame ();

frame.setTitle("Kiuntoro| 201243501128");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JApplet applet= new orang ();

applet.init();

frame.getContentPane().add(applet);

frame.pack();

frame.setVisible(true);

frame.setResizable(false);

}

public void init () {

JPanel panel4 = new Panel2D ();

getContentPane().add(panel4);

}

class Panel2D extends JPanel {

int awal = 0;

int mulai = 0;

public Panel2D (){

setPreferredSize(new Dimension(1010, 570));

//setBackground(Color.BLUE); //baground 2D

}

//public void paintComponent (Graphics g) {

//super.paintComponent (g);

//Graphics2D g2= (Graphics2D) g;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D gd = (Graphics2D) g;

Shape atas = new Rectangle2D.Double(0, 0, 1020, 580);

GradientPaint paint = new GradientPaint(400, 300, new Color(100,

250, 255), 500, 0, Color.BLUE);

gd.setPaint(paint);

gd.fill(atas);

//prosedur Membuat jalan

g.setColor(Color.black);

g.fillRect(0, 300, 1020, 100);

g.setColor(Color.blue);

//mobil

g.fillRect(205,305,100,30 ); //badan mobil bawah

g.fillOval(207,278,104,45); //badan mobil atas

g.setColor(Color.RED);

g.fillRect(210,305,100,30 ); //badan mobil bawah

g.fillOval(210,280,100,45); //badan mobilatas

g.fillArc (180,298,60,70,0,180);// atas ban mobil

g.fillArc (285,298,60,70,0,180);// atas ban mobil

g.setColor(Color.yellow);

g.fillArc (318,301,23,23,0,80);// atas ban mobil

g.setColor(Color.DARK_GRAY); //ban mobil

g.fillOval(198, 313, 32,32); //ban mobil

g.fillOval(298, 313, 32,32); //ban mobil

g.setColor(Color.BLACK); //ban mobil

g.fillOval(204, 319, 20,20); //ban mobil

g.fillOval(304, 319, 20,20); //ban mobil

g.setColor(Color.gray);

g.fillArc (230,285,70,40,0,90);// atas ban mobil

g.setColor(Color.black);

g.fillArc (234,286,64,36,0,90);// atas ban mobil

g.setColor(Color.red);

g.fillArc (220,285,70,40,90,90);// atas ban mobil

g.setColor(Color.black);

g.fillArc (223,286,68,36,90,90);// atas ban mobil

g.setColor(Color.yellow);

g.fillRect(258,286,10,18 );

//leher

g.setColor(Color.ORANGE);

g.fillRect(465,200,30,40 );

g.setColor(Color.BLACK);

g.drawRect(465,200,30,40 );

//kaki

g.setColor(Color.BLACK);

g.fillArc(500, 500, 20, 50, -180,360);

g.setColor(Color.BLACK);

g.fillArc(440, 500, 20, 50, -180,360);

//sarung

g.setColor(Color.RED);

g.fillRect(430,380,100,150 );

g.setColor(Color.BLACK);

g.drawRect(430,380,100,150 );

//baju

g.setColor(Color.MAGENTA);

g.fillRect(420,230,120,150 );

g.setColor(Color.BLACK);

g.drawRect(420,230,120,150 );

g.setColor(Color.ORANGE);

g.fillArc(465, 215, 30, 30, 180, 180);

g.setColor(Color.BLACK);

g.drawLine(480, 245, 480, 380);

g.setColor(Color.CYAN);

g.fillRect(495,270,30,30 );

g.setColor(Color.RED);

g.drawRect(495,270,30,30 );

//lengan kanan

g.setColor(Color.ORANGE);

g.fillArc(600, 233, 50, 20, -180,360);

g.setColor(Color.cyan);

g.fillRect(540,230,90,35 );

g.setColor(Color.BLACK);

g.drawRect(540,230,90,35 );

//lengan kiri

g.setColor(Color.ORANGE);

g.fillArc(310, 233, 50, 20, -180,360);

g.setColor(Color.cyan);

g.fillRect(330,230,90,35 );

g.setColor(Color.BLACK);

g.drawRect(330,230,90,35 );

//kepala

g.setColor(Color.ORANGE);

g.fillOval(405,70,150 ,150);

g.setColor(Color.black);

g.drawOval(405,70,150 ,150);

//kopyah

g.setColor(Color.BLACK);

g.fillRect(405,50,150 ,60 );

//mata kiri

g.setColor(Color.WHITE);

g.fillRect(425,125,45 ,20 );

g.setColor(Color.BLACK);

g.drawRect(425,125,45 ,20 );

g.setColor(Color.BLACK);

g.fillOval(455,127,15 ,15);

//mata kanan

g.setColor(Color.WHITE);

g.fillRect(490,125,45 ,20 );

g.setColor(Color.BLACK);

g.drawRect(490,125,45 ,20 );

g.setColor(Color.BLACK);

g.fillOval(520,127,15 ,15);

//mulut

g.setColor(Color.RED);

g.fillOval(445,180,70 ,20);

g.setColor(Color.BLACK);

g.drawOval(445,180,70 ,20);

g.setColor(Color.WHITE);

g.fillOval(455,180,50 ,5);

g.setColor(Color.BLACK);

g.drawOval(455,180,50 ,5);

//lisensi

gd.setColor(Color.black);

Font font = new Font("Times New Roman", Font.BOLD, 16);

gd.setFont(font);

gd.drawString("CREATED BY:", 55, 450);

Font font1 = new Font("Times New Roman", Font.BOLD, 36);

gd.setFont(font1);

gd.drawString("Kiuntoro", 30, 480);

}

public void init() {

JPanel panel = new AreaPanel2();

getContentPane().add(panel);

}

}

class AreaPanel2 extends JPanel {

public AreaPanel2() {

setPreferredSize(new Dimension(760, 400));

}

public void paintComponent(Graphics g) {

// tempat koding kaya biasa di taruh sini

}

}

}

Proses:

- Mempunyai Latar Background yang berwana-warni.

- Gambar tersebut merupakan kreatifitas dari rect,oval,polygon,arc,round-

rect,

- Draw rect dengan x1,y1, width,height yang telah di tentukan

- Draw oval dengan x1,y1.width, height yang telah ditentukan

- Draw arc dengan inputan yang telah di tentukan

- Terhubung dengan program menu.

Output:

9) Program Lukisan

Input:

import java.awt.Graphics ;

import java.awt.Color ;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class lukisan extends JApplet {

public static void main(String s[]) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JApplet applet = new lukisan();

applet.init();

frame.getContentPane().add(applet);

frame.pack();

frame.setVisible(true);

}

public void init() {

JPanel panel = new Pan();

getContentPane().add(panel);

}

class Pan extends JPanel {

public Pan() {

setPreferredSize(new Dimension(1300, 600));

}

public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g1 = (Graphics2D) g;

Graphics2D g2 = (Graphics2D) g;

// nama dan nim

kotak(g2);

GradientPaint paint1 = new GradientPaint(100, 100, new Color(130, 36,

2), 250, 100, new Color(253, 149, 51));

Font font = new Font("Comic Sans ms", Font.BOLD, 25);

g1.setPaint(paint1);

g1.setFont(font);

g1.drawString("Kiuntoro", 50, 500);

g1.drawString("( 201243501128)", 65, 550);

garisdda(g2, 0, 350, 325, 350);

garisdda(g2, 625, 350, 1300, 350);

garisdda(g2, 350, 400, 600, 400);

garisdda(g2, 600, 400, 650, 300);

garisdda(g2, 300, 300, 350, 400);

garisdda(g2, 300, 300, 650, 300);

kotak_brass(g2, 350, 250, 600, 300);

bunder(g2, 1150, 100, 75);

body(g2);

garisdda(g2, 950, 100, 1050, 100);

garisdda(g2, 960, 25, 1060, 50);

garisdda(g2, 960, 175, 1060, 150);

garisdda(g2, 980, 250, 1080, 175);

budermidpoint(g2, 375, 275, 20);

budermidpoint(g2, 430, 275, 20);

budermidpoint(g2, 485, 275, 20);

budermidpoint(g2, 540, 275, 20);

awane(g2, -10, 50);

awane(g2, 90, 1);

awane(g2, 210, 50);

awane(g2, 310, 1);

awane(g2, 410, 50);

awane(g2, 510, 1);

awane(g2, 610, 50);

awane(g2, 710, 1);

}

public void body(Graphics2D g) {

GeneralPath gp = new GeneralPath();

GradientPaint p = new GradientPaint(0, 350, Color.gray, 50, 30,

Color.black);

g.setPaint(p);

gp.moveTo(300, 300);

gp.lineTo(650, 300);

gp.lineTo(600, 400);

gp.lineTo(350, 400);

gp.closePath();

g.fill(gp);

}

public void kotak_brass(Graphics2D gr, int x_awal, int y_awal, int

x_akhir, int y_akhir) {

GeneralPath gp = new GeneralPath();

Shape s = new Rectangle2D.Double(x_awal, y_awal, 250, 50);

GradientPaint p = new GradientPaint(0, 350, Color.green, 50, 30,

Color.black);

gr.setPaint(p);

gr.fill(s);

int Dx = Math.abs(x_akhir - x_awal);

int Dy = Math.abs(y_akhir - y_awal);

int parameter = (2 * Dy) - Dx;

String status = "Kotak_BM";

this.repaint();

double g = x_awal, h = y_awal;

while (x_awal < x_akhir) {

if (parameter < 0) {

parameter += (2 * Dy);

} else {

y_awal++;

parameter = parameter + (2 * Dy) - (2 * Dx);

}

gp.moveTo(x_awal, y_awal);

x_awal++;

}

gp.lineTo(g, y_awal);

gp.lineTo(g, h);

gp.lineTo(x_awal, h);

gp.lineTo(x_awal, y_awal);

gp.closePath();

gr.draw(gp);}

public void garisdda(Graphics2D g, int x1, int y1, int x2, int y2) {

double m = (double) (y2 - y1) / (x2 - x1);

double y = (double) y1;

int iy;

for (int x = x1; x <= x2; x++) {

iy = (int) Math.round(y);

g.drawLine(x, iy, x, iy);

y += m;

}

}

}

public void kotak(Graphics2D g) {

Shape s = new Rectangle2D.Double(0, 0, 1300, 350);

GradientPaint p = new GradientPaint(0, 350, new Color(0, 255, 255), 50,

30, new Color(70, 51, 204));

g.setPaint(p);

g.fill(s);

Shape s1 = new Rectangle2D.Double(0, 350, 1300, 250);

GradientPaint r = new GradientPaint(0, 350, Color.white, 1300, 600,

Color.blue);

g.setPaint(r);

g.fill(s1);

}

void budermidpoint(Graphics2D g, int x1, int y1, int r) {

Shape s = new Ellipse2D.Double(x1 - r, y1 - r, r * 2, r * 2);

g.setColor(Color.red);

g.fill(s);

int x = 0, y = r, p = 1 - r;

int xctr = x1, yctr = y1;

g.drawLine(xctr + x, yctr + y, xctr + x, yctr + y);

g.drawLine(xctr - x, yctr + y, xctr - x, yctr + y);

g.drawLine(xctr + x, yctr - y, xctr + x, yctr - y);

g.drawLine(xctr - x, yctr - y, xctr - x, yctr - y);

g.drawLine(xctr + y, yctr + x, xctr + y, yctr + x);

g.drawLine(xctr - y, yctr + x, xctr - y, yctr + x);

g.drawLine(xctr + y, yctr - x, xctr + y, yctr - x);

g.drawLine(xctr - y, yctr - x, xctr - y, yctr - x);

while (x < y) {

x++;

if (p < 0) {

p += 2 * x + 1;

} else {

y--;

p += 2 * (x - y) + 1;

}

}

}

public void bunder(Graphics2D g, int x1, int y1, int r) {

Shape s = new Ellipse2D.Double(x1 - r, y1 - r, r * 2, r * 2);

GradientPaint p = new GradientPaint(x1, y1, Color.orange, x1 + 200, y1 +

50, Color.red);

g.setPaint(p);

g.fill(s);

}

public void awane(Graphics2D g2, int x, int y) {

GeneralPath gp = new GeneralPath();

gp.moveTo(30 + x, 80 + y);

gp.curveTo(-5 + x, 60 + y, 22 + x, 15 + y, 40 + x, 42 + y);

gp.curveTo(37 + x, 31 + y, 53 + x, 31 + y, 58 + x, 43 + y);

gp.curveTo(60 + x, 7 + y, 116 + x, 4 + y, 100 + x, 34 + y);

gp.curveTo(150 + x, 29 + y, 150 + x, 89 + y, 120 + x, 90 + y);

gp.curveTo(100 + x, 120 + y, 95 + x, 97 + y, 87 + x, 100 + y);

gp.curveTo(71 + x, 120 + y, 35 + x, 100 + y, 35 + x, 70 + y);

gp.closePath();

gp.closePath();

g2.setPaint(new GradientPaint(0 + x, 20 + y, Color.lightGray, 0 + x, 150 +

y, Color.white));

g2.fill(gp);

g2.setColor(Color.white);

g2.draw(gp);

}

}

Proses:

- Mempunyai Latar Background yang berwana-warni.

- Gambar tersebut merupakan kreatifitas dari rect,oval,polygon,arc,round-

rect,

- Draw rect dengan x1,y1, width,height yang telah di tentukan

- Draw oval dengan x1,y1.width, height yang telah ditentukan

- Draw arc dengan inputan yang telah di tentukan

- Terhubung dengan program menu.

Output:

10) Program Catur

Input:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class catur extends JApplet {

public void paint(Graphics g) {

super.paint(g);

Graphics2D g2d = (Graphics2D) g;

g2d.setPaint(new Color(204,153,0));

g2d.fillRect(18, 38, 264, 264);

g2d.setPaint(Color.BLACK);

g2d.fillRect(30, 50, 240, 240);

g2d.setPaint(Color.WHITE);

g2d.fillRect(30,80,30,30);

g2d.fillRect(30,140,30,30);

g2d.fillRect(30,200,30,30);

g2d.fillRect(30,260,30,30);

g2d.fillRect(90,80,30,30);

g2d.fillRect(90,140,30,30);

g2d.fillRect(90,200,30,30);

g2d.fillRect(90,260,30,30);

g2d.fillRect(150,80,30,30);

g2d.fillRect(150,140,30,30);

g2d.fillRect(150,200,30,30);

g2d.fillRect(150,260,30,30);

g2d.fillRect(210,80,30,30);

g2d.fillRect(210,140,30,30);

g2d.fillRect(210,200,30,30);

g2d.fillRect(210,260,30,30);

g2d.setPaint(Color.WHITE);

g2d.fillRect(60,50,30,30);

g2d.fillRect(60,110,30,30);

g2d.fillRect(60,170,30,30);

g2d.fillRect(60,230,30,30);

g2d.fillRect(120,50,30,30);

g2d.fillRect(120,110,30,30);

g2d.fillRect(120,170,30,30);

g2d.fillRect(120,230,30,30);

g2d.fillRect(180,50,30,30);

g2d.fillRect(180,110,30,30);

g2d.fillRect(180,170,30,30);

g2d.fillRect(180,230,30,30);

g2d.fillRect(240,50,30,30);

g2d.fillRect(240,110,30,30);

g2d.fillRect(240,170,30,30);

g2d.fillRect(240,230,30,30);

}

public void init() {

JPanel panel = new AreaPanel2();

getContentPane().add(panel);

}

}

class AreaPanel2 extends JPanel {

public AreaPanel2() {

setPreferredSize(new Dimension(760, 400));

}

public void paintComponent(Graphics g) {

// tempat koding kaya biasa di taruh sini

}

}

Proses:

- Mempunyai Latar Background yang berwana-warni.

- Gambar tersebut merupakan kreatifitas dari rect,oval,polygon,arc,round-

rect,

- Draw rect dengan x1,y1, width,height yang telah di tentukan

- Draw oval dengan x1,y1.width, height yang telah ditentukan

- Draw arc dengan inputan yang telah di tentukan

- Terhubung dengan program menu.

Output:

11) Program Empat Jam

Input:

import java.applet.*;

import java.awt.*;

import java.util.*;

import java.text.*;

import javax.swing.*;

public class empatJam extends Applet implements Runnable

{

int width, height;

Thread t = null;

boolean threadSuspended;

int jam=0, menit=0, detik=0;

String strWaktu = "";

public void init()

{

width = getSize().width;

height = getSize().height;

setBackground(Color.black);

}

public void start()

{

if(t == null)

{

t = new Thread(this);

t.setPriority(Thread.MIN_PRIORITY);

threadSuspended = false;

t.start();

}

else

{

if(threadSuspended)

{

threadSuspended=false;

synchronized(this)

{

notify();

}

}

}

}

public void stop()

{

threadSuspended = true;

}

public void run()

{

try

{

while(true)

{

Calendar cal = Calendar.getInstance();

jam = cal.get(Calendar.HOUR_OF_DAY);

if(jam > 12) jam -= 12;

menit = cal.get(Calendar.MINUTE);

detik = cal.get(Calendar.SECOND);

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss",

Locale.getDefault());

Date tgl = cal.getTime();

strWaktu = sdf.format(tgl);

if(threadSuspended)

{

synchronized(this)

{

while(threadSuspended)

{

wait();

}

}

}

repaint();

//Color[] c = {Color.red, Color.green, Color.yellow,

Color.black};

//int[] x = {6, 12, 15};

//setBackground(c[detik%4]);

t.sleep(1000);

}

}

catch(InterruptedException e)

{

}

}

void drawHand(int posX, int posY, double angle, int radius, Graphics g)

{

angle -= 0.5 * Math.PI;

int x = (int) (radius*Math.cos(angle));

int y = (int) (radius*Math.sin(angle));

g.drawLine(posX, posY, posX+x, posY+ y);

}

void drawWedge(int posX, int posY, double angle, int radius, Graphics

g)

{

angle -= 0.5 * Math.PI;

int x = (int) (radius*Math.cos(angle));

int y = (int) (radius*Math.sin(angle));

angle += 2*Math.PI/3;

int x2 = (int) (5*Math.cos(angle));

int y2 = (int) (5*Math.sin(angle));

angle += 2*Math.PI/3;

int x3 = (int) (5*Math.cos(angle));

int y3 = (int) (5*Math.sin(angle));

g.drawLine(posX+ x2, posY+ y2, posX+ x, posY + y);

g.drawLine(posX+ x3, posY + y3, posX+ x, posY + y);

g.drawLine(posX+ x2,posY + y2, posX+ x3, posY + y3);

}

void drawRoundedNum(int posX, int posY, int radius, Graphics g)

{for(int i=0;i<12;i++)

{

int x = (int) (0.8*radius*Math.cos(i*Math.PI/6-0.5*Math.PI));

int y = (int) (0.8*radius*Math.sin(i*Math.PI/6-0.5*Math.PI));

String j = Integer.toString(i);

if(i==0) j="12";

for(int k=0;k<width;k+=width/4)

if(k%2==0) g.drawString(j, x+posX, y+posY);

}

}

public void paint(Graphics g)

{

g.setColor(Color.white);

int z=0;

for(int i=0;i<width;i+=width/4)

if(i%2==1)

{

drawWedge(i,height/4,2*Math.PI * (jam-z) / 12, width/12, g);

drawWedge(i,height/4,2*Math.PI * (menit) / 60, width/8, g);

drawWedge(i,height-height/4,2*Math.PI * (jam-z-5) / 12,

width/12, g);

drawWedge(i,height-height/4,2*Math.PI * (menit) / 60, width/8,

g);

drawHand(i,height/4,2*Math.PI * (detik) / 60, width/8, g);

drawHand(i,height-height/4,2*Math.PI * (detik) / 60, width/8,

g);

drawRoundedNum(i,height/4, width/4, g);

drawRoundedNum(i,height-height/4, width/4, g);

z+=7;

}

g.setColor(Color.white);

//g.drawString(strWaktu, 10, height-10);

}

}

Proses:

- Mempunyai Latar Background hitam dan gambar jamnya berwarna putih.

- Gambar tersebut merupakan kreatifitas dari rect,oval,polygon,arc,round-

rect yang bernomor dan bisa berjalan seperti animasi

- Draw rect dengan x1,y1, width,height yang telah di tentukan

- Draw oval dengan x1,y1.width, height yang telah ditentukan

- Draw arc dengan inputan yang telah di tentukan

- Terhubung dengan time otomatis dengan time yang ada pada komputer

kita. Maksudnya yaitu mengikuti dengan jam yang ada pada di komputer

kita.

Output:

12) Program Grafik Gempa

Input:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class TampilanGrafikGempa extends Canvas

{

private int jarakTitik;

private int titikTengah;

private int jumlahTitik;

private int[] titik;

public TampilanGrafikGempa (int jumlahTitik)

{

this.jumlahTitik = jumlahTitik;

titik = new int[jumlahTitik];

for (int i=0; i<jumlahTitik; i++)

titik[i] = (int)(Math.random()*41-20);

setBackground(Color.white);

}

@Override

public void paint (Graphics g)

{

jarakTitik = getWidth()/jumlahTitik + 1;

titikTengah = getHeight()/2;

g.setColor(Color.red);

g.drawLine(0, titikTengah, getWidth(), titikTengah);

g.setColor(Color.blue);

for (int i=1; i<jumlahTitik; i++)

{

g.drawLine((i-1)*jarakTitik, titikTengah+titik[i-1], i*jarakTitik,

titikTengah+titik[i]);

titik[i-1] = titik[i];

}

titik[jumlahTitik-1] = (int) (Math.random()*250-150);

}

}

class ProsesGrafik extends Thread

{

private TampilanGrafikGempa gambar;

public ProsesGrafik (TampilanGrafikGempa gambar)

{

this.gambar = gambar;

}

@Override

public void run()

{

while (true)

{

gambar.repaint();

try

{

Thread.sleep(100);

}

catch (Exception e)

{

}

}

}

}

public class GrafikGempa extends JFrame implements ActionListener

{

private TampilanGrafikGempa gambar = new

TampilanGrafikGempa(65);

private ProsesGrafik proses = new ProsesGrafik(gambar);

private Button selesai = new Button("Selesai");

public GrafikGempa()

{

super ("Simulasi Grafik Gempa");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize (500,500);

setLocation(100, 100);

selesai.addActionListener(this);

getContentPane().setLayout(new BorderLayout());

getContentPane().add(gambar, BorderLayout.CENTER);

getContentPane().add(selesai, BorderLayout.SOUTH);

show();

proses.run();

setVisible(true);

}

public static void main (String[] args)

{

new GrafikGempa();

}

public void actionPerformed(ActionEvent e) {

try{

if (e.getSource()== selesai ) {

proses.run();

}

} catch(Exception ex ) {

System.err.println("error"+ex.getMessage());

}

}

}

Proses:

- Mempunyai Latar Background putih, garis pusat berwarna merah, dan

garis turun naiknya berwarna biru.

- Gambar tersebut merupakan kreatifitas dari line, rect,oval,poly-

gon,arc,roundrect yang bernomor dan bisa berjalan seperti animasi

- Draw rect dengan x1,y1, width,height yang telah di tentukan

- Draw oval dengan x1,y1.width, height yang telah ditentukan

- Draw arc dengan inputan yang telah di tentukan

- Draw Line dengan inputan yang telah di tentukan

- Bergerak dengan otomatis secara turun naik seperti cara kerja alat

pendeteksi gempa

Output:

13) Program Image Zoomer

Input:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ImageZoomer extends JApplet implements ActionListener

{

//static variables

public static final double DEFAULT_ZOOM_PERCENTAGE = 10.00;

public static final String ZOOM_CURSOR = "zoom_cursor.gif";

//public variables

private String images[] = {"123.jpg",

"buriek.jpg",

"a7x1.jpg",

"bg.jpg",

"skull.jpg"};

private JButton m_launch;

private JList m_imageList;

private JTextField m_zoomPercentage;

public void init()

{

getContentPane().setLayout(new BorderLayout());

m_imageList = new JList(images);

m_imageList.setSelectedIndex(0);

m_launch = new JButton("Launch Image Zoomer");

m_launch.addActionListener(this);

Panel input = new Panel(new FlowLayout());

input.add(m_launch);

Panel input1 = new Panel(new FlowLayout());

input1.add(new Label("Enter zoom percentage: "));

m_zoomPercentage = new JTextField(5);

m_zoomPercentage.setText("10");

input1.add(m_zoomPercentage);

getContentPane().add(BorderLayout.NORTH, input1);

getContentPane().add(BorderLayout.CENTER, m_imageList);

getContentPane().add(BorderLayout.SOUTH, input);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource().equals(m_launch))

{

//getting the image

String imageName = ((String)

(m_imageList.getSelectedValue())).trim();

Image image = getImage(getCodeBase(), imageName);

Image zoomcursorImage = getImage(getCodeBase(),

ZOOM_CURSOR);

MediaTracker mt = new MediaTracker(this);

mt.addImage(image, 1);

mt.addImage(zoomcursorImage, 2);

try

{

mt.waitForAll();

}

catch (InterruptedException e)

{

e.printStackTrace();

}

double per = DEFAULT_ZOOM_PERCENTAGE;

try

{

per =

Double.valueOf(m_zoomPercentage.getText().trim()).intValue();

}

catch(NumberFormatException numExp)

{

}

//create the Image Zoomer frame

ImageZoomerFrame imageZoomer = new

ImageZoomerFrame(image, per, imageName);

//set the zoom cursor image

imageZoomer.setZoomCursorImage(zoomcursorImage);

}

}

}

Proses:

- Kita Dapat zoom out dan zoom in gambar yang telah dimasukan ke folder

yang sama dengan folder. Gambar tersebut adalah "123.jpg", "buriek.jpg",

"a7x1.jpg”, "bg.jpg", "skull.jpg".

- Menghubungkan dengan program image zoomer frame.

Output:

14) Program Image Zoomer Frame

Input:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ImageZoomerFrame extends JFrame

implements MouseListener, ActionListener

{

private ImagePanel m_imagePanel;

private JScrollPane m_srollPane;

private JPanel m_imageContainer;

private JLabel m_zoomedInfo;

private JButton m_zoomInButton;

private JButton m_zoomOutButton;

private JButton m_originalButton;

private Cursor m_zoomCursor;

public ImageZoomerFrame(Image image, double zoomPercentage,

String imageName)

{

super("Image Zoomer [" + imageName + "]");

if(image == null)

{

add(new JLabel("Image " + imageName + " not Found"));

}

else

{

JPanel topPanel = new JPanel(new

FlowLayout(FlowLayout.CENTER));

m_zoomInButton = new JButton("Zoom In");

m_zoomInButton.addActionListener(this);

m_zoomOutButton = new JButton("Zoom Out");

m_zoomOutButton.addActionListener(this);

m_originalButton = new JButton("Original");

m_originalButton.addActionListener(this);

m_zoomedInfo = new JLabel("Zoomed to 100%");

topPanel.add(new JLabel("Zoom Percentage is " +

(int)zoomPercentage + "%"));

topPanel.add(m_zoomInButton);

topPanel.add(m_originalButton);

topPanel.add(m_zoomOutButton);

topPanel.add(m_zoomedInfo);

m_imagePanel = new ImagePanel(image, zoomPercentage);

m_imagePanel.addMouseListener(this);

m_imageContainer = new JPanel(new

FlowLayout(FlowLayout.CENTER));

m_imageContainer.setBackground(Color.BLACK);

m_imageContainer.add(m_imagePanel);

m_srollPane = new JScrollPane(m_imageContainer);

m_srollPane.setAutoscrolls(true);

getContentPane().add(BorderLayout.NORTH, topPanel);

getContentPane().add(BorderLayout.CENTER, m_srollPane);

getContentPane().add(BorderLayout.SOUTH,

new JLabel("Left Click to Zoom In," +

" Right Click to Zoom Out", JLabel.CENTER));

m_imagePanel.repaint();

}

pack();

setVisible(true);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource().equals(m_zoomInButton))

{

m_imagePanel.zoomIn();

adjustLayout();

}

else if(ae.getSource().equals(m_zoomOutButton))

{

m_imagePanel.zoomOut();

adjustLayout();

}

else if(ae.getSource().equals(m_originalButton))

{

m_imagePanel.originalSize();

adjustLayout();

}

}

public void setZoomCursorImage(Image zoomcursorImage)

{

m_zoomCursor = Toolkit.getDefaultToolkit().createCustomCursor(

zoomcursorImage, new Point(0, 0), "ZoomCursor");

}

private void adjustLayout()

{

m_imageContainer.doLayout();

m_srollPane.doLayout();

m_zoomedInfo.setText("Zoomed to " +

(int)m_imagePanel.getZoomedTo() + "%");

}

public void mouseClicked(MouseEvent e)

{

if(e.getButton() == MouseEvent.BUTTON1)

{

m_imagePanel.zoomIn();

}

else if(e.getButton() == MouseEvent.BUTTON3)

{

m_imagePanel.zoomOut();

}

adjustLayout();

}

public void mouseEntered(MouseEvent e)

{

m_imageContainer.setCursor(m_zoomCursor);

}

public void mouseExited(MouseEvent e)

{

m_imageContainer.setCursor(new

Cursor(Cursor.DEFAULT_CURSOR));

}

public void mousePressed(MouseEvent e)

{

}

public void mouseReleased(MouseEvent e)

{

}

public class ImagePanel extends JPanel

{

private double m_zoom = 1.0;

private double m_zoomPercentage;

private Image m_image;

public ImagePanel(Image image, double zoomPercentage)

{

m_image = image;

m_zoomPercentage = zoomPercentage / 100;

}

public void paintComponent(Graphics grp)

{

Graphics2D g2D = (Graphics2D)grp;

//set the background color to white

g2D.setColor(Color.WHITE);

//fill the rect

g2D.fillRect(0, 0, getWidth(), getHeight());

//scale the graphics to get the zoom effect

g2D.scale(m_zoom, m_zoom);

//draw the image

g2D.drawImage(m_image, 0, 0, this);

}

public Dimension getPreferredSize()

{

return new Dimension((int)(m_image.getWidth(this) +

(m_image.getWidth(this) * (m_zoom - 1))),

(int)(m_image.getHeight(this) +

(m_image.getHeight(this) * (m_zoom -1 ))));

}

public void setZoomPercentage(int zoomPercentage)

{

m_zoomPercentage = ((double)zoomPercentage) / 100;

}

public void originalSize()

{

m_zoom = 1;

}

public void zoomIn()

{

m_zoom += m_zoomPercentage;

}

//Proses pengecilan image

public void zoomOut()

{

m_zoom -= m_zoomPercentage;

if(m_zoom < m_zoomPercentage)

{

if(m_zoomPercentage > 1.0)

{

m_zoom = 1.0;

}

else

{

zoomIn();

}

}

}

public double getZoomedTo()

{

return m_zoom * 100;

}

}

}

Proses:

- Mendesign zoom out dan zoom in yaitu button, label, cursor dan sizenya

- Terhubung dengan program image zoomer frame.

Output: