Question

In: Computer Science

This is Java Programing. Add a shape of oval to BOXBALLOVAL. import java.awt.*; import java.awt.event.*; import...

This is Java Programing. Add a shape of oval to BOXBALLOVAL.

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.util.Timer;

import javax.swing.*;

public class BOXBALLOVAL {

   public static void main(String[] args) {

new myframe();// creating main jframe instance

   }

}

class myframe extends JFrame

{

   Container c;

   JPanel panel;

   JButton addbutton, removebutton;

   JLabel counter, ballsize;

   JTextField size_input;

   JComboBox cb;

   buttonListener handle;

   myDrawboard myboard;

   JFrame mainFrame;

   public myframe()

   {

super("Your title");

c = getContentPane();

size_input = new JTextField(5);

counter = new JLabel("Count : ");

ballsize = new JLabel("Size : ");

  

size_input.setText("50");

addbutton = new JButton("Add");

removebutton = new JButton("Remove");

cb = new JComboBox();

cb.addItem("Ball");

cb.addItem("Box");

handle = new buttonListener();

addbutton.addActionListener(handle);

removebutton.addActionListener(handle);

panel = new JPanel();

panel.add(ballsize);

panel.add(size_input);

panel.add(addbutton);

panel.add(removebutton);

panel.add(counter);

panel.add(cb);

myboard = new myDrawboard();

c.add(myboard.panel, BorderLayout.CENTER);

c.add(panel, BorderLayout.SOUTH);

  

setSize(800, 600);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

// update screen (refresh)

Timer timer = new Timer();

timer.schedule(new myTimer(), 0, 16);

   }

   class myTimer extends TimerTask

   {

@Override

public void run() {

   repaint();

}

   }

   class buttonListener implements ActionListener {

int i;

public void actionPerformed(ActionEvent action)

{

   if (action.getSource() == addbutton) {

if (!size_input.getText().equals("")) {

   try

   {

myboard.additem(Integer.parseInt(size_input.getText()), cb.getSelectedItem().toString());

counter.setText(" Count : " + myboard.countItem()+ " ");

   }

   catch (NumberFormatException e)

   {

System.out.println(e);

JOptionPane.showMessageDialog(null, "Enter only number!", "Invalid Input", JOptionPane.INFORMATION_MESSAGE);

   }

}

else

{

   JOptionPane.showMessageDialog(null, "Enter the Object size!", "Input needed", JOptionPane.INFORMATION_MESSAGE);

}

   }

   if (action.getSource() == removebutton)

   {

myboard.removeBall();

counter.setText(" Count : " + myboard.countItem()+ " ");

   }

}

   }

}

class myDrawboard

{

   private static int count = 0;

   Graphics2D g2;

   MyPanel panel = new MyPanel();

   public void additem(int size, String shape)

   {

count++;

panel.addShape(size, shape);

   }

   public String countItem() {

return Integer.toString(count);

   }

   public void removeBall() {

if (panel.deleteShape()) {

   count--;

}

   }

}

class MyPanel extends JPanel

{

   ArrayList myArrayList = new ArrayList();

   public MyPanel()

   {

   setBackground(Color.BLACK);

   }

   public void addShape(int size, String shape)

   {

Random randomGenerator = new Random();

int x = randomGenerator.nextInt(200);

int y = randomGenerator.nextInt(200);

int R = randomGenerator.nextInt(256);

int G = randomGenerator.nextInt(256);

int B = randomGenerator.nextInt(256);

int vx = randomGenerator.nextInt(10)+2;

int vy = randomGenerator.nextInt(10)+2;

Color randomcolor = new Color(R, G, B);

if (shape == "Box")

{

   Box box = new Box();

   box.setInfo(size, x, y, randomcolor, vx, vy);

   myArrayList.add(box);

}

else // shape==ball

{

   Ball ball = new Ball();

   ball.setInfo(size, x, y, randomcolor, vx, vy);

   myArrayList.add(ball);

}

   }

   public boolean deleteShape()

   {

if (myArrayList.size() > 0)

{

   myArrayList.remove(myArrayList.size() - 1); // remove the last one

   return true;

}

return false;

   }

   public void paintComponent(Graphics g)

   {

Graphics2D g2 = (Graphics2D) g;

g2.setColor(Color.BLACK);

g2.fillRect(0,0,getWidth(), getHeight());

for (int i = 0; i < myArrayList.size(); i++)

{

   myArrayList.get(i).update(getWidth(), getHeight());

   myArrayList.get(i).drawObject(g2);

}

   }

}

interface DrawObject

{

   void drawObject(Graphics2D g2);

   void update(int width, int height);

}

class Ball implements DrawObject

{

   private int size;

   int x;

   int y;

   int velX;

   int velY;

   private Color color;

   public void setInfo(int size, int x, int y, Color randomcolor, int vx, int vy)

   {

this.size = size;

this.x = x;

this.y = y;

this.velX = vx;

this.velY = vy;

this.color = randomcolor;

   }

   public void drawObject(Graphics2D g2)

   {

g2.setColor(color);

g2.fillOval(x, y, size * 2, size * 2);

g2.setFont(new Font("SansSerif", Font.BOLD, 50));

   // g2.drawString("cs211",x,y);

g2.drawString("CS211",100,100);

   }

   //Ball moving

   public void update(int width, int height)

   {

x += velX;

if(x < 0 || x > width-size*2)

velX *= -1;

y += velY;

if(y < 0 || y > height-size*2)

velY *= -1;

   }

}

class Box implements DrawObject

{

   private int size;

   int x;

   int y;

   int velX;

   int velY;

   private Color color;

   private Rectangle square;

   public void setInfo(int size, int x, int y, Color randomcolor, int vx, int vy)

   {

square = new Rectangle(x, y, size, size);

this.velX = vx;

this.velY = vy;

color = randomcolor;

   }

   public void drawObject(Graphics2D g2)

   {

g2.setColor(color);

g2.fillRect(square.x, square.y, square.width, square.height);

   }

   //box moving

   public void update(int width, int height)

   {

square.x += velX;

if(square.x < 0 || square.x > width-square.width)

velX *= -1;

square.y += velY;

if(square.y < 0 || square.y > height-square.height)

velY *= -1;

   }

}

Solutions

Expert Solution

A)

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.util.Timer;

import javax.swing.*;

class ShapeCollision {

private BOXBALL;

private frame limits;

private int w = 800;

private int h = 600;

private Boxball= New Boxball(w, h);

private Boxball= New Boxball[6];

ShapeCollision() {

img = new Boxball(w, h, BufferedImage.TYPE_INT_RGB);

final Jframe = new Jframe(new frameIcon(frame))

for (int i = 0; i < boxball.length; i++) {

           Boxballoval[i] = newBoxball (60 + i * 80, 40 + i * 80,i);

       }

       limits = new frame(new Rectangle(0, 0, w, h));

public class Boxball {

public static void main(String[] args) {

new myframe();// creating main jframe instance

}

}

class myframe extends JFrame

{

Container c;

JPanel panel;

JButton addbutton, removebutton;

JLabel counter, ballsize;

JTextField size_input;

JComboBox cb;

buttonListener handle;

myDrawboard myboard;

JFrame mainFrame;

public myframe()

{

super("Your title");

c = getContentPane();

size_input = new JTextField(5);

counter = new JLabel("Count : ");

ballsize = new JLabel("Size : ");

  

size_input.setText("50");

addbutton = new JButton("Add");

removebutton = new JButton("Remove");

cb = new JComboBox();

cb.addItem("Ball");

cb.addItem("Box");

handle = new buttonListener();

addbutton.addActionListener(handle);

removebutton.addActionListener(handle);

panel = new JPanel();

panel.add(ballsize);

panel.add(size_input);

panel.add(addbutton);

panel.add(removebutton);

panel.add(counter);

panel.add(cb);

myboard = new myDrawboard();

c.add(myboard.panel, BorderLayout.CENTER);

c.add(panel, BorderLayout.SOUTH);

Timer timer = new Timer();

timer.schedule(new myTimer(), 0, 16);

}

class myTimer extends TimerTask

{

@Override

public void run() {

repaint();

}

}

class buttonListener implements ActionListener {

int i;

public void actionPerformed(ActionEvent action)

{

if (action.getSource() == addbutton) {

if (!size_input.getText().equals("")) {

try

{

myboard.additem(Integer.parseInt(size_input.getText()), cb.getSelectedItem().toString());

counter.setText(" Count : " + myboard.countItem()+ " ");

}

catch (NumberFormatException e)

{

System.out.println(e);

JOptionPane.showMessageDialog(null, "Enter only number!", "Invalid Input", JOptionPane.INFORMATION_MESSAGE);

}

}

else

{

JOptionPane.showMessageDialog(null, "Enter the Object size!", "Input needed", JOptionPane.INFORMATION_MESSAGE);

}

}

if (action.getSource() == removebutton)

{

myboard.removeBall();

counter.setText(" Count : " + myboard.countItem()+ " ");

}

}

}

}

class myDrawboard

{

private static int count = 0;

Graphics2D g2;

MyPanel panel = new MyPanel();

public void additem(int size, String shape)

{

count++;

panel.addShape(size, shape);

}

public String countItem() {

return Integer.toString(count);

}

public void removeBall() {

if (panel.deleteShape()) {

count--;

}

}

}

class MyPanel extends JPanel

{

ArrayList myArrayList = new ArrayList();

public MyPanel()

{

setBackground(Color.BLACK);

}

public void addShape(int size, String shape)

{

Random randomGenerator = new Random();

int x = randomGenerator.nextInt(200);

int y = randomGenerator.nextInt(200);

int R = randomGenerator.nextInt(256);

int G = randomGenerator.nextInt(256);

int B = randomGenerator.nextInt(256);

int vx = randomGenerator.nextInt(10)+2;

int vy = randomGenerator.nextInt(10)+2;

Color randomcolor = new Color(R, G, B);

if (shape == "Box")

{

Box box = new Box();

box.setInfo(size, x, y, randomcolor, vx, vy);

myArrayList.add(box);

}

else // shape==ball

{

Ball ball = new Ball();

ball.setInfo(size, x, y, randomcolor, vx, vy);

myArrayList.add(ball);

}

}

public boolean deleteShape()

{

if (myArrayList.size() > 0)

{

myArrayList.remove(myArrayList.size() - 1); // remove the last one

return true;

}

return false;

}

public void paintComponent(Graphics g)

{

Graphics2D g2 = (Graphics2D) g;

g2.setColor(Color.BLACK);

g2.fillRect(0,0,getWidth(), getHeight());

for (int i = 0; i < myArrayList.size(); i++)

{

myArrayList.get(i).update(getWidth(), getHeight());

myArrayList.get(i).drawObject(g2);

}

}

}

interface DrawObject

{

void drawObject(Graphics2D g2);

void update(int width, int height);

}

class Ball implements DrawObject

{

private int size;

int x;

int y;

int velX;

int velY;

private Color color;

public void setInfo(int size, int x, int y, Color randomcolor, int vx, int vy)

{

this.size = size;

this.x = x;

this.y = y;

this.velX = vx;

this.velY = vy;

this.color = randomcolor;

}

public void drawObject(Graphics2D g2)

{

g2.setColor(color);

g2.fillOval(x, y, size * 2, size * 2);

g2.setFont(new Font("SansSerif", Font.BOLD, 50));

// g2.drawString("cs211",x,y);

g2.drawString("CS211",100,100);

}

//Ball moving

public void update(int width, int height)

{

x += velX;

if(x < 0 || x > width-size*2)

velX *= -1;

y += velY;

if(y < 0 || y > height-size*2)

velY *= -1;

}

}

class Box implements DrawObject

{

private int size;

int x;

int y;

int velX;

int velY;

private Color color;

private Rectangle square;

public void setInfo(int size, int x, int y, Color randomcolor, int vx, int vy)

{

square = new Rectangle(x, y, size, size);

this.velX = vx;

this.velY = vy;

color = randomcolor;

}

public void drawObject(Graphics2D g2)

{

g2.setColor(color);

g2.fillRect(square.x, square.y, square.width, square.height);

}

//box moving

public void update(int width, int height)

{

square.x += velX;

if(square.x < 0 || square.x > width-square.width)

velX *= -1;

square.y += velY;

if(square.y < 0 || square.y > height-square.height)

velY *= -1;

}

}


Related Solutions

Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class Cipher { public static final int NUM_LETTERS = 26; public static final int ENCODE = 1; public static final int DECODE = 2; public static void main(String[] args) /* FIX ME */ throws Exception { // letters String alphabet = "abcdefghijklmnopqrstuvwxyz"; // Check args length, if error, print usage message and exit if (args.length != 3) { System.out.println("Usage:\n"); System.out.println("java...
Stack2540Array   import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array   import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1) return null ; return stack [ top ]; }...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path):...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path): """ Retrieve list of students and their expected grade from file, generate a sampled test grade for each student drawn from a Gaussian distribution defined by the student expected grade as mean, and the given standard deviation. If the sample is higher than 100, re-sample. If the sample is lower than 0 or 5 standard deviations below mean, re-sample Write the list of student...
Create a PowersTable application that displays a table of of powers. ( Java Programing )
Create a PowersTable application that displays a table of of powers. ( Java Programing )
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
// TASK #2 Add an import statement for the Scanner class // TASK #2(Alternate) // Add...
// TASK #2 Add an import statement for the Scanner class // TASK #2(Alternate) // Add an import statement for the JOptionPane class /** This program demonstrates how numeric types and operators behave in Java. */ public class NumericTypes { public static void main (String [] args) { // TASK #2 Create a Scanner object here // (not used for alternate) // Identifier declarations final int NUMBER = 2 ; // Number of scores final int SCORE1 = 100; //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT