Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package fi.jamk.balls;
- import java.awt.Graphics;
- import java.util.ArrayList;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- /**
- *
- * @author G8482
- */
- public class BallsDemo extends JFrame implements Runnable{
- private Ball ball;
- private Thread thread;
- private final int WIDTH = 800;
- private final int HEIGHT = 600;
- private ArrayList<Ball> balls;
- private final int BALLS = 1000;
- public BallsDemo() {
- super("BallsDemo");
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setSize(WIDTH, HEIGHT);
- setResizable(false);
- //this.ball = new Ball(WIDTH,HEIGHT);
- balls = new ArrayList<>();
- for (int i=1;i<=BALLS;i++) {
- balls.add(new Ball(WIDTH, HEIGHT));
- }
- getContentPane().add(new Piirtopaneeli());
- // luodaan uusi säie ohjelmaan
- this.thread = new Thread(this);
- this.thread.start();
- }
- // tämä run metodi suoritetaan omassa säikeessä
- // EI siis samassa kuin main-metodi
- @Override
- public void run() {
- while(true) {
- // game engine
- // ball.move();
- for (Ball ball : balls) ball.move();
- // piirrä näyttö uudelleen
- repaint();
- // huilataan hetki
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- }
- }
- }
- public static void main(String args[]) {
- new BallsDemo().setVisible(true);
- }
- // luokka toteuttaa JPanelin, jonka avulla piirretään "peliä" näytölle
- class Piirtopaneeli extends JPanel {
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- // g.fillOval(ball.getX(), ball.getY(), ball.getSize(), ball.getSize());
- for (Ball ball : balls) {
- g.setColor(ball.getColor());
- g.fillOval(ball.getX(), ball.getY(), ball.getSize(), ball.getSize());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement