Make Object Move across screen and comeback on JGRASP

//© A+ Computer Science  -  www.apluscompsci.com
//Name - Leslie Sanchez
//Date - 8/30/17
//Class - P4
//Lab  - Shape

import java.awt.Color;
import java.awt.Graphics;


public class Shape
{
   //instance variables
   private int xPos;
   private int yPos;
   private int width;
   private int height;
   private Color color;

   public Shape(int x, int y, int wid, int ht, Color col)
   {
      xPos = x;
      yPos = y;
      width = wid;
      height = ht;
      color = col;
   }


   public void draw(Graphics window)
   {
   // 310,220,120,130
      window.setColor(color);
      window.fillOval(xPos, yPos, width, height);
      window.fillOval(xPos, yPos, (width/3), (height/3));
      window.fillOval(xPos+82, yPos, width-80, height-90);
      window.setColor(Color.BLACK);
      window.drawOval(xPos+40, yPos+70, width-70, height-80);
      window.drawOval(xPos+40+10, yPos+85, width-110, height-120);
      window.drawOval(xPos+40+10+20, yPos+85, width-110, height-120);
      window.fillOval(xPos+35, yPos+30, width-105, height-110);
      window.fillOval(xPos+65, yPos+30, width-105, height-110);


      //draw whatever you want
      //    ^
      //  [ :: ]
      //    ()

   }

   public void move() {
  int middlePigX = 310;
   int middlePigY = 220;
   for(int i= width;i<= getWidth()-40;i++)
   {
      for(int j = height;j<= getHeight()-40;j++)
      {
         width = i;
         height = j;
         if(i == getWidth()-40 || j == getHeight()-40)
         {
            width = middlePigX;
            height= middlePigY;
            }

            }
       }
    }


   //BONUS
   //add in set and get methods for all instance variables
   public void setXpos(int x){xPos = x;}
   public int getXPos(){ 
      return xPos;}
   public void setYpos(int y){yPos = y;}
   public int getYPos(){ 
      return yPos;}
   public void setWidth(int w){width = w;}
   public int getWidth(){
      return width;}
   public void setHeight(int h){height = h;}
   public int getHeight(){
      return height;}
   public void setColor(Color c){color = c;}
   public Color getColor(){
      return color;}

   public String toString()
   {
      return xPos+" "+yPos+" "+width+" "+height+" "+color;
   }
}

import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Canvas;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;

public class ShapePanel extends JPanel
{
    public ShapePanel()
    {
        setBackground(Color.WHITE);
        setVisible(true);
    }

    public void update(Graphics window)
    {
        paint(window);

    }

    /*
     *All of your test code should be placed in paint.
     */
    public void paint(Graphics window)
    {
        window.setColor(Color.WHITE);
        window.fillRect(0,0,getWidth(), getHeight());
        window.setColor(Color.YELLOW);
        window.drawRect(20,20,getWidth()-40,getHeight()-40);
        window.setFont(new Font("TAHOMA",Font.BOLD,30));
        window.drawString("THREE LITTLE PIGS",225,70);


        //instantiate a Shape
      Shape bigHead = new Shape(310,220,120,130,Color.PINK);
        //tell your shape to draw
      bigHead.draw(window);

        //instantiate a Shape
        //tell your shape to draw

        //instantiate a Shape
        //tell your shape to draw
    }
}

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

public class GraphicsRunner extends JFrame
{
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;

    public GraphicsRunner()
    {
        super("Graphics Runner");

        setSize(WIDTH,HEIGHT);

        getContentPane().add(new ShapePanel());

        //add other classes to run them 
        //BigHouse, Robot, or ShapePanel 

        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main( String args[] )
    {
        GraphicsRunner run = new GraphicsRunner();
    }
}

I have to create three objects and have them move, my objects are pigs but they need to move across the screen and either bounce back off the border or come back around. I've created one object and just trying to get that moving but my move method isn't working, it complies just does nothing. Oh its in three different classes . Thanks for any help!!!

Answers

  • This isn't a Java forum, it's a Processing forum. See processing.org for details.

Sign In or Register to comment.