Question

I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class.

I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width, height, and color). I also need someone to complete the Print Class, which will print 10 rectangles and 10 ovals to the screen.

This is what I have done so far -

//Location Class (gets the x and y positions)

public class Location{
   private int x;
   private int y;

   public Location(int x, int y){
       this.x = x;
       this.y = y;
   }
   public int getX(){
       return x;
   }
   public int getY(){
       return y;
   }
}

//Dimension Class (gets the width and height)

public class Dimension{
   private int width;
   private int height;

   public Dimension(int width, int height){
       this.width = width;
       this.height = height;
   }
   public int getWidth(){
       return width;
   }
   public int getHeight(){
       return height;
   }
}

//Shape Class (super class for the shapes)

import java.awt.*;

public class Shape{
   Location loc;
   Dimension dim;
   Color c;

   public Shape(Location loc, Dimension dim, Color c){
       this.loc = loc;
       this.dim = dim;
       this.c = c;
   }
   public Location getLoc(){
       return loc;
   }
   public Dimension getDim(){
       return dim;
   }
   public Color getColor(){
       return c;
   }
}

//Rectangle Class (creates and returns a rectangle)

import java.awt.*;

public class Rectangle extends Shape{

   public Rectangle(Location loc, Dimension dim, Color c){
       super(loc, dim, c);
   }
   public Rectangle getRectangle(){
       Rectangle rec = new Rectangle(loc, dim, c);
       return rec;
   }
}

//Oval Class (creates and returns an ellipse)

import java.awt.*;
import java.awt.geom.Ellipse2D;

public class Oval extends Shape{

   public Oval(Location loc, Dimension dim, Color c){
           super(loc, dim, c);
   }
   public Ellipse2D.Double getEllipse(){
       Ellipse2D.Double ell = new Ellipse2D.Double(super.getLoc().getX(), super.getLoc().getX(), super.getDim().getWidth(),super.getDim().getHeight());

       return ell;
   }
}

//print Class (fills the arrayList with the shapes and displays it to the screen)

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class printClass extends JComponent{
   JFrame frame = new JFrame();
   ArrayList<Shape> shapes = new ArrayList<Shape>();
   //Constructor
   public printClass(){
       //Go through and set all the critaria for the frame & fill the arrayList
       //in this contructor. (There should be 10 rectangles and 10 ellipses)
   }
  
   //In the paintComponent, display the information to the screen
   public void paintComponent(Graphics g)
   public static void main(String[] args){
      
       printClass pri = new printClass();
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You.


Dimension.java

package shapes;
//Dimension Class (gets the width and height)

public class Dimension{
   private int width;
   private int height;

   public Dimension(int width, int height){
       this.width = width;
       this.height = height;
   }
   public int getWidth(){
       return width;
   }
   public int getHeight(){
       return height;
   }
}

Location.java

package shapes;
//Location Class (gets the x and y positions)


public class Location{
   private int x;
   private int y;

   public Location(int x, int y){
       this.x = x;
       this.y = y;
   }
   public int getX(){
       return x;
   }
   public int getY(){
       return y;
   }
}

Shape.java

package shapes;

//Shape Class (super class for the shapes)

import java.awt.*;

public class Shape{
   Location loc;
   Dimension dim;
   Color c;

   public Shape(Location loc, Dimension dim, Color c){
       this.loc = loc;
       this.dim = dim;
       this.c = c;
   }
   public Location getLoc(){
       return loc;
   }
   public Dimension getDim(){
       return dim;
   }
   public Color getColor(){
       return c;
   }
}

Rectangle.java

package shapes;
import java.awt.*;

//Rectangle Class (creates and returns a rectangle)


public class Rectangle extends Shape{

   public Rectangle(Location loc, Dimension dim, Color c){
       super(loc, dim, c);
   }
   public Rectangle getRectangle(){
       Rectangle rec = new Rectangle(loc, dim, c);
       return rec;
   }
}

Oval.java

package shapes;

//Oval Class (creates and returns an ellipse)

import java.awt.*;
import java.awt.geom.Ellipse2D;

public class Oval extends Shape{

   public Oval(Location loc, Dimension dim, Color c){
       super(loc, dim, c);
   }
   public Ellipse2D.Double getEllipse(){
       Ellipse2D.Double ell = new Ellipse2D.Double(super.getLoc().getX(), super.getLoc().getX(), super.getDim().getWidth(),super.getDim().getHeight());

       return ell;
   }
}

printClass.java

package shapes;

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

//print Class (fills the arrayList with the shapes and displays it to the screen)
import javax.swing.JComponent;
import javax.swing.JFrame;

public class printClass extends JComponent{
   JFrame frame = new JFrame();
   ArrayList<Shape> shapes = new ArrayList<Shape>();
   //Constructor
   public printClass(){
       //add 10 rectangles 10 circles
       Random rand = new Random();
       for(int i=0;i<10;i++){
           shapes.add(new Rectangle(new Location(rand.nextInt(700), rand.nextInt(700)),
                   new Dimension(rand.nextInt(200)+10, rand.nextInt(200)+10),
                   new Color(rand.nextInt(255),
                           rand.nextInt(255),
                           rand.nextInt(255))));
       }
       for(int i=0;i<10;i++){
           shapes.add(new Oval(new Location(rand.nextInt(700), rand.nextInt(700)),
                   new Dimension(rand.nextInt(200)+10, rand.nextInt(200)+10),
                   new Color(rand.nextInt(255),
                           rand.nextInt(255),
                           rand.nextInt(255))));
       }
   }

   //In the paintComponent, display the information to the screen
   public void paintComponent(Graphics g){
       int i;
       for (i=0;i<10;i++){
           Shape shape = shapes.get(i);
           g.setColor(shape.getColor());
           g.fillRect(shape.getLoc().getX(),
                   shape.getLoc().getY(),
                   shape.getDim().getWidth(),
                   shape.getDim().getHeight());
       }
       for (i=10;i<20;i++){
           Shape shape = shapes.get(i);
           g.setColor(shape.getColor());
           g.fillOval(shape.getLoc().getX(),
                   shape.getLoc().getY(),
                   shape.getDim().getWidth(),
                   shape.getDim().getHeight());
       }
   }
   public static void main(String[] args){
       printClass pri = new printClass();
       JFrame app = new JFrame("Shapes");
       app.add(pri, BorderLayout.CENTER);
       app.setSize(1000, 1000);
       app.setLocationRelativeTo(null);
       app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       app.setVisible(true);
   }
}

output

Shapes X Ja } pu pu

Add a comment
Know the answer?
Add Answer to:
I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Hi! I'm working on building a GUI that makes cars race across the screen. So far...

    Hi! I'm working on building a GUI that makes cars race across the screen. So far my code produces three cars, and they each show up without error, but do not move across the screen. Can someone point me in the right direction? Thank you! CarComponent.java package p5; import java.awt.*; import java.util.*; import javax.swing.*; @SuppressWarnings("serial") public class CarComponent extends JComponent { private ArrayList<Car> cars; public CarComponent() { cars = new ArrayList<Car>(); } public void add(Car car) { cars.add(car); } public...

  • Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you w...

    Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you will build the below hierarchy: Overview: You will implement the supplier code: Shape, Square, TSquare, and Triangle, which will be used by the Homework7Main program. This program will use the DrawingPanel to draw these objects onto a canvas. Specification: You will take advantage of inheritance with using a super class, Shape.java. (below) Shape is an abstract class, which means it can be extended, but...

  • I need help with my java code i am having a hard time compiling it //...

    I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...

  • Use Python 3 Create a program that uses Turtle to draw shapes. Show the following menu:...

    Use Python 3 Create a program that uses Turtle to draw shapes. Show the following menu: Enter Circle Enter Rectangle Remove Shape Draw Shapes Exit Circles – User inputs position, radius, and color. The position is the CENTER of the circle Rectangles – User inputs position, height, width, color. The position is the lower left-hand corner Colors – Allow red, yellow, blue, and green only Remove – Show the number of items in the list and let the user enter...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • I need help with a java error Question: Consider a graphics system that has classes for...

    I need help with a java error Question: Consider a graphics system that has classes for various figures—say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and center point, while a box and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system. The class Figure is...

  • import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow...

    import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT