import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;

public class Praktikum2 extends JFrame implements KeyListener
{
	private int ordinatX, ordinatY;
	private int lebar = 40, tinggi = 20;
	private int geserX, geserY;
	public Praktikum2()
	{
		super ("Menggerakkan Geometri dengan Keyboard");
		addKeyListener(this);
	}

	public static void main(String []args)
	{
		Praktikum2 p2 = new Praktikum2();
		p2.setSize(800, 600);
		p2.setVisible(true);

		p2.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public void keyTyped(KeyEvent e) {}
	public void keyReleased(KeyEvent e) {}
	public void keyPressed(KeyEvent e)
	{
		if(e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			lebar = 40;
			tinggi = 20;
			ordinatX ++;
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			lebar = 40;
			tinggi = 20;
			ordinatX --;
		}
		if(e.getKeyCode() == KeyEvent.VK_DOWN)
		{
			lebar = 20;
			tinggi = 40;
			ordinatY ++;
		}
		if(e.getKeyCode() == KeyEvent.VK_UP)
		{
			lebar = 20;
			tinggi = 40;
			ordinatY --;
		}
		geserX = (getWidth()/2) + ordinatX;
		geserY = (getHeight()/2) + ordinatY;
		repaint ();
	}
	public void paint(Graphics g)
	{
		g.clearRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.BLACK);
		g.drawRect(geserX, geserY + ordinatY, lebar, tinggi);
	}
}

No related posts.

Tulisan terkait dibawakan kepada Anda oleh Yet Another Related Posts Plugin.