Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package fi.jamk.balls;
  8.  
  9. import java.awt.Graphics;
  10. import java.util.ArrayList;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13.  
  14. /**
  15. *
  16. * @author G8482
  17. */
  18. public class BallsDemo extends JFrame implements Runnable{
  19. private Ball ball;
  20. private Thread thread;
  21. private final int WIDTH = 800;
  22. private final int HEIGHT = 600;
  23. private ArrayList<Ball> balls;
  24. private final int BALLS = 1000;
  25.  
  26. public BallsDemo() {
  27. super("BallsDemo");
  28. setDefaultCloseOperation(EXIT_ON_CLOSE);
  29. setSize(WIDTH, HEIGHT);
  30. setResizable(false);
  31.  
  32. //this.ball = new Ball(WIDTH,HEIGHT);
  33. balls = new ArrayList<>();
  34. for (int i=1;i<=BALLS;i++) {
  35. balls.add(new Ball(WIDTH, HEIGHT));
  36.  
  37. }
  38. getContentPane().add(new Piirtopaneeli());
  39. // luodaan uusi säie ohjelmaan
  40. this.thread = new Thread(this);
  41. this.thread.start();
  42. }
  43.  
  44. // tämä run metodi suoritetaan omassa säikeessä
  45. // EI siis samassa kuin main-metodi
  46. @Override
  47. public void run() {
  48. while(true) {
  49. // game engine
  50. // ball.move();
  51. for (Ball ball : balls) ball.move();
  52. // piirrä näyttö uudelleen
  53. repaint();
  54. // huilataan hetki
  55. try {
  56. Thread.sleep(10);
  57. } catch (InterruptedException e) {
  58.  
  59. }
  60. }
  61. }
  62.  
  63. public static void main(String args[]) {
  64. new BallsDemo().setVisible(true);
  65. }
  66.  
  67. // luokka toteuttaa JPanelin, jonka avulla piirretään "peliä" näytölle
  68. class Piirtopaneeli extends JPanel {
  69. @Override
  70. public void paintComponent(Graphics g) {
  71. super.paintComponent(g);
  72. // g.fillOval(ball.getX(), ball.getY(), ball.getSize(), ball.getSize());
  73. for (Ball ball : balls) {
  74. g.setColor(ball.getColor());
  75. g.fillOval(ball.getX(), ball.getY(), ball.getSize(), ball.getSize());
  76. }
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement