bkafroboy69 0 Newbie Poster

these are all my codes and im trying to make shapoes in myshapetesrer2.these are the directions i have to follow but i dont kn ow if i did tem right any help?

Write an abstract class: ColoredShape
A ColoredShape has a position (x and y coordinates that are integers) and a color (of type Color). The constructor should have these parameters in this order. The class will also have an boolean attribute filled which is initially false. The class will have method and mutator methods for the four attributes. The accessor attribute for filled will be called isFilled. The x and y coordinates represent the upper left corner of the bounding box of the shape.

The class will have a toString method that produces a String containing all of the attributes of the ColoredShape, all on one line. You can use the toString method of Color to describe the color.

The class will have 2 abstract methods:
public abstract void draw(Graphics g);
public abstract boolean inside(int x, int y);

Write a class called ColoredRectangle that extends ColoredShape. The constructor of ColoredRectangle will have the same parameters as the constructor for ColoredShape followed by two additional integer parameters, width and height. The class should have accessor and mutator methods for these extra attributes.

Override the toString method as appropriate.

Since ColoredRectangle is not abstract, you must implement methods draw and inside. For now, inside will just return false. The draw method will draw the rectangle with the appropriate color with its upper left corner given by its x and y coordinates. If the filled attribute is set, the rectangle will be drawn as filled using fillRect. Otherwise, draw the rectangle with drawRect.

Write a test program called ShapeTester2 to test your ColoredRectangle class. It should create a ShapeFrame with title bar similar to the one in part 1, but it should say Part 2. Create a few rectangles of various sizes and colors, some filled, some not. Add them to the ShapeFrame with a 1 second delay between each add. After adding a shape, and before the delay, add a message to the ShapeFrame describing the shape you are adding. You can use the toString to describe the shape. Test all of the public methods of ColoredRectangle that you have written, including the ones inherited form ColoredShape. Use the addMessage method to display the results. Lastly, output a message indicating that Part 2 tests have been completed.

package graphics;

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


public class ShapeTester2 {

    public static void main(String[] args) {
        ShapeFrame ColoredRectangle = new ShapeFrame("Shapes Part 2 by Matthew Gamble",600,200);
    
        
        
        
        ColoredRectangle.addMessage("Part 2 Completed");
        ColoredRectangle.delay(1000);
        ColoredRectangle.addMessage("\nPart 2 tester is done");
        ColoredRectangle.draw();
    }
}
package graphics;

import java.awt.*;

public class ColoredRectangle extends ColoredShape{

    private int width;
        private int height;
    private Graphics2D graphics; 
    
    
    
    public  ColoredRectangle(int x1,int y1,String color1,int wid,int hei){
        super(x1,y1,color1);
        width = wid;
        height = hei;
    }
    public int getWidth() {
        return width;
    }
    public int getHeight() {
        return height;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    
    public void draw(Graphics g) {
        graphics.drawRect(10, 10, 100, 100);

        
    }

    
    
        public boolean inside(int x, int y) {
        
        return false;
    }

public String toString(){
    return "The x cooridinates are" + x + "The y cooridinates are" + y + 
    "The color is "+color+",the width is"+width+"the height is"+height;
}

}
package graphics;

import java.awt.Graphics;

public abstract class ColoredShape {
    protected int x;
    protected int y;
    protected String color;
    private boolean filled;
    
    
    
    public ColoredShape(int x1,int y1,String color1){
        x = x1;
        y = y1;
        color= color1;
    }

    public boolean isFilled() {
        return filled;
    }



    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public String getColor() {
        return color;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public abstract void draw(Graphics g);
    
    public abstract boolean inside(int x, int y);

    
    public String toString(){
        return "The x cooridinates are" + x + "The y cooridinates are" + y + 
        "The color is "+color;
    }    
}
package graphics;



import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class ShapeFrame extends JFrame {

   private ArrayList<ColoredShape> shapes;
   private ShapePanel shapePanel;
   private JTextArea textArea;
   private JScrollPane scrollPane;
   private Color background = Color.lightGray;

   public ShapeFrame(String title, int width, int height) {
      super(title);
      try {
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      } catch (Exception e) {}
      shapePanel = new ShapePanel(width, height);
      JPanel mainPanel = new JPanel();
      mainPanel.setLayout(new BoxLayout(mainPanel,
            BoxLayout.Y_AXIS));
      mainPanel.add(shapePanel);
      textArea = new JTextArea(10, 20);
      textArea.setEditable(false);
      scrollPane = new JScrollPane(textArea);
      mainPanel.add(scrollPane);
      getContentPane().add(mainPanel);
      pack();
      shapes = new ArrayList<ColoredShape>();
      setVisible(true);
   }

   public void addMessage(String s) {
      textArea.append(s);
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            scrollToBottom();
         }
      });
   }

   public synchronized void addShape(ColoredShape x) {
      shapes.add(x);
      shapePanel.repaint();
   }
   
   public synchronized ColoredShape[] getAllShapes() {
      ColoredShape[] shapesArray  = new ColoredShape[shapes.size()];
      for (int i=0;i<shapes.size();i++)
         shapesArray[i] = shapes.get(i);
      return shapesArray;
   }

   public synchronized void clear() {
      shapes.clear();
      shapePanel.repaint();
   }

   public void delay(int n) {
      try {
         Thread.sleep(n);
      } catch (InterruptedException e) {
      }
   }
   
   public int getGraphicsWidth() {
      return shapePanel.getBounds().width;
   }
   
   public int getGraphicsHeight() {
      return shapePanel.getBounds().height;
   }

   public void refresh() {
      shapePanel.repaint();
   }

   private void scrollToBottom() {
      JScrollBar sb = scrollPane.getVerticalScrollBar();
      if (sb == null)
         return;
      sb.setValue(sb.getMaximum());
   }
   
   private synchronized void drawAllShapes(Graphics g) {
      for (int i = 0; i < shapes.size(); i++)
        shapes.get(i).draw(g);
   }

   private class ShapePanel extends JPanel {

      private Dimension preferredSize;

      public ShapePanel(int width, int height) {
         preferredSize = new Dimension(width, height);
      }

      public Dimension getPreferredSize() {
         return preferredSize;
      }

      public void paintComponent(Graphics g) {
        System.out.println("Number of shapes: " + shapes.size());
         g.setColor(background);
         Rectangle bounds = getBounds();
         g.fillRect(0, 0, bounds.width, bounds.height);
         drawAllShapes(g);
      }

   }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.