Member Avatar for krejar

Given a UML diagram of:

Oval
-------
-------
+draw (g:Graphics, x:int, y:int)
+toString(): String

I have currently

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

public class Oval extends Shape
{
   public Oval (Color color, boolean isFilled, int width)
	{
	   super (color, isFilled, width);
	}  
	
   public void draw (Graphics g, int x, int y)
	{
	    
	}
	
	public String toString()
	{
	   String s = super.toString() + "Shape is: Oval";
		return s;
	}
}

The way this works, the draw method here will overwrite the draw method in Shape. Ironically, there is absolutely nothing in the Shape draw method.

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


public class Shape
{
   protected Color color;
	protected boolean isFilled;
	protected int width;
	
	
	public Shape (Color color, boolean isFilled, int width)
	{
	   this.color = color;
		this.isFilled = isFilled;
		this.width = width;
	}
	
	public void draw (Graphics g, int x, int y)
	{
	}
		
	public String toString()
	{
		String s = color.toString() + "" + isFilled + "" + width + "";
		return s;		
	}
}

For 1, I am not even sure this is the way it is supposed to be written and 2, for the draw method how does it work with the 3 variables? I thought that the Graphics g was a paint component.

The only instruction is that the Oval should be drawn with a height equal to 1.5 times their width. Again, with the proper API on the docs.oracle.org for the drawRect, I would need the x and y cord for a starting place, which need to be centered in the middle of the shape. However in my instructions there is no indication of what that is.

did you mean the interface Shape?

Member Avatar for krejar

No. Shape is a completely different thing here. All I am doing is making classes for myself, well, actually some school work. I am to create these classes as if I am making my own API.

It looks to me like you have correctly met the requirements of your exercise here. How it's going to work, and how it relates to the classes in the Java API - these are probably not relevant questions.

But...

Having an empty method in a superclass like that is pretty common, although the normal way to do it would be to declare the superclass as abstract, and declare the method as abstract, so subclasses are forced to provide an implementation.

When you draw things in Swing you have a paintComponent method for the panel where things are drawn, and this has a Graphics passed as a parameter. If you want to draw a number of shapes in there it's normal to pass the Graphics plus an x,y position to a method in the shape to draw itself.

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.