Add a draw() method to your Horse class. You will also need to create a couple o
ID: 3552800 • Letter: A
Question
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
Explanation / Answer
//Use your DriverPanelDriver class as you asked not to change........
//Modified DrawPanel class
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Horse h1,h2;
DrawPanel()
{
h1= new Horse();
h2 = new Horse();
setVisible(true);
}
public void paintComponent( Graphics g )
{
// call super class method
super.paintComponent ( g );
g.drawLine(0,0, 200,175);
//Here you can set the different properties for your Horse object by calling different functions defiened in Horse class
h1.draw(g );//sending the graphics reference to draw class of the Horse....for Horse object 1
h2.draw(g );//sending the graphics reference to draw class of the Horse....for Horse object 2
h1.setAge(20);
h1.setBreed("German");
h1.setName("Angel");
h1.sleep( );
}
}
--------------------------------------------------------------
//modified Horse class
import java.awt.Graphics;
public class Horse
{
private String name;
private int age;
private String breed;
Horse()
{
name = "Horse";
age = 10;
breed = "American";
}
public void draw(Graphics gg)
{
//for different horse object you can draw different picture or something using gg reference and graphics functions
}
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" );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.