Trying to change the size of the shapes over time. Making them bigger as they move upwards and to the right, and making them smaller as they move downwards and to the left.

This is the class that contains the move method:

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class Shape {

  int x;
  int y;
  int width;
  int height;
  Color colour;
  int moveX = 1;
  int moveY = 1;

  /*constructor*/
  public Shape() {
    Random r = new Random();
    width = r.nextInt (21) + 10;
    height = width;
    x = r.nextInt (400 - width);
    y = r.nextInt (400 - height);
    colour = new Color (r.nextInt(256), r.nextInt(256), r.nextInt(256));
  }

  /*returns a random value within the range (low < n <= high)*/
  public int randomRange (int lo, int hi) {
    Random generator = new Random();
    return generator.nextInt (hi - lo + 1) + lo;
  }

  /*changes the location of the Shape by 1 pixel to the right and 1 pixel down every time it is called*/
  public void move() {
    x += moveX;
    y += moveY;

    //rebounds x 
    if (x <= 0 || x >= 400 - width) {
      moveX = moveX * -1;
    }

    //rebounds y
    if (y <= 0 || y >= 400 - height) {
      moveY = moveY * -1;
    }

    //Shapes wider than 15 pixels move up and down, all other Shapes move sideways
    if (width > 15) {
      y += moveY;
    } else {
      x += moveX;
    }

    //increases size of shape as it moves upwards and to the right, decreases size as it moves downwards and to the left
    if (x <= 0 || x >= 400 - width) {
        width = width += 1/2;
        height = width;
    } else {
      width = width -= 1/2;
      height = width;
    }
  }

  /*sets the colour and draws the oval*/
  public void display (Graphics g) {
    g.setColor (colour);
    g.fillOval (x, y, width, height);
  }

}//end Shape class

And this is the class that containes the main method:

import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ShapePanel extends JPanel {

  Shape[] shapes = new Shape[20];
  JPanel drawPanel, controlPanel;
  JButton addShape, start, stop;
  JTextField showNum;
  JLabel countLabel;
  int count;
  Timer timer;
  private final int DELAY = 10;

  /*creates and presents the program frame*/
  public static void main (String[] args) {
    JFrame frame = new JFrame ("Lab 21");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    ShapePanel panel = new ShapePanel();
    frame.getContentPane().add (panel);

    frame.pack();
    frame.setVisible(true);
  }//end main

  /*constructor*/
  public ShapePanel() {
    addShape = new JButton ("Add Shape");
    start = new JButton ("Start");
    stop = new JButton ("Stop");
    countLabel = new JLabel ("Count: ");
    showNum = new JTextField (2);
    drawPanel = new JPanel();

    ButtonListener listener = new ButtonListener();
    addShape.addActionListener (listener);
    start.addActionListener (listener);
    stop.addActionListener (listener);

    timer = new Timer (DELAY, listener);

    controlPanel = new JPanel();
    controlPanel.setPreferredSize (new Dimension(100, 400));
    controlPanel.add (addShape);
    controlPanel.add (countLabel);
    controlPanel.add (showNum);
    controlPanel.add (start);
    controlPanel.add (stop);

    add (controlPanel);

    DrawingPanel drawPanel = new DrawingPanel();
    add (drawPanel);
  }

  /*represents the listener for the buttons*/
  private class ButtonListener implements ActionListener {

    /*adds new Shape, controls timer and sets start and stop button functions*/
    public void actionPerformed (ActionEvent event) {

      //checks that counter is less than length of array, adds a new Shape and increments counter
      if (event.getSource() == addShape) {
        if (count < shapes.length) {
          shapes[count] = new Shape();
          count++;
          showNum.setText(Integer.toString(count));
        }
      }

      //controls the timer
      if (event.getSource() == timer) {
        for (int i = 0; i < shapes.length; i++) {
          if (shapes[i] != null) {
            shapes[i].move();
          }
        }
      }

      //sets start and stop button functions
      if (event.getSource() == stop) {
        timer.stop();
      } else if (event.getSource() == start) {
        timer.start();
      }      
      repaint();
    }
  }//end of inner ButtonListener class


  /*where the Shape objects are drawn*/
  private class DrawingPanel extends JPanel {

    /*constructor*/
    public DrawingPanel() {
      setPreferredSize (new Dimension(400, 400));
      setBackground (Color.pink);
    }

    /*loops through each valid entry of the array calling the display method on each Shape*/
    public void paintComponent (Graphics g) {
      super.paintComponent(g);
      for (int i = 0; i < shapes.length; i++) {
        if (shapes[i] != null) {
          shapes[i].display(g);
        }
      }
    }
  }//end of inner DrawingPanel class

}//end ShapePanel class

Recommended Answers

All 6 Replies

Can you explain what the code does when it executes and what the problems are?
How is the motion of the shape controlled?

Oh right, sorry. The shapes just travel randomly, bouncing off the boundaries. They're not changing their size.

Have you tried debugging the code by adding println statements to print out the values of the variables that control the size of the shapes to see what the computer sees?

Ya I tried that. It didn't really help much.

I decided to change the colour of the shapes as they enter into different quadrants instead.

. It didn't really help much.

What was printed out? The print outs should show you what is happening.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.