Question

In: Computer Science

Please use Java language with comments! Thanks! Write a program that will display multiple dots move...

Please use Java language with comments! Thanks!

Write a program that will display multiple dots move across the Frame and randomly change direction when they hit the edge of the screen. Do this by creating a Dot class and making each dot an object of that class. You may reuse code written in class for this part of the assignment. Create a subclass of the Dot class called PlayerDot that is controlled by the player through keyboard input (arrow keys) instead of moving randomly. You only need one object of this class.

Solutions

Expert Solution

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

class dot extends JPanel implements Runnable
{   int x=0,y=0;
int flag=0;
Thread t;
boolean b;

dot()
{
  
   start();
  
}
public void paint(Graphics g){
  
   //g.drawLine(10, 10, 200, 300);
   getRootPane().setBackground(Color.BLACK);
   if(flag==0) {
   g.setColor(Color.BLACK);
   g.drawRect(x, y, 2, 2);
  
   g.setColor(Color.red);
   g.drawRect(x+1, y+1, 2, 2);
   }
   else {
       g.setColor(Color.BLACK);
       g.drawRect(x, y, 2, 2);
      
       g.setColor(Color.red);
       g.drawRect(x-1, y-1, 2, 2);
      
   }
  
}
   public void start() {
   t = new Thread(this);
   b = false;
   t.start();
   }
   public void run () {
   char ch;
   for(;;) {
   try {
         
   repaint();
   Thread.sleep(10);
   if((x<400 || y<400)&& flag==0)
   {
   x++;
       y++;
      
     
   }
   else if(x==400 || y==400)
   {
       flag=1;
  
  
   }
   if (flag==1)
   {
       x--;
       y=y-1;
   }
     
   }
   catch(InterruptedException e) {}
   }
   }
   }

  

public class test extends JFrame{

   test()
   {int x,y;
       JFrame frame= new JFrame("Moving dot");
      
         
       frame.add(new dot());
         
  
       frame.setSize(400, 400);
       frame.setVisible(true);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setResizable(false);  
   }
   public static void main(String[] args){
          
       test t1 = new test();  
   }


Related Solutions

Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803 This assignment involves extension to the single source-single destination shortest path problem. The Program Your program should: 1. Read the name of a text file from the console. (Not the command line) 2. Read an undirected graph from the file. 3. Find the shortest path between the start and goal vertices specified in the file. 4. Print out the vertices on the path, in...
Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks 1. A driver class with a main method, which creates instances of various objects (two of each subclass) and adds them as items to an ArrayList named "inventory". A foreach loop should loop through the ArrayList and call the use() method of each item. Define the ArrayList in a way that it only holds elements of the GameItem class and its subclasses. Make proper...
Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks (Write a code question) Write a generic method called "findMin" that takes an array of Comparable objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value. (Analysis of Algorithms question) Determine the growth function and time complexity (in Big-Oh notation) of the...
Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks (Write a code question) Write a generic method called "findMin" that takes an array of String objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value.
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments)...
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments) // declare the constructor that sets the type and rate based in the sqft parm        // set values based on sqft <1000 is small with $100 per night, // sqft between 1000 and 2000, mid-sized $200 per night, and // over 2000 as a large cabin with $300 per night        //declare getRate        //declare getType        //declare setRate with int rate parm...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in Programming Project 8 from Chapter 2. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT