Sabtu, 18 Desember 2010

Membuat Ukuran dan Lokasi Random


package Random;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
*
* @author manjava
*/
public class RandomLocation extends JFrame{

int[] location;
int x,y;
public RandomLocation(String title){
super(title);
setDefaultCloseOperation(3);
location = new int[2];
for(int i =0; i<2; i++){
location[i] = (int) (500*Math.random());
x= location[0];
y= location[1];
}
setContentPane(panel);
setSize(getX(),getY());
setLocation(getX(),getY());
}

JPanel panel = new JPanel(){

public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D gd = (Graphics2D) g.create();

gd.setColor(Color.red);
gd.fillOval(0, 0, getWidth(), getHeight());
gd.dispose();
}
};

public int getX(){
return x;
}
public int getY(){
return y;
}

public static void main(String[] args){
RandomLocation r = new RandomLocation("Random Location");
r.setVisible(true);
}
}

Tidak ada komentar: