Last active
August 29, 2015 14:07
-
-
Save ornirus/5d38a1482d8f9c9763cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.event.*; | |
import java.util.*; | |
public class RandomCircle extends JFrame | |
implements ActionListener { | |
int xPos, yPos, direction, distance; | |
private JPanel panel; | |
private Random random; | |
private javax.swing.Timer timer; | |
public static void main(String[] args) { | |
RandomCircle frame = new RandomCircle(); | |
frame.setSize(800, 800); | |
frame.createGUI(); | |
frame.setVisible(true); | |
} | |
private void createGUI() { | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
Container window = getContentPane(); | |
window.setLayout(new FlowLayout() ); | |
random = new Random(); | |
panel = new JPanel(); | |
panel.setPreferredSize(new Dimension(700, 700)); | |
panel.setBackground(Color.lightGray); | |
window.add(panel); | |
timer = new javax.swing.Timer(10, this); | |
timer.start(); | |
xPos = 350; | |
yPos = 350; | |
direction = 0; | |
} | |
public void actionPerformed(ActionEvent event) { | |
Graphics paper = panel.getGraphics(); | |
paper.setColor(Color.blue); | |
paper.drawOval(xPos, yPos, 15, 15); | |
direction = random.nextInt(4); | |
distance = (random.nextInt(6) + 5) * 2; | |
paper.setColor(Color.red); | |
paper.fillOval(350, 350, 15, 15); | |
if (direction == 0) { | |
yPos = yPos - distance; | |
yPos = (yPos + 685) % 685; | |
} | |
if (direction == 1) { | |
yPos = yPos + distance; | |
yPos = (yPos + 685) % 685; | |
} | |
if (direction == 2) { | |
xPos = xPos + distance; | |
xPos = (xPos + 685) % 685; | |
} | |
if (direction == 3) { | |
xPos = xPos - distance; | |
xPos = (xPos + 685) % 685; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment