Computer Science Unit 2 – Graphics and Animation
Unit 2 – Lab 10 Polka Dot
This program will be saved in a Lab10 folder. We'll use 'package Lab10 ' to create this new folder for our separate program for Lab10.
1. Lab10 See Packets: Unit2 Graphics and Animation p. 37 Lab10 Polka Dot
2. Create Driver10.java, PolkaDot.java, and PolkaDotPanel.java:
Right click on Unit2Progs: Source Packages
New – Java class
Class Name: Driver10
Project: Unit2Progs
Location: Source Packages
Package: Lab10 ← DON'T FORGET THIS STEP TOO
Right click on Unit2Progs: Source Packages
New – Java class
Class Name: PolkaDot
Project: Unit2Progs
Location: Source Packages
Package: Lab10 ← DON'T FORGET THIS STEP TOO
Right click on Unit2Progs: Source Packages
New – Java class
Class Name: PolkaDotPanel
Project: Unit2Progs
Location: Source Packages
Package: Lab10
- Type or copy in code for Driver10:
package Lab10;
import javax.swing.JFrame;
public class Driver10
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Unit2, Lab10: Polka Dots");
frame.setSize(400, 400);
frame.setLocation(0, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new PolkaDotPanel());
frame.setVisible(true);
}
}
4. Polka Dot
package Lab10;
import java.awt.*;
public class Polkadot
{
private double myX; // x and y coordinates of center
private double myY;
private double myDiameter;
private Color myColor;
private double myRadius;
// constructors
public Polkadot() //default constructor
{
myX = 200;
myY = 200;
myDiameter = 25;
myColor = Color.RED;
myRadius = myDiameter/2;
}
public Polkadot(double x, double y, double d, Color c)
{
myX = x;
myY = y;
myDiameter = d;
myColor = c;
myRadius = d/2;
}
// accessor methods
public double getX()
{
return myX;
}
public double getY()
{
//______
}
public double getDiameter()
{
//______
}
public Color getColor()
{
//______
}
public double getRadius()
{
return myRadius;
}
// modifier methods
public void setX(double x)
{
myX = x;
}
public void setY(/*______*/)
{
//______
}
public void setColor(Color c)
{
myColor = c;
}
public void setDiameter(double d)
{
myDiameter = d;
myRadius = d/2;
}
public void setRadius(double r)
{
myRadius = r;
myDiameter = 2*r;
}
// instance methods
public void jump(int rightEdge, int bottomEdge)
{
// moves location to random (x, y) within the edges
myX = (Math.random()* (rightEdge-myDiameter) + myRadius);
myY = (Math.random()* (bottomEdge-myDiameter) + myRadius);
}
public void draw(Graphics myBuffer)
{
myBuffer.setColor(myColor);
myBuffer.fillOval((int)(getX() - getRadius()), (int)(getY()-getRadius()), (int)getDiameter(), (int)getDiameter());
}
}
5. PolkaDotPanel:
package Lab10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class PolkaDotPanel extends JPanel
{
//constants
private static final int FRAME = 400;
private static final Color BACKGROUND = new Color(204, 204, 204);
//fields
private BufferedImage myImage;
private Graphics myBuffer;
private Timer t;
private Polkadot pd, pd2;
private int xPos, yPos;
public PolkaDotPanel()
{
myImage = new BufferedImage(FRAME, FRAME, BufferedImage.TYPE_INT_RGB);
myBuffer = myImage.getGraphics();
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, FRAME, FRAME);
pd = new Polkadot();
t = new Timer(1000, new Listener());
t.start();
}
public void paintComponent(Graphics g)
{
g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
}
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
/**************************
your code goes here set the color of myBuffer to BACKGROUND: myBuffer.setColor(..)
in myBuffer, fill a rectangle at (0,0), width = FRAME, height = FRAME
myBuffer.fillRectangle(x,y, width, height)
**************************/
pd.jump(FRAME, FRAME);
pd.draw(myBuffer); // Also have the 2nd polka dot, pd2, jump and draw itself
repaint();
}
}
}