Kamis, 05 November 2009

Gui Robot Pemrograman IV

package  gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import latih.*;
import java.util.Random;

public class guiRobot extends JPanel implements KeyListener, ActionListener{
Icon iconStart = new ImageIcon ("side_arrow.gif");
Icon iconStop = new ImageIcon ("stop.gif");
Icon iconRight = new ImageIcon ("next.gif");
Icon iconUp = new ImageIcon ("up.gif");
Icon iconDown = new ImageIcon ("down.gif");
Icon iconPrev = new ImageIcon ("prev.gif");
Icon iconAcak = new ImageIcon ("directional.gif");
Icon gambar = new ImageIcon("Cross.gif");
Timer timer;
String digit;
JTextArea txPosisi = new JTextArea();
JLabel lbWaktu = new JLabel("00:00");
JButton btLeft = new JButton(iconPrev);
JButton btRight = new JButton(iconRight);
JButton btUp = new JButton(iconUp);
JButton btDown = new JButton(iconDown);
JButton btAcak = new JButton(iconAcak);
JButton btStart = new JButton(iconStart);
JButton btStop = new JButton(iconStop);



JLabel btX = new JLabel(gambar);
int posX=250,posY=200,counter=0,control,spasi=5;

Random rand = new Random();
Random random = new Random();

Robots mecha = new Robots(posX,posY);
 
    public guiRobot() {
    setLayout(null);
    this.setBackground(Color.white);
timer=new Timer(1000,this); //aktif setiap 1 detik

txPosisi.setFont(new Font("Tahoma",1,10));
txPosisi.setForeground(Color.blue); //new Color(0,0,255)
txPosisi.setBounds(0,0,140,20); //posisi label

lbWaktu.setFont(new Font("Tahoma",1,10));
lbWaktu.setHorizontalAlignment(JLabel.CENTER);
lbWaktu.setForeground(Color.blue); //new Color(0,0,255)
lbWaktu.setBounds(220,0,50,20); //posisi label


btLeft.setBounds(0,340,35,30); //posisi label
btLeft.setBackground(Color.white);

btRight.setForeground(Color.black); //new Color(0,0,255)
btRight.setBounds(35,340,35,30); //posisi label


btUp.setBackground(Color.WHITE);
btUp.setBounds(70,340,35,30); //posisi label

btDown.setBackground(Color.WHITE);
btDown.setBounds(105,340,35,30); //posisi label

btAcak.setBackground(Color.WHITE);
btAcak.setBounds(140,340,35,30); //posisi label


btStop.setBounds(425,340,35,30); //posisi label
btStart.setBounds(460,340,35,30); //posisi label


btX.setBounds(250,200,15,15); //posisi label

add(btX);
    add(txPosisi);add(lbWaktu);
    add(btLeft);btLeft.addActionListener(this);
    add(btRight);btRight.addActionListener(this);
    add(btUp);btUp.addActionListener(this);
    add(btDown);btDown.addActionListener(this);
    add(btAcak);btAcak.addActionListener(this);
    add(btStop);btStop.addActionListener(this);
    add(btStart);btStart.addActionListener(this);
   
    this.addKeyListener(this);  // This class has its own key listeners.
        this.setFocusable(true);    // Allow panel to get focus
   
    Robots mecha = new Robots(posX,posY);
Counter waktu = new Counter();
    }

public void actionPerformed(ActionEvent evt){

Robots mecha = new Robots(posX,posY);
Counter waktu = new Counter();

if(evt.getSource()==btStart){
  timer.start();
}

if (evt.getSource()==timer){
control=random.nextInt(3);
  switch(control){
 
  case 0 : mecha.moveLeft();break;
  case 1 : mecha.moveRight();break;
  case 2 : mecha.moveUp();break;
  case 3 : mecha.moveDown();break;
  }
 
  posX=mecha.getPosX();
  posY=mecha.getPosY();
 
btX.setBounds(this.posX,this.posY,15,15);
txPosisi.setText("Posisi (x,y) = ( "+posX+" , "+posY+" ) ");

counter++;
lbWaktu.setText(waktu.setFormatCount(counter));
}

if(evt.getSource()==btStop){
  timer.stop();

}

if( evt.getSource() == btLeft ){
mecha.moveLeft();
posX=mecha.getPosX();
btX.setBounds(this.posX,this.posY,15,15);
txPosisi.setText("Posisi (x,y) = ( "+posX+" , "+posY+" ) " );

}

if( evt.getSource() == btRight ){
mecha.moveRight();
this.posX=mecha.getPosX();
btX.setBounds(this.posX,this.posY,15,15);
txPosisi.setText("Posisi (x,y) = ( "+posX+" , "+posY+" ) " );
}

if( evt.getSource() == btUp ){
mecha.moveUp();
this.posY=mecha.getPosY();
btX.setBounds(posX,posY,15,15);
txPosisi.setText("Posisi (x,y) = ( "+posX+" , "+posY+" ) " );
}

if( evt.getSource() == btDown ){
mecha.moveDown();
this.posY=mecha.getPosY();
btX.setBounds(this.posX,this.posY,15,15);
txPosisi.setText("Posisi (x,y) = ( "+posX+" , "+posY+" ) " );
}

if( evt.getSource() == btAcak ){
this.posY=rand.nextInt(330);
this.posX=rand.nextInt(400);
btX.setBounds(this.posX,this.posY,15,15);
txPosisi.setText("Posisi (x,y) = ( "+posX+" , "+posY+" ) " );

}

}

public void keyTyped(KeyEvent e){}
   
    public void keyPressed(KeyEvent e){
        // Check the shift key, and do 10x the movement if the
        // shift key is down when the arrow keys are pressed.
        // Altho keyPressed is called for normal characters, they
        // are ignored here and handled in keyTyped.
     
        if (e.isShiftDown()) {
        spasi = 10;
        } else {
          spasi = 5;
        }      
     
     
        //-- Process arrow "virtual" keys
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT : mecha.moveLeft();   break;
            case KeyEvent.VK_RIGHT: mecha.moveRight(); break;
            case KeyEvent.VK_UP   : mecha.moveUp(); break;
            case KeyEvent.VK_DOWN : mecha.moveDown(); break;
        }
        this.posY=mecha.getPosY();
        this.posX=mecha.getPosX();
         
        txPosisi.setText("Posisi (x,y) = ( "+this.posX+" , "+this.posY+" ) " );
        btX.setBounds(this.posX*spasi,this.posY*spasi,15,15);
    }

    public void keyReleased(KeyEvent e){}


    public static void main(String[] args){
JFrame frame=new JFrame("Posisi Robot");
guiRobot rob = new guiRobot();
frame.add(rob);
frame.setSize(500,400);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null); //posisi di tengah
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

0 komentar:

Posting Komentar

Related Posts Plugin for WordPress, Blogger...