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

Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
Please use Java language in an easy way with comments! Thanks! Create a calculator that uses...
Please use Java language in an easy way with comments! Thanks! Create a calculator that uses an array of buttons for numbers from 0-9, the . for decimals, and for operators +, -, * ,/ and = as well as a JTextfield that displays the current value or the result. Use ActionListeners and LayoutManagers appropriately. The example below shows how to determine which button has been clicked. ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand()); }...
Please use java language in an easy way with comments! Expand your below program, so it...
Please use java language in an easy way with comments! Expand your below program, so it can include cursed items (weapons that break, armor that causes more damage when worn). In order to do that, write an interface called "CursedItem". Then create subclasses of the armor, and weapon class that implement the CursedItem interface : CursedWeapon: using a random number generator, there is a 4 in 10 chance that the weapon breaks during combat. CursedArmor: this item amplifies the damage...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT