I was given an assignment to create a 3-d shape and prompt the user for the surface area and volume. I have multiple errors in my coding, but after research, I can not solve the problems.I attempted to fix the problem,but can not so I have // it out. Can anyone solve or steer me in the right direction?
The code is below:
//Point originOne = new Point(23, 94);
//Rectangle rectOne = new Rectangle(originOne, 100, 200);
//Rectangle rectTwo = new Rectangle(50,100);
//Point originOne;
//Point originOne = new Point(23, 94);
import javax.swing.JOptionPane;
public static void main(String[] args) {
int width;
int height;
Rectangle(volume,area);
JOptionpane.showMessageDialog("please input integer");
public static int volume;
int vol;
int side;
vol =side*3;
public static int area;
int
JOptionPane.showMessageDialog( null,"information",
, JOptionPane.OK_CANCEL_OPTION);
JOptionPane.showInputDialog("Please input a value");
public static int surfacearea;
}
public class Rectangle {
public int x = 0;
public int y = 0;
//constructor
public void Point(int a, int b) {
x = a;
y = b;
}
public int width = 0;
public int height = 0;
public int Point ;
public int origin;
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
// a method for computing the area of the rectangle
public int Area() {
//return width * height;
}
}
This should help slightly.. There is still a lot wrong with it. I hope my comments help a little, if you need anything else feel free to comment.
import javax.swing.JOptionPane;
import java.awt.Point;
public class Rectangle {
public static int area;
public static int volume;
public int x = 0;
public int y = 0;
public int width = 0;
public int height = 0;
public int Point; // Uh?
public int origin; // this isnt a Point and yet you are creating an instance of Point..
//constructor
/**
* What is even the point of this????
*/
public void Point(int a, int b) {
x = a;
y = b;
}
// main needs to go in a class
public static void main(String[] args) {
int width;
int height;
Rectangle myRectangle = new Rectangle(volume,area); // actually set a variable
//JOptionpane.showMessageDialog("please input integer"); // research this..
int vol;
int side; //side isnt initialized?
vol = side*3; //this will not work
//JOptionPane.showMessageDialog( null,"information", JOptionPane.OK_CANCEL_OPTION);
//JOptionPane.showInputDialog("Please input a value");
}
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
//This isnt calling your point method...
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x; // Again, this is declared as an int.
origin.y = y; // this is declared as a int.
}
// a method for computing the area of the rectangle
public int Area() {
return width * height;
}
}
Related
I'm trying to draw the Mandelbrot Set, with points in the set as black, and everything else as white. In this initial version, I do not wish to be able to zoom in but rather just create a static image.
I created a ComplexNumber class, as shown below, to handle squaring and adding complex numbers together.
public class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary){
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber times(ComplexNumber number){
double a = this.real*number.real;
double b = this.imaginary*number.real;
double c = this.real*number.imaginary;
double d = this.imaginary*number.imaginary*-1;
double newReal = a+d;
double newImaginary = b+c;
ComplexNumber newComplexNumber = new ComplexNumber(newReal, newImaginary);
return newComplexNumber;
}
public ComplexNumber add(ComplexNumber number){
double newReal = this.real+number.real;
double newImaginary = this.imaginary+number.imaginary;
return new ComplexNumber(newReal, newImaginary);
}
public double abs(){
return Math.hypot(this.real, this.imaginary);
}
public double getReal() {
return real;
}
public double getImaginary() {
return imaginary;
}
}
And here is the code where I render the GUI and actually calculate the points in the Mandelbrot Set.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class MandelBrotSet extends JComponent {
public static final int WIDTH = 800;
public static final int HEIGHT = 800;
public static final int ITERATIONS = 100;
public static final double startX = -2;
public static final double width = 4;
public static final double startY = 2;
public static final double height = 4;
public static final double dx = width/(WIDTH-1);
public static final double dy = height/(HEIGHT-1);
private BufferedImage buffer;
public MandelBrotSet() {
buffer = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
JFrame frame = new JFrame("Mandelbrot Set");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
}
#Override
public void addNotify() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
#Override
public void paint(Graphics g) {
g.drawImage(buffer, 0, 0, null);
}
public void render(){
for (int x=0; x<WIDTH; x++){
for (int y=0; y<HEIGHT; y++){
int color = calculatePoint(x, y);
buffer.setRGB(x, y, color);
}
}
}
public int calculatePoint(int x, int y){
ComplexNumber number = convertToComplex(x, y);
ComplexNumber z = number;
int i;
for (i=0; i<ITERATIONS; i++){
z = z.times(z).add(number);
if (z.abs()>2.0){
break;
}
}
if (i==ITERATIONS) {
return 0x00000000;
}
else {
return 0xFFFFFFFF;
}
}
public static ComplexNumber convertToComplex(int x, int y){
double real = startX + x*dx;
double imaginary = 2 - y*dy;
return new ComplexNumber(real, imaginary);
}
public static void main(String[] args) {
MandelBrotSet mandy = new MandelBrotSet();
mandy.render();
}
}
After running this code, I'm getting the image below. There seems to be a small glimpse of the Mandelbrot set, but then it's obscured by a ton of black. What am I doing wrong?
Updated Solution Below. Thanks for the help.
The rendering of your image takes longer than displaying the MandelBrotSet-Object, and it is never updated afterwards.
You create an object of type MandelBrotSet, then immediately call it's render method. During the creation of that object, you put it in a JFrame which you immediately display. When the frame is displayed, the rendering is incomplete, which is why your ImageBuffer is not yet filled (it takes longer to build the mandelbrot-form).
To solve this, you could repaint the involved components and the frame. My understanding of awt's repaint functions is not good enough to tell you how exactly to do it right (maybe someone else can help there), but adding this to the end of your render method should help:
revalidate();
repaint();
Either revalidate or repaint can probably be omitted.
Would be cool, if you updated your question with the done image :)
Was giving the first code below. What is the proper way to create the Turtle Class? -- Basically, I am trying to get this to show no error: Turtle t = new Turtle(STARTX, STARTY, w);
I think my problem might be here with the overload constructor: public Turtle (double STARTX, double STARTY, Class w )
import java.awt.*; //import color;
public class PA1{
//These are constant values that you can use
private static final int STARTX = 100;
private static final int STARTY = 100;
private static final int CHAR_WIDTH = 100;
private static final int CHAR_HEIGHT = 100;
private static final int CHAR_SPACING = 50;
public static void main(String[] args){
//set the width and height of the world
int width = 1000;
int height = 1000;
World w = new World(width, height);
//create a turtle at the starting x and starting y pos
Turtle t = new Turtle(STARTX, STARTY, w);
//Set the turtle pen width.
t.setPenWidth(15);
//This is just an example. Feel free to use it as a reference.
//draw a T
//Assume that the turtle always starts in the top left corner of the character.
t.turn(90);
t.forward(CHAR_WIDTH);
t.backward(CHAR_WIDTH/2);
t.turn(90);
t.forward(CHAR_HEIGHT);
//Move the turtle to the next location for the character
t.penUp();
t.moveTo(STARTX+CHAR_WIDTH+CHAR_SPACING*1, STARTY);
t.penDown();
//WRITE YOUR CODE HERE
}
}
I created 2 new class:
public class World {
//World w = new World(width, height);
private double defaultWidth;
private double defaultLength;
public World () {
defaultWidth = 0.0;
defaultLength = 0.0; }
public World (double width, double length){
defaultWidth = width;
defaultLength = length; }
public double getWidth () {
return defaultWidth; }
public double getLength () {
return defaultLength; }
public void setWidth (double width){
defaultWidth = width; }
public void setLength(double length){
defaultLength = length; }
}
and
public class Turtle {
// Turtle t = new Turtle(STARTX, STARTY, w);
private double defaultSTARTX;
private double defaultSTARTY;
//private double defaultW;
public Turtle () {
defaultSTARTX = 0.0;
defaultSTARTY = 0.0;
//defaultW = 0.0;
}
public Turtle (double STARTX, double STARTY, Class w ){
defaultSTARTX = STARTX;
defaultSTARTY = STARTY;
//defaultW = w;
}
public double getSTARTX () {
return defaultSTARTX; }
public double getSTARTY () {
return defaultSTARTY; }
public void setSTARTX (double STARTX){
defaultSTARTX = STARTX; }
public void setSTARTY(double STARTY){
defaultSTARTY = STARTY; }
}
If you really need to pass a World object to the Turtle object, you should define the Turtle constructor as this:
public Turtle (double STARTX, double STARTY, World w ){
defaultSTARTX = STARTX;
defaultSTARTY = STARTY;
defaultW = w;
}
Also, you must declare "w" not as double, as you have done at some point, but as a "World" class variable in Turtle:
private World defaultW;
Passing Class as a constructor parameter, you are trying to pass a generic class definition, not an Object instance of any class. The diference is subtle, but is there, and is your most likely issue.
I found many issues in the code you provided above. I do not know if it was the full code or not. I tried to fix all the problems in your code - related to your constructor and other java standards. Below is working for me -
public class MyClass {
private static final int STARTX = 100;
private static final int STARTY = 50;
public static void main(String args[]) {
int width = 1000;
int height = 1000;
World w = new World(width, height);
//create a turtle at the starting x and starting y pos
Turtle t = new Turtle(STARTX, STARTY, w);
System.out.println(t.getSTARTX() + " : " + t.getSTARTY() + " : " + t.getWorld().getLength() + " : " + t.getWorld().getWidth());
}
}
class World {
private double width;
private double length;
public World () {
this.width = 0.0;
this.length = 0.0;
}
public World (double width, double length) {
this.width = width;
this.length = length;
}
public double getWidth () { return this.width; }
public double getLength () { return this.length; }
public void setWidth (double width){ this.width = width; }
public void setLength(double length){ this.length = length; }
}
class Turtle {
private double STARTX;
private double STARTY;
private World world;
public Turtle () {
this.STARTX = 0.0;
this.STARTY = 0.0;
this.world = new World();
}
public Turtle (double STARTX, double STARTY, World w) {
this.STARTX = STARTX;
this.STARTY = STARTY;
this.world = w;
}
public double getSTARTX () { return this.STARTX; }
public double getSTARTY () { return this.STARTY; }
public World getWorld(){ return this.world; }
public void setSTARTX (double STARTX){ this.STARTX = STARTX; }
public void setSTARTY(double STARTY){ this.STARTY = STARTY; }
public void setWorld (World world){ this.world = world; }
}
Hope it resolves your query.
Happy coding. :)
I am trying to develop a checkerboard given a template of classes and some code for school. I got the board to appear but the right amount of checkers are not being drawn. There are supposed to be 7 red and 9 black checkers but each time I run the program a different amount of each is drawn.
import java.applet.Applet;
import java.awt.*;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Checkers extends JApplet
{
private final int MAX_SIZE = 8;
private final int APP_WIDTH = 400;
private final int APP_HEIGHT = 400;
private final int MAXSIZE = 8;
Square[][] sq;
public void paint(Graphics page)
{
setBackground(Color.white);
fillBoard(page); // draws the method that will draw the checkers
placeCheckers(page, 7, Color.red); //method to place the red checkers
placeCheckers(page, 9, Color.black); //method to draw black checkers
CheckJumps(page); //check if checkers can jump
setSize (APP_WIDTH,APP_HEIGHT);
}
public void fillBoard(Graphics page)
{
sq = new Square[8][8];
int x,y;
Color rb;
for (int row = 0; row < MAXSIZE; row++)
for (int col = 0; col < MAXSIZE; col++)
{
x = row * (APP_WIDTH/MAXSIZE);
y = col * (APP_HEIGHT/MAXSIZE);
if ( (row % 2) == (col % 2) )
rb = Color.red;
else
rb = Color.black;
sq[row][col] = new Square (x, y, rb);
}
for (int row = 0; row < 8; row++)
for (int col = 0; col < 8; col++)
sq[row][col].draw(page);
}
public void placeCheckers (Graphics page, int num_checkers, Color ncolor)
{
int count, row, col;
int x, y;
Circle c;
Random rand = new Random();
for (count = 0; count < num_checkers; count++)
{
do
{
row = rand.nextInt(8);
col = rand.nextInt(8);
} while (sq[row][col].getOccupy() || ncolor == sq[row][col].getColor());
x = row * (APP_WIDTH/MAXSIZE);
y = col * (APP_HEIGHT/MAXSIZE);
c = new Circle (x, y, 50, ncolor);
c.draw(page);
sq[row][col].setOccupy(true);
}
}
class Square
{
private int x, y = 0;
private Color c;
private boolean occupied;
public Square (int x, int y, Color c)
{
this.x = x;
this.y = y;
this.c = c;
}
public void setX (int x)
{
x = this.x;
}
public int getX ()
{
return x;
}
public void setY (int y)
{
y= this.y;
}
public int getY ()
{
return y;
}
public void setColor (Color c)
{
c = this.c;
}
public Color getColor ()
{
return c;
}
public void setOccupy (boolean occupied)
{
occupied = this.occupied;
}
public boolean getOccupy ()
{
return occupied;
}
public String toString()
{
return ("X coordinate: " + x + "\nY coordinate:" + y + "\nSquare color: " + c);
}
public void draw (Graphics page)
{
page.setColor(c);
page.fillRect(x, y, 50, 50);
}
}
class Circle
{
private int x,y;
private int diameter;
private Color c;
public Circle (int x, int y, int diameter, Color c)
{
this.x = x;
this.y = y;
this.diameter = diameter;
this.c = c;
}
public void setX (int x)
{
x = this.x;
}
public int getX ()
{
return x;
}
public void setY (int y)
{
y= this.y;
}
public int getY ()
{
return y;
}
public void setColor (Color c)
{
c = this.c;
}
public Color getColor ()
{
return c;
}
public void setDiameter (int x)
{
diameter = x;
}
public void draw (Graphics page)
{
page.setColor(c);
page.fillOval(x, y, diameter, diameter);
}
}
If, you have followed some of the advice from your previous question you may have avoided this issue.
As near as I can tell, your problem is you're not calling super.paint, which is responsible for (amongst a lot of other things) preparing the Graphics context for painting. It does this, by clearing what ever was painted on it previously.
Instead of overriding paint of JApplet, which will cause flicker when the applet is updated, you should start with something like a JPanel and override it's paintComponent method. JPanel is double buffered, which will prevent any flicker from occuring. Don't forget to call super.paintComponent.
You shouldn't be calling fillBorder every time paint is called, this is wasteful on a number of levels, instead, you should only call it when you need to. With a little bit more clever design, you could actually get away with calling it from the constructor, but I don't have the time to re-code your entire program.
The size the applet is defined by the HTML page which contains it, not the applet itself, relying on magic numbers (like APP_WIDTH and APP_HEIGHT) is a bad idea. You should, instead, rely on known values, like getWidth and getHeight. This of course assumes you'd like to be able to resize the playable area and avoid possible issues with people deploying your applet with the wrong size ;)
While, I'm guessing that placeCheckers is a test method, you should know, paint can be called any number of times for any number of reasons, many of which you don't control, this means that the checkers will be randomized each time paint is called.
Instead, you should consider creating a virtual board which contains the information about the state of the game and update this as required. You would then simply use the painting process to reflect this model.
An example of how I might "start"...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JApplet;
import javax.swing.JPanel;
public class Checkers extends JApplet {
#Override
public void init() {
add(new Board());
}
public class Board extends JPanel {
private final int APP_WIDTH = 400;
private final int APP_HEIGHT = 400;
private final int MAXSIZE = 8;
Square[][] sq;
#Override
public void invalidate() {
fillBoard();
super.invalidate();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
sq[row][col].draw(g);
}
}
setBackground(Color.white);
placeCheckers(g, 7, Color.red); //method to place the red checkers
placeCheckers(g, 9, Color.black); //method to draw black checkers
}
#Override
public Dimension getPreferredSize() {
return new Dimension(APP_WIDTH, APP_HEIGHT);
}
public void fillBoard() {
sq = new Square[8][8];
int x, y;
Color rb;
int gridSize = Math.min(getWidth(), getHeight());
int size = gridSize / MAXSIZE;
for (int row = 0; row < MAXSIZE; row++) {
for (int col = 0; col < MAXSIZE; col++) {
x = row * (gridSize / MAXSIZE);
y = col * (gridSize / MAXSIZE);
if ((row % 2) == (col % 2)) {
rb = Color.red;
} else {
rb = Color.black;
}
sq[row][col] = new Square(x, y, rb, size);
}
}
}
public void placeCheckers(Graphics page, int num_checkers, Color ncolor) {
int count, row, col;
int x, y;
Circle c;
int gridSize = Math.min(getWidth(), getHeight());
int size = gridSize / MAXSIZE;
Random rand = new Random();
for (count = 0; count < num_checkers; count++) {
do {
row = rand.nextInt(8);
col = rand.nextInt(8);
} while (sq[row][col].getOccupy() || ncolor == sq[row][col].getColor());
x = row * (gridSize / MAXSIZE);
y = col * (gridSize / MAXSIZE);
c = new Circle(x, y, size, ncolor);
c.draw(page);
sq[row][col].setOccupy(true);
}
}
}
class Square {
private int x, y = 0;
private Color c;
private boolean occupied;
private int size;
public Square(int x, int y, Color c, int size) {
this.x = x;
this.y = y;
this.c = c;
this.size = size;
}
public void setX(int x) {
x = this.x;
}
public int getX() {
return x;
}
public void setY(int y) {
y = this.y;
}
public int getY() {
return y;
}
public void setColor(Color c) {
c = this.c;
}
public Color getColor() {
return c;
}
public void setOccupy(boolean occupied) {
occupied = this.occupied;
}
public boolean getOccupy() {
return occupied;
}
public String toString() {
return ("X coordinate: " + x + "\nY coordinate:" + y + "\nSquare color: " + c);
}
public void draw(Graphics page) {
page.setColor(c);
page.fillRect(x, y, size, size);
}
}
class Circle {
private int x, y;
private int diameter;
private Color c;
public Circle(int x, int y, int diameter, Color c) {
this.x = x;
this.y = y;
this.diameter = diameter;
this.c = c;
}
public void setX(int x) {
x = this.x;
}
public int getX() {
return x;
}
public void setY(int y) {
y = this.y;
}
public int getY() {
return y;
}
public void setColor(Color c) {
c = this.c;
}
public Color getColor() {
return c;
}
public void setDiameter(int x) {
diameter = x;
}
public void draw(Graphics page) {
page.setColor(c);
page.fillOval(x, y, diameter, diameter);
}
}
}
Updated
This ones had me scratching me head for a while. Basically, after some additional checking I discovered that the checkers where being allowed to occupy space that was suppose to be already taken. After bashing me head against the do-while loop, I check the setOccupy method and found...
public void setOccupy(boolean occupied) {
occupied = this.occupied;
}
You're assiging the Square's occupied state back to the value you are passing, which has no effect on anything
Instead, it should look more like...
public void setOccupy(boolean occupied) {
this.occupied = occupied;
}
You may also like to have a read through Why CS teachers should stop teaching Java applets
This is my class that everything is done in:
import java.awt.Color;
import java.awt.Graphics;
//This class will not compile until all
//abstract Locatable methods have been implemented
public class Block implements Locatable
{
//instance variables
private int xPos;
private int yPos;
private int width;
private int height;
private Color color;
//constructors
public Block () {}
public Block(int x,int y,int w,int h)
{
xPos = x;
yPos = y;
width = w;
height = h;
}
public Block(int x,int y,int w,int h, Color c)
{
xPos = x;
yPos = y;
width = w;
height = h;
color = c;
}
//set methods
public void setBlock(int x, int y, int w, int h)
{
xPos = x;
yPos = y;
width = w;
height = h;
}
public void setBlock(int x, int y, int w, int h, Color c)
{
xPos = x;
yPos = y;
width = w;
height = h;
color = c;
}
public void draw(Graphics window)
{
window.setColor(color);
window.fillRect(getX(), getY(), getWidth(), getHeight());
}
//get methods
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
//toString
public String toString()
{
String complete = getX() + " " + getY() + " " + getWidth() + " " + getHeight() + " java.awt.Color[r=" + color.getRed() + ", g=" + color.getGreen() + ", b=" + color.getBlue() + "]";
return complete;
}
}
and here is my interface class that has to be implemented:
public interface Locatable
{
public void setPos( int x, int y);
public void setX( int x );
public void setY( int y );
public int getX();
public int getY();
}
I haven't had formal instruction yet on interfaces/implementations and thus, am not sure what needs to be done to get the first class to run right
When you implementing an interface, you have to implement all the methods declared in that interface.
Interface is a contract that your implementing class must full fill.in your case your implementing class Block should implement the following methods to full fill the contract.
public void setPos( int x, int y);
public void setX( int x );
public void setY( int y );
public int getX();
public int getY();
public class Block implements Locatable {
public void setPos( int x, int y){
// your implementatioon code
}
public void setX( int x ) {
// your implementatioon code
}
public void setY( int y ){
// your implementatioon code
}
public int getX(){
// your implementatioon code
return (an int value);
}
public int getY(){
// your implementatioon code
return (an int value);
}
}
EDIT: for your NPE from the comments.
you never initialized your Color object.and trying to call a method on its refrence in your toString method.
private Color color;
initialize it like this
private Color color = new Color(any of the Color constructors);
check here for Color API
Bah. I figured out the bug. Of course it was absurdly simple. The overridden update under MovingPanelItem needs to be written as follows:
#Override
public void update( int x, int y )
{
xCoord = xCoord + getxStep();
yCoord = yCoord + getyStep();
}
FULL DISCLOSURE: This is homework.
The PROBLEM:
Upon each Key listener event the screen should be updated and the moving objects should move. Currently, while the objects show up initially, if I press the prescribed key they disappear.
Also, all of the PanelItem's are stored in an ArrayList ( in another class ). All of the objects which are not subclasses of MovingPanelItem remain on the screen upon the KeyEvent.
I know I've left a lot to the imagination so if more detail is needed please let me know.
The superclass:
public class PanelItem
{
private Image img;
protected int xCoord;
protected int yCoord;
protected int width;
protected int height;
//constructor
public SceneItem( String path, int x, int y, int w int h )
{
xCoord = x;
yCoord = y;
setImage( path, w, h );
}
public void SetImage( String path, int w, int h )
{
width = w;
height = h;
img = ImageIO.read(new File(path));
}
//to be Overriden
public void update( int width, int height )
{
}
}
The Subclass:
public class MovingPanelItem extends PanelItem
{
private int xStep, yStep;
// x & y correspond to the coordinate plane
// w & h correspond to image width and height
// xs & ys correspond to unique randomized 'steps' in the for the x and y values
public MovingSceneItem(String path, int x, int y, int w, int h, int xs, int ys)
{
super( path, x, y, w, h);
setxStep(xs);
setyStep(ys);
update( x, y );
}
#Override
public void update( int x, int y )
{
this.xCoord = x + getxStep();
this.yCoord = y + getyStep();
}
}
(In response to Eels)
The panel/window itself is contained within the following class:
public class Panel extends JPanel {
protected ArrayList<PanelItem> panelItems;
private JLabel statusLabel;
private long randomSeed;
private Random random;
public Panel(JLabel sl) {
panelItems = new ArrayList<PanelItem>();
statusLabel = sl;
random = new Random();
randomSeed = 100;
}
private void addPanel() {
addPanelItems(10, "Mouse");
}
private void addPanelItems(int num, String type) {
Dimension dim = getSize();
dim.setSize(dim.getWidth() - 30, dim.getHeight() - 30);
synchronized (panelItems) {
for (int i = 0; i < num; i++) {
int x = random.nextInt(dim.width);
int y = random.nextInt(dim.height);
if (type.equals("Person")) {
int xs = random.nextInt(21) - 10;
int ys = random.nextInt(21) - 10;
panelItems.add(new Mouse(x, y, xs, ys));
}
}
repaint();
}
// causes every item to get updated.
public void updatePanel() {
Dimension dim = getSize();
synchronized (panelItems) {
for (PanelItem pi : panelItems) {
pi.update(dim.width, dim.height);
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
synchronized (panelItems) {
for (PanelItem pi : panelItems) {
pi.draw(g);
}
}
}
}
KeyListener class within the test class:
private class MyKeyListener extends KeyAdapter
{
#Override public void keyTyped(KeyEvent e)
{
if(e.getKeyChar() == 'f') {
panel.updatePanel();
panel.repaint();
}
// other key events include reseting the panel &
// creating a new panel, both of which are working.
}
}
Within the above code the only code I'm allowed to change ( & the only code I've written ) is the subclass MovingPanel item.