Access Millions of academic & study documents

I have two classes One is Carnivore and the other is Herbivore

Content type
User Generated
Showing Page:
1/3
// I have two classes One is Carnivore and the other is
Herbivore. Each of them is having its own shape and move
function across the screen. Now i want to put them
togehter in the following Environment Class so that i can
see them moving across the screen simultaneously. But
this function is giving me a blank screen if i try to add both
of those classes. If i add just one class, it works. What am
i doing wrong in this Java coding? import
javax.swing.JFrame; import javax.swing.JPanel; public
class Environment { public static Environment earth;
public Environment() { Carnivore s = new
Carnivore(); Herbivore h = new Herbivore();
JFrame f = new JFrame(); JPanel pane = new
JPanel(); pane.add(s); pane.add(h);
f.add(pane); f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle(\"The Jungle Life\"); } }
Solution
Modified your code... you should have main method, then
we have to create and panel and add this panel to frame
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Environment
{

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/3
//frame instance declared
public JFrame f;
//constructor
public Environment(){
prepareGUI();
}
//main method
public static void main(String args[]){
Environment earth = new Environment();
earth.showEnvironment();
}
//preparing frame in this method
public void prepareGUI()
{
f = new JFrame();
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle(\"The Jungle Life\");
}
//then finally adding this frame to panel
public void showEnvironment(){
//creating carnivore and herbivore objects
Carnivore s = new Carnivore();
Herbivore h = new Herbivore();

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/3

Sign up to view the full document!

lock_open Sign Up
Unformatted Attachment Preview
// I have two classes One is Carnivore and the other is Herbivore. Each of them is having its own shape and move function across the screen. Now i want to put them togehter in the following Environment Class so that i can see them moving across the screen simultaneously. But this function is giving me a blank screen if i try to add both of those classes. If i add just one class, it works. What am i doing wrong in this Java coding? import javax.swing.JFrame; import javax.swing.JPanel; public class Environment { public static Environment earth; public Environment() { Carnivore s = new Carnivore(); Herbivore h = new Herbivore(); JFrame f = new JFrame(); JPanel pane = new JPanel(); pane.add(s); pa ne.add(h); f.add(pane); f.setVisible(true); f.setSize(600,400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle(\"The Jungle Life\"); } } Solution Modified your code... you should ha ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4

Similar Documents