Reference no: EM13166556
Add a draw() method to your Horse class. You will also need to create a couple of Horses in your DrawPanel class, and call the draw() method for each Horse from the paintComponent() method. There is no need to modify the DrawPanelDriver class.
Horse class
public class Horse
{
private String name;
private int age;
private String breed;
Horse()
{
name = "Horse";
age = 10;
breed = "American";
}
public void setName(String n)
{
name = n;
}
public void setAge(int a){
age = a;
}
public void setBreed(String b)
{
breed = b;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getBreed()
{
return breed;
}
public void eat()
{
System.out.println( name + " is eating" );
}
public void sleep()
{
System.out.println( name + " is sleeping" );
}
public void walk()
{
System.out.println( name + " is walking" );
}
public void trot()
{
System.out.println( name + " is trotting" );
}
}
DrawPanel class
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel extends JPanel
{
private Cow c1 = new Cow( 20,30, "Bob" );
private Cow c2 = new Cow( 50,70, "Dave" );
public void paintComponent( Graphics g )
{
// call super class method
super.paintComponent ( g );
g.drawLine(0,0, 200,175);
c1.draw( g );
c2.draw( g );
c1.sleep( g );
}// end paintComponent
}// end DrawPanel
DrawPanelDriver class
// Application to display DrawPanel
// No need to modify this code
import javax.swing.JFrame; // window base class
public class DrawPanelDriver
{
public static void main( String[] args )
{
// create a DrawPanel object
DrawPanel panel = new DrawPanel();
// create a new frame (window) to hold the panel
JFrame application = new JFrame();
// exit when the application is closed
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
// put our panel in the JFrame
application.add( panel );
application.setSize( 250, 250 ); // frame size
application.setVisible( true );
}
}// end of DrawPanelDriver