Inheritance - How to?
hi,
i am newbie in java and i am still tryin to learn with "how to's".
i have a project working with inheritance and interface methods of shapes.
in the project, user must be draw any shapes from the list with the JOptionpane and choose shape width, radius etc.
i wrote this code without inheritance, but i couldn't understand how to use Strings in the draw methods of each sub classes and how to use the Graphics package. can anyone show the right way to me?
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.JApplet;
publicclass DrawShapesextends JApplet{
// Class attributes
String b;//base of triangle and rectangle
String h;//height of triangle and rectangle and square
String x;//center coordinate - x
String y;//center coordinate - y
String s;//choice
String r;//radius of circle
String q;//Quantity of objects
publicvoid init(){
do{
s=JOptionPane.showInputDialog(null,"Please Choose a Shape\n" +
"1. Circle\n" +
"2. Square\n" +
"3. Triangle\n" +
"4. Rectangle\n");
}while( !s.equals("1") && !s.equals("2") && !s.equals("3")
&& !s.equals("4") );
//Chose a shape
if(s.equals("1")){
r=JOptionPane.showInputDialog(null,"Please enter a radius: ");
}
elseif (s.equals("2")){
h=JOptionPane.showInputDialog(null,"Please enter length of side: ");
}
elseif(s.equals("3")){
b=JOptionPane.showInputDialog(null,"Please enter Base: ");
h=JOptionPane.showInputDialog(null,"Please enter Height: ");
}
elseif(s.equals("4")){
b=JOptionPane.showInputDialog(null,"Please enter Width: ");
h=JOptionPane.showInputDialog(null,"Plesae enter Height: ");
}
//input center point
x=JOptionPane.showInputDialog(null,"Input center (x value): ");
y=JOptionPane.showInputDialog(null,"Input center (y value): ");
q=JOptionPane.showInputDialog(null,"Input number of shapes to draw: ");
}//end of init
publicvoid paint(Graphics g){
super.paint(g);
int i;//variable to control for is
int rad, base, height, qty, xcent, ycent;
//parse variables used in all shapes
qty=Integer.parseInt(q);
xcent=Integer.parseInt(x);
ycent=Integer.parseInt(y);
//select shape to draw
if(s.equals("1")){
rad=Integer.parseInt(r);
for(i=1;i<=qty;i++){
g.drawOval(xcent-rad,ycent-rad,rad*2,rad*2);
rad*=1.25;
}
}
elseif (s.equals("2")){
height=Integer.parseInt(h);
for(i=1;i<=qty;i++){
g.drawRect(xcent-height/2,ycent-height/2,height,height);
height*=1.5;
}
}
elseif(s.equals("3")){
double x1,x2,x3,y1,y2,y3,db,dh,dx,dy;
db=Double.parseDouble(b);//double of base
dh=Double.parseDouble(h);//double of height
dx=Double.parseDouble(x);//double of xcenter
dy=Double.parseDouble(y);//double of ycenter
for(i=1;i<=qty;i++){
x3=dx;//top point x = center x
x1=dx-db/2;//left point x
x2=dx+db/2;//right point x
y1=db/(2*-dh)*(dx-(2*dx-db/2)/2)+dy+dh/2;//y centering equation
y2=y1;//bottom y's equal to each other
y3=y1-dh;//top y is equal to bottom + height
g.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
g.drawLine((int)x1,(int)y1,(int)x3,(int)y3);//drawline takes integers
g.drawLine((int)x3,(int)y3,(int)x2,(int)y2);
db*=1.5;
dh*=1.5;
}
}
elseif(s.equals("4")){
base=Integer.parseInt(b);
height=Integer.parseInt(h);
for(i=1;i<=qty;i++){
g.drawRect(xcent-base/2,ycent-height/2,base,height);
base*=1.5;
height*=1.5;
}
}
}
}

