Created
March 31, 2016 18:59
-
-
Save AnnaBoro/8948b8917b134fe35730b9724f99ecc4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package lesson9; | |
import java.awt.*; | |
public class Ball implements Runnable{ | |
private int x; | |
private int y; | |
private int speed; | |
private Color color; | |
public Ball() { | |
} | |
public Ball(int x, int y, int speed, Color color) { | |
this.x = x; | |
this.y = y; | |
this.speed = speed; | |
this.color = color; | |
} | |
public int getX() { | |
return x; | |
} | |
public void setX(int x) { | |
this.x = x; | |
} | |
public int getY() { | |
return y; | |
} | |
public void setY(int y) { | |
this.y = y; | |
} | |
public int getSpeed() { | |
return speed; | |
} | |
public void setSpeed(int speed) { | |
this.speed = speed; | |
} | |
public Color getColor() { | |
return color; | |
} | |
public void setColor(Color color) { | |
this.color = color; | |
} | |
public void draw(Graphics g) { | |
g.setColor(this.color); | |
g.fillOval(x, y, 40, 40); | |
} | |
@Override | |
public void run() { | |
while (true) { | |
for (int i = 0; i < 536; i ++) { | |
x += 1; | |
try { | |
Thread.sleep(speed); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
if (x < 536) { | |
continue; | |
} | |
break; | |
} | |
for (int i = 0; i < 536; i ++) { | |
x -= 1; | |
try { | |
Thread.sleep(speed); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or 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
package lesson9; | |
public class DemoBalls { | |
public static void main(String[] args) { | |
ViewPanel panel = new ViewPanel(); | |
panel.runBalls(); | |
} | |
} |
This file contains hidden or 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
package lesson9; | |
import java.util.List; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.util.ArrayList; | |
public class ViewPanel extends JPanel{ | |
final int BF_WIDTH = 576; | |
final int BF_HEIGHT = 576; | |
private List<Ball> ballList; | |
public ViewPanel() { | |
ballList = new ArrayList<>(); | |
JFrame frame = new JFrame("Flying ball"); | |
frame.setLocation(500, 150); | |
frame.setMinimumSize(new Dimension(BF_WIDTH, BF_HEIGHT + 22)); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.getContentPane().add(this); | |
setBallList(); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
for (Ball b : ballList) { | |
b.draw(g); | |
} | |
} | |
public void setBallList() { | |
ballList.add(new Ball(0, 0, 2, Color.YELLOW)); | |
ballList.add(new Ball(0, 50, 3, Color.MAGENTA)); | |
ballList.add(new Ball(0, 100, 1, Color.GREEN)); | |
ballList.add(new Ball(0, 150, 5, Color.BLUE)); | |
ballList.add(new Ball(0, 200, 4, Color.PINK)); | |
ballList.add(new Ball(0, 250, 8, Color.DARK_GRAY)); | |
ballList.add(new Ball(0, 300, 6, Color.RED)); | |
ballList.add(new Ball(0, 350, 9, Color.CYAN)); | |
ballList.add(new Ball(0, 400, 10, Color.ORANGE)); | |
} | |
public void runBalls() { | |
for (Ball b : ballList) { | |
new Thread(b).start(); | |
try { | |
Thread.sleep(25); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
while (true) { | |
repaint(); | |
try { | |
Thread.sleep(16); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment