Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA PROGRAM using NETBEANS! Write a program that Makes a 400x400 window. Dra

ID: 3574895 • Letter: I

Question

IN JAVA PROGRAM using NETBEANS! Write a program that Makes a 400x400 window. Draw a cat at the point (350, 350) and some food at the point (50, 50). (Your artistic skills will not be evaluated.) Whenever the user presses an arrow key, move the cat in that direction 50 spaces. If the cat goes on the boarder of the window (on either 0 or 400), stop the program saying the cat fell off the wolrd (see Example 1).
If th cat reaches the food, point (50, 50), without falling off, then say the have won and quit the game (Example 2).
Hint: Key input is done by the KeyListener class. To see what input you get, run the getKeyCode() method and compare to KeyEevent.VK_LEFT and the other directions.
Example 1 (user input is underlined, bold is descriptive not verbatim):

The pattern of the kitty for EXAMPLE 1: up, right, window closes you fall off the world! Then EXAMPLE 2: left 6 times then up 6 times window closes you win!

If you don't understand shoot me a message!

Dmily student Dmily studeni M suggested Pu Mendeley Dat E Can't reach th D copy of Exple 51-Wen-outle power analysi Exploration di Exploration di hw0 x Add to the lavorites bar by selecting Arror by getting therm f another browser. Irriporl your lavorites of 18 a a

Explanation / Answer

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/*
* <applet code="Cat.class" width="400" height="400"></applet>
*/
public class Cat extends Applet implements KeyListener {

   int x=350,y=350;
   public void init()
   {
       setSize(400,400);
       addKeyListener(this);
   }
   public void paint(Graphics g){
       g.setColor(Color.BLACK);
       g.drawOval(x, y, 50, 50);
      
       g.setColor(Color.YELLOW);
       g.drawRect(50, 50, 60, 40);
      
       if(x==0 ||x==400 || y==0 ||y==40)
       {
           g.setColor(Color.RED);
           g.drawString("Cat fell off the world", 200, 200);
           try{
           Thread.sleep(3000);
       }catch(Exception e1){}
       System.exit(0);
       }
      
       if(x==50 && y==50)
       {
           g.setColor(Color.RED);
           g.drawString("You Have won", 200, 200);
           try{
               Thread.sleep(3000);
           }catch(Exception e1){}
           System.exit(0);
       }
   }
  
  
  
   public void keyPressed(KeyEvent k){
  
       if(k.getKeyCode()==KeyEvent.VK_LEFT)
       {
          
           x=x-50;
          
           repaint();
          
       }
      
       else if(k.getKeyCode()==KeyEvent.VK_RIGHT)
       {
          
           x=x+50;
           repaint();
       }
      
       else if(k.getKeyCode()==KeyEvent.VK_UP)
       {
               y=y-50;
           repaint();
       }
      
       else if(k.getKeyCode()==KeyEvent.VK_DOWN)
       {
          
           y=y+50;
           repaint();
       }
      
   }
  
   public void keyReleased(KeyEvent k){
      
   }
  
   public void keyTyped(KeyEvent k){
      
   }
  
   }