Selasa, 17 Desember 2013

Tugas ke 2 Grafika komputer

Contoh program netbeans, untuk pembuatan sepeda sederhana. 

Langsung saja untuk lisning programnya dibawah ini :






package sepedesederhana;

import java.awt.*;
import java.awt.event.*;
public class SepedeSederhana extends Frame implements ActionListener{
    int x = 100;
    int y = 100;
public static void main(String[] args) {
    Frame frame = new SepedeSederhana();
    frame.setSize(640, 480);
    frame.setVisible(true);
}
public SepedeSederhana() {
setTitle("AWT Demo");
// create menu
    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    Menu menu = new Menu("File");
    mb.add(menu);
    MenuItem mi = new MenuItem("Exit");
  // end program when window is closed
    WindowListener l = new WindowAdapter()  {
    @Override
    public void windowClosing(WindowEvent ev) {
    System.exit(0);
    }
    };
this.addWindowListener(l);
// mouse event handler
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ev) {
    x = ev.getX();
    y = ev.getY();
    repaint();
}
};
addMouseListener(mouseListener);
}
    @Override
    public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval(100, 330, 20, 20);
g.fillOval(510, 330, 20, 20);
g.setColor(Color.black);
g.drawOval(10,240,  200, 200);
g.setColor(Color.black);
g.drawOval(420,240,  200, 200);
g.setColor(Color.black);
g.fillOval(330, 330, 30,30);
g.fillOval(330, 330, 200,10);
 g.setColor(Color.BLACK);
 g.fillRect(150, 150,8, 200);
    
     
g.drawLine(40, 130, 500, 200);
g.drawArc(380, 360, 90, 50, 180, 180);
g.drawRect(110, 240, 2, 200); //g.drawRect(x, y, WIDTH, WIDTH)
g.drawRect(115, 240, 0, 200); //g.drawRect(x, y, WIDTH, WIDTH)
g.drawRect(150, 250, 0, 10); //g.drawRect(x, y, WIDTH, WIDTH)
g.drawRect(150, 150, 0, 100); //g.drawRect(x, y, WIDTH, WIDTH)
}
    @Override
    public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if ("Exit".equals(command)) {
System.exit(0);
}
}
    private void addActionListener(SepedeSederhana aThis) {
        throw new UnsupportedOperationException("Not supported yet.");
        //To change body of generated methods, choose Tools | Templates.
    }
 

Hasil outputnya dibawah ini :

 


Terimahkasih sudah berkunjung.....

Tidak ada komentar:

Poskan Komentar