Circle Panel GUI

Arman Majumder 0 Tallied Votes 1K Views Share

A red circle moves back and forth horizontally, when clicked (with a mouse) the circle halts motion and the background colour changes randomly, if clicked again the circle resumes it's motion and changes the background colour randomly. This loop continues until the user exits the program.

*Two separate programs, CirclePanel and Circle(Circle Class)*

// Arman Majumder
// Circle Panel GUI
// Requires Circle Class <Circle.java>  Included in bottom
   
   import java.awt.*;
   import java.awt.event.*;
   import java.util.*;
   import java.io.*;
   import javax.swing.*;
   import java.util.Random;
   
    public class CirclePanel extends JPanel
   {
      private Circle circle;
      private javax.swing.Timer timer;
   
       public CirclePanel(Color backColor, int width, int height)
      {
         setBackground(backColor);
         setPreferredSize(new Dimension(width, height));
         circle = new Circle(width / 2, height / 2, 50, Color.red);
         circle.setFilled(true);
         circle.setDirection(180);
         circle.setVelocity(5);
         timer = new javax.swing.Timer(5, new MoveListener());
         timer.start();
         
         addMouseListener(new PanelListener());
      }
   
       public void paintComponent(Graphics g)
      {
         super.paintComponent(g);
         circle.draw(g);
      }
      
       private class MoveListener implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
         {
            int x = circle.getX();
            int radius = circle.getRadius();
            int width = getWidth();
            if (x - radius <= 0 || x + radius >= width)
               circle.turn(180);
            circle.move();
            repaint();
         }
      }
      
       private class PanelListener extends MouseAdapter
      {
          public void mousePressed(MouseEvent e)
         {
            int velocity = circle.getVelocity();
         
            if (velocity == 5)
            {
               circle.setVelocity(0);
               
               Random color = new Random();           
               int red = color.nextInt(256);
               int green = color.nextInt(256);
               int blue = color.nextInt(256); 
               Color backDrop = new Color(red, green, blue);
               setBackground(backDrop);
               repaint();
            }
            
            else
            {
               circle.setVelocity(5);
            
               Random color = new Random();           
               int red = color.nextInt(256);
               int green = color.nextInt(256);
               int blue = color.nextInt(256); 
               Color backDrop = new Color(red, green, blue);
               setBackground(backDrop);
               repaint();
            }
         }
      }

       public static void main(String [] args)
      {
         JFrame theGUI = new JFrame();
         theGUI.setTitle("Circle");
         theGUI.setSize(400, 400);
         theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         CirclePanel panel = new CirclePanel(Color.white, 400, 400);
         Container pane = theGUI.getContentPane();
         pane.add(panel);
         theGUI.setVisible(true);
      }
   }

//////////////////////////////////////////////////////////////////////////////////
//Circle Class 
// New Pogram
//////////////////////////////////////////////////////////////////////////////////

// Arman Majumder
// Corresponds with Circle Panel GUI <CirclePanel.java>
// Represents a circle

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

    public class Circle
   {
      private int centerX, centerY, radius;
      private Color color;
      private int direction, velocity;
      private boolean filled;
   
       public Circle(int x, int y, int r, Color c)
      {
         centerX = x;
         centerY = y;
         radius = r;
         color = c;
         direction = 0;
         velocity = 0;
         filled = false;
      }
   
       public void draw(Graphics g)
      {
         Color oldColor = g.getColor();
         g.setColor(color);
      // Translates circle's center to rectangle's origin for drawing.
         if (filled)
            g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
         else
            g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
         g.setColor(oldColor);
      }
   
       public void fill(Graphics g)
      {
         Color oldColor = g.getColor();
         g.setColor(color);
      // Translates circle's center to rectangle's origin for drawing.
         g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
         g.setColor(oldColor);
      }
   
       public boolean containsPoint(int x, int y)
      {
         int xSquared = (x - centerX) * (x - centerX);
         int ySquared = (y - centerY) * (y - centerY);
         int radiusSquared = radius * radius;
         return xSquared + ySquared - radiusSquared <= 0;
      }
   
       public void move(int xAmount, int yAmount)
      {
         centerX = centerX + xAmount;
         centerY = centerY + yAmount;
      }
   
       public int getRadius()
      {
         return radius;
      }
   
       public int getX()
      {
         return centerX;
      }
   
       public int getY()
      {
         return centerY;
      }
   
       public int getDirection()
      {
         return direction;
      }
   
       public void setVelocity(int v)
      {
         velocity = v;
      }  
   	 
       public int getVelocity()
      {
         return velocity;
      }
   
       public void setDirection(int d)
      {
         direction = d % 360;
      }
   
       public void turn(int degrees)
      {
         direction = (direction + degrees) % 360;
      }
   
   // Moves the circle in the current direction using its
   // current velocity
       public void move()
      {
         move((int)(velocity * Math.cos(Math.toRadians(direction))),
            (int)(velocity * Math.sin(Math.toRadians(direction))));
      }
   
       public void setFilled(boolean b)
      {
         filled = b;
      }
   }
cUtEAkew 0 Newbie Poster

import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import javax.swing.*; import java.util.Random; public class Circle { private int centerX, centerY, radius; private Color color; private int direction, velocity; private boolean filled; public Circle(int x, int y, int r, Color c) { centerX = x; centerY = y; radius = r; color = c; direction = 0; velocity = 0; filled = false; } public void draw(Graphics g) { Color oldColor = g.getColor(); g.setColor(color); // Translates circle's center to rectangle's origin for drawing. if (filled) g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2); else g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2); g.setColor(oldColor); } public void fill(Graphics g) { Color oldColor = g.getColor(); g.setColor(color); // Translates circle's center to rectangle's origin for drawing. g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2); g.setColor(oldColor); } public boolean containsPoint(int x, int y) { int xSquared = (x - centerX) * (x - centerX); int ySquared = (y - centerY) * (y - centerY); int radiusSquared = radius * radius; return xSquared + ySquared - radiusSquared <= 0; } public void move(int xAmount, int yAmount) { centerX = centerX + xAmount; centerY = centerY + yAmount; } public int getRadius() { return radius; } public int getX() { return centerX; } public int getY() { return centerY; } public int getDirection() { return direction; } public void setVelocity(int v) { velocity = v; } public int getVelocity() { return velocity; } public void setDirection(int d) { direction = d % 360; } public void turn(int degrees) { direction = (direction + degrees) % 360; } // Moves the circle in the current direction using its // current velocity public void move() { move((int)(velocity * Math.cos(Math.toRadians(direction))), (int)(velocity * Math.sin(Math.toRadians(direction)))); } public void setFilled(boolean b) { filled = b; } }

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.