Temen - teman di sini saya akan memberikan cara untuk
membuat gambar orang dengan menggunakan java.
teman-teman bisa lihat source code di bawah ini
package Hergan_orang;
import java.awt.*;
import java.awt.event.*;
public class Tugas_hergan extends Frame implements ActionListener{
int x = 100;
int y = 100;
public static void main(String[] args) {
Frame frame = new Tugas_hergan();
frame.setSize(640, 480);
frame.setVisible(true);
}
public Tugas_hergan() {
setTitle("Tugas Hergan");
// create menu
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu menu = new Menu("File");
mb.add(menu);
MenuItem mi = new MenuItem("Exit");
mi.addActionListener(this);
menu.add(mi);
// end program when window is closed
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
};
this.addWindowListener(l);
// mouse event handler
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
x = ev.getX();
y = ev.getY();
repaint();
}
};
addMouseListener(mouseListener);
}
public void paint(Graphics g) {
setBackground (Color.green);
//matahari
g.setColor(Color.yellow);
g.fillOval(430, 50, 80, 80);
//cahaya matahari
g.setColor(new Color(245,135,14)); //cahaya matahari
g.drawLine(430, 90, 380, 90); //garis kiri
g.drawLine(440, 60, 380, 30); //kiri atas
g.drawLine(440, 120, 380, 150); //kiri bawah
g.drawLine(560, 90, 510, 90); //garis kanan
g.drawLine(560, 30, 500, 60); //kanan atas
g.drawLine(560, 150, 500, 120); //kanan bawah
g.drawLine(470, 10, 470, 50); //utara
g.drawLine(470, 130, 470, 170); //selatan
//kepala
g.setColor(Color.BLUE);
g.drawOval(320,240, 200, 200);
// mata
g.setColor(Color.black);
g.fillOval(360, 310, 50, 20);
g.fillOval(440, 310, 50, 20);
// mulut
g.setColor(Color.black);
g.drawArc(380, 360, 90, 50, 180, 180);
g.drawArc(380, 366, 90, 50, 180, 180);
//alis mata
g.setColor(Color.yellow);
g.drawArc(360, 290, 50, 20, -180, -180);
g.drawArc(440, 290, 50, 20, -180, -180);
// bola mata
g.setColor(Color.white);
g.fillOval(379, 321, 10, 10);
g.fillOval(457, 321, 10, 10);
//hidung
g.setColor(Color.MAGENTA);
g.fillOval(410, 350, 30, 30);
//telinga
g.setColor(Color.GRAY);
g.fillOval(300, 310, 20, 60);
g.fillOval(520, 310, 20, 60);
//badan
g.setColor(Color.blue);
g.drawOval(320,440, 200, 300);
//tangan kanan
g.setColor(Color.BLUE);
g.drawOval(499,479, 110, 60);
//tangan kiri
g.drawOval(230,479, 110, 60);
//hiasan di badan
g.setColor(Color.BLUE);
g.drawOval(339,590, 160, 80);
}
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if ("Exit".equals(command)) {
System.exit(0);
}
}
}
Hasilnya
teman-teman bisa lihat source code di bawah ini
package Hergan_orang;
import java.awt.*;
import java.awt.event.*;
public class Tugas_hergan extends Frame implements ActionListener{
int x = 100;
int y = 100;
public static void main(String[] args) {
Frame frame = new Tugas_hergan();
frame.setSize(640, 480);
frame.setVisible(true);
}
public Tugas_hergan() {
setTitle("Tugas Hergan");
// create menu
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu menu = new Menu("File");
mb.add(menu);
MenuItem mi = new MenuItem("Exit");
mi.addActionListener(this);
menu.add(mi);
// end program when window is closed
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
};
this.addWindowListener(l);
// mouse event handler
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
x = ev.getX();
y = ev.getY();
repaint();
}
};
addMouseListener(mouseListener);
}
public void paint(Graphics g) {
setBackground (Color.green);
//matahari
g.setColor(Color.yellow);
g.fillOval(430, 50, 80, 80);
//cahaya matahari
g.setColor(new Color(245,135,14)); //cahaya matahari
g.drawLine(430, 90, 380, 90); //garis kiri
g.drawLine(440, 60, 380, 30); //kiri atas
g.drawLine(440, 120, 380, 150); //kiri bawah
g.drawLine(560, 90, 510, 90); //garis kanan
g.drawLine(560, 30, 500, 60); //kanan atas
g.drawLine(560, 150, 500, 120); //kanan bawah
g.drawLine(470, 10, 470, 50); //utara
g.drawLine(470, 130, 470, 170); //selatan
//kepala
g.setColor(Color.BLUE);
g.drawOval(320,240, 200, 200);
// mata
g.setColor(Color.black);
g.fillOval(360, 310, 50, 20);
g.fillOval(440, 310, 50, 20);
// mulut
g.setColor(Color.black);
g.drawArc(380, 360, 90, 50, 180, 180);
g.drawArc(380, 366, 90, 50, 180, 180);
//alis mata
g.setColor(Color.yellow);
g.drawArc(360, 290, 50, 20, -180, -180);
g.drawArc(440, 290, 50, 20, -180, -180);
// bola mata
g.setColor(Color.white);
g.fillOval(379, 321, 10, 10);
g.fillOval(457, 321, 10, 10);
//hidung
g.setColor(Color.MAGENTA);
g.fillOval(410, 350, 30, 30);
//telinga
g.setColor(Color.GRAY);
g.fillOval(300, 310, 20, 60);
g.fillOval(520, 310, 20, 60);
//badan
g.setColor(Color.blue);
g.drawOval(320,440, 200, 300);
//tangan kanan
g.setColor(Color.BLUE);
g.drawOval(499,479, 110, 60);
//tangan kiri
g.drawOval(230,479, 110, 60);
//hiasan di badan
g.setColor(Color.BLUE);
g.drawOval(339,590, 160, 80);
}
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if ("Exit".equals(command)) {
System.exit(0);
}
}
}
Hasilnya
1 komentar:
good job..............
Posting Komentar