Test2.java

Download: java1.zip.


Test2.java

// Test2.java
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.util.ArrayList;

public class Test2 extends JPanel
{
    ArrayList<Ball> balls = new ArrayList<Ball>();

    Test2()
    {
	balls.add(new Ball(50,50,Color.RED));
	balls.add(new Ball(100,120,Color.BLUE));
	balls.add(new Ball(160,50,Color.YELLOW));
	System.out.println("Balls:");
	for (Ball b: balls) System.out.println(b);
    }
    public void paintComponent(Graphics g)
    {
	super.paintComponent( g );
	for (Ball b: balls) b.draw(g);
    }

    public static void main(String args[] )
    {
	Test2 panel = new Test2();
	JFrame application = new JFrame("Test2");
	application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	application.add(panel);
	application.setSize(200,200);
	application.setVisible(true);
    }
}


Ball.java

import java.awt.*;

public class Ball
{
    double px, py;
    int radius;
    double mass;
    Color color;
    Ball()
    {
	color = new Color(255,0,0);
	set_position(50,50);
	set_radius(17);
    }
    Ball(double px, double py, Color color)
    {
	this.color = color;
	set_position(px,py);
	set_radius(17);
    }

    void set_position(double px, double py)
    {
	this.px = px;
	this.py = py;
    }
    
    void set_radius(int r)
    {
	radius = r;
	mass = r*r;
    }

    public String toString()
    {
	String str = "("+px+", "+py+") " + color;
	return str;
    }
    
    
    public void draw(Graphics g) 
    {
	g.setColor(color);
	g.fillOval((int)(px-radius), (int) (py-radius), (int)2*radius, (int)2*radius);
    }

    
}


Maintained by John Loomis, updated Thu Jun 02 16:50:01 2016