0

this is my first post here. I'm a student taking AP Computer Science in high school. For a project, I have to draw a picture of a basketball. Okay, easy enough. The problem is that I have to do this using correct Composition and Inheritance. I can't just throw a bunch of graphics methods in the main class. I've got all the basic code down, but here is what I need help with...

  1. I want class Basketball to inherit everything from super-class Circle. Did I do that correctly by extending class Circle in the class Basketball heading?
  2. Inside of class Basketball, how can I link to class Lines and class AirValve in order to show proper composition?

As you can see, I understand the concepts but I don't understand how to make them happen very well.

import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.Color;
import java.awt.Graphics;


/* The point of this lab is to draw a basketball using one example of
 * proper inheritance and two examples of proper composition. 
 */



public class Basketball extends Circle
{
public static void main(String[] args)
{
   Lines line = new Lines();
   AirValve airvalve = new AirValve();
}

}

class Lines
{

public Lines()
{

}

public void paintLine(Graphics g)
{
    g.drawArc(300, 275, 200, 275, 295, 135); //left arc
    g.drawLine(650, 150, 650, 650); //middle line
    g.drawLine(400, 400, 900, 400); //cross line
    g.drawArc(800, 275, 200, 275, 245, -135); //right arc
}

}

class Circle
{

public Circle()
{

}

public void paintCirc(Graphics g)
{
    g.setColor(Color.orange);
    g.fillOval(400, 150, 500, 500);
}

}

class AirValve
{

public AirValve()
{

}

public void paintAV(Graphics g)
{
    g.setColor(Color.black);
    g.fillOval(645, 625, 10, 10);
}

}
Xstian
  • 8,184
  • 10
  • 42
  • 72
wildeca
  • 3
  • 2
  • a basketball is not a circle... aren't you supposed to implement some sort of `Drawable` instead? then basketball could be composited from several drawables, i.e. an oval, some lines and the air valve – BeyelerStudios Dec 09 '15 at 16:35
  • It's 2D, so it is a circle. – wildeca Dec 10 '15 at 14:49

1 Answers1

0

Inheritance:

public class Circle extends JComponent {
  //draws a circle
  public void paintComponent(Graphics g) {
   //...
  }
}

public class Basketball extends Circle {
  //draws a basketball
  public void paintComponent(Graphics g) {
   super.paintComponent(g); // this will draw the circle outline of your basketball
   //draw the middle
  }
}

And composition:

//draws a circle
public static void drawCircle(Graphics g, int x, int y) {
}

//draws a cross
public static void drawCircle(Graphics g, int x, int y) {
}

//draws a basketball
public static void drawBasketball(Graphics g, int x, int y) {
  drawCircle(g, x, y);
  drawCross(g, x, y);
}
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80