SlideShare a Scribd company logo
1 of 2
// 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
{
//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();
JPanel panel = new JPanel();
panel.add(s);
panel.add(h);
f.add(panel);
f.setVisible(true);
}
}

More Related Content

More from dorisc7

- At time 110 four processes (P1 P4) are waiting for a timeout signal.docx
- At time 110 four processes (P1  P4) are waiting for a timeout signal.docx- At time 110 four processes (P1  P4) are waiting for a timeout signal.docx
- At time 110 four processes (P1 P4) are waiting for a timeout signal.docxdorisc7
 
(70 pts) Nuclear History Identify the event in nuclear history that oc.docx
(70 pts) Nuclear History Identify the event in nuclear history that oc.docx(70 pts) Nuclear History Identify the event in nuclear history that oc.docx
(70 pts) Nuclear History Identify the event in nuclear history that oc.docxdorisc7
 
(2) The CPU incorporates several major components (as shown in Fig- de.docx
(2) The CPU incorporates several major components (as shown in Fig- de.docx(2) The CPU incorporates several major components (as shown in Fig- de.docx
(2) The CPU incorporates several major components (as shown in Fig- de.docxdorisc7
 
(Audit evidence) The third GAAS of field work requires that the audito.docx
(Audit evidence) The third GAAS of field work requires that the audito.docx(Audit evidence) The third GAAS of field work requires that the audito.docx
(Audit evidence) The third GAAS of field work requires that the audito.docxdorisc7
 
What is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docx
What is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docxWhat is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docx
What is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docxdorisc7
 
What is the concept of sustainable income- Please describe it in your.docx
What is the concept of sustainable income- Please describe it in your.docxWhat is the concept of sustainable income- Please describe it in your.docx
What is the concept of sustainable income- Please describe it in your.docxdorisc7
 
What is the backbone for the Internet- Briefly describe how the Intern.docx
What is the backbone for the Internet- Briefly describe how the Intern.docxWhat is the backbone for the Internet- Briefly describe how the Intern.docx
What is the backbone for the Internet- Briefly describe how the Intern.docxdorisc7
 
What is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docx
What is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docxWhat is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docx
What is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docxdorisc7
 
what is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docx
what is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docxwhat is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docx
what is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docxdorisc7
 
What is the pH of a 0-114 M solution of the acid HA if its ionization (1).docx
What is the pH of a 0-114 M solution of the acid HA if its ionization (1).docxWhat is the pH of a 0-114 M solution of the acid HA if its ionization (1).docx
What is the pH of a 0-114 M solution of the acid HA if its ionization (1).docxdorisc7
 
What is the minimum number of nodes in an AVL tree of height 8- Let N(.docx
What is the minimum number of nodes in an AVL tree of height 8- Let N(.docxWhat is the minimum number of nodes in an AVL tree of height 8- Let N(.docx
What is the minimum number of nodes in an AVL tree of height 8- Let N(.docxdorisc7
 
What is the meaning of the keyword this- Explain why it is valid only.docx
What is the meaning of the keyword this- Explain why it is valid only.docxWhat is the meaning of the keyword this- Explain why it is valid only.docx
What is the meaning of the keyword this- Explain why it is valid only.docxdorisc7
 
what is the foundation for strategy implementation-SolutionA challengi.docx
what is the foundation for strategy implementation-SolutionA challengi.docxwhat is the foundation for strategy implementation-SolutionA challengi.docx
what is the foundation for strategy implementation-SolutionA challengi.docxdorisc7
 
What is the future of cloud computing--SolutionCloud computing occupie.docx
What is the future of cloud computing--SolutionCloud computing occupie.docxWhat is the future of cloud computing--SolutionCloud computing occupie.docx
What is the future of cloud computing--SolutionCloud computing occupie.docxdorisc7
 
What is the amount of the capitalization of retained earnings for the.docx
What is the amount of the capitalization of retained earnings for the.docxWhat is the amount of the capitalization of retained earnings for the.docx
What is the amount of the capitalization of retained earnings for the.docxdorisc7
 
What is the formal definition of a CSIRT- What is the difference in th.docx
What is the formal definition of a CSIRT- What is the difference in th.docxWhat is the formal definition of a CSIRT- What is the difference in th.docx
What is the formal definition of a CSIRT- What is the difference in th.docxdorisc7
 
What is the difference between the creation and the exploitation of te.docx
What is the difference between the creation and the exploitation of te.docxWhat is the difference between the creation and the exploitation of te.docx
What is the difference between the creation and the exploitation of te.docxdorisc7
 
What is the difference between routing and forwarding- Do routers hav.docx
What is the difference between routing and forwarding-  Do routers hav.docxWhat is the difference between routing and forwarding-  Do routers hav.docx
What is the difference between routing and forwarding- Do routers hav.docxdorisc7
 
What is the difference between information found on the internet and o.docx
What is the difference between information found on the internet and o.docxWhat is the difference between information found on the internet and o.docx
What is the difference between information found on the internet and o.docxdorisc7
 
What is the difference between Information System and Information Tech.docx
What is the difference between Information System and Information Tech.docxWhat is the difference between Information System and Information Tech.docx
What is the difference between Information System and Information Tech.docxdorisc7
 

More from dorisc7 (20)

- At time 110 four processes (P1 P4) are waiting for a timeout signal.docx
- At time 110 four processes (P1  P4) are waiting for a timeout signal.docx- At time 110 four processes (P1  P4) are waiting for a timeout signal.docx
- At time 110 four processes (P1 P4) are waiting for a timeout signal.docx
 
(70 pts) Nuclear History Identify the event in nuclear history that oc.docx
(70 pts) Nuclear History Identify the event in nuclear history that oc.docx(70 pts) Nuclear History Identify the event in nuclear history that oc.docx
(70 pts) Nuclear History Identify the event in nuclear history that oc.docx
 
(2) The CPU incorporates several major components (as shown in Fig- de.docx
(2) The CPU incorporates several major components (as shown in Fig- de.docx(2) The CPU incorporates several major components (as shown in Fig- de.docx
(2) The CPU incorporates several major components (as shown in Fig- de.docx
 
(Audit evidence) The third GAAS of field work requires that the audito.docx
(Audit evidence) The third GAAS of field work requires that the audito.docx(Audit evidence) The third GAAS of field work requires that the audito.docx
(Audit evidence) The third GAAS of field work requires that the audito.docx
 
What is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docx
What is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docxWhat is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docx
What is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docx
 
What is the concept of sustainable income- Please describe it in your.docx
What is the concept of sustainable income- Please describe it in your.docxWhat is the concept of sustainable income- Please describe it in your.docx
What is the concept of sustainable income- Please describe it in your.docx
 
What is the backbone for the Internet- Briefly describe how the Intern.docx
What is the backbone for the Internet- Briefly describe how the Intern.docxWhat is the backbone for the Internet- Briefly describe how the Intern.docx
What is the backbone for the Internet- Briefly describe how the Intern.docx
 
What is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docx
What is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docxWhat is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docx
What is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docx
 
what is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docx
what is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docxwhat is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docx
what is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docx
 
What is the pH of a 0-114 M solution of the acid HA if its ionization (1).docx
What is the pH of a 0-114 M solution of the acid HA if its ionization (1).docxWhat is the pH of a 0-114 M solution of the acid HA if its ionization (1).docx
What is the pH of a 0-114 M solution of the acid HA if its ionization (1).docx
 
What is the minimum number of nodes in an AVL tree of height 8- Let N(.docx
What is the minimum number of nodes in an AVL tree of height 8- Let N(.docxWhat is the minimum number of nodes in an AVL tree of height 8- Let N(.docx
What is the minimum number of nodes in an AVL tree of height 8- Let N(.docx
 
What is the meaning of the keyword this- Explain why it is valid only.docx
What is the meaning of the keyword this- Explain why it is valid only.docxWhat is the meaning of the keyword this- Explain why it is valid only.docx
What is the meaning of the keyword this- Explain why it is valid only.docx
 
what is the foundation for strategy implementation-SolutionA challengi.docx
what is the foundation for strategy implementation-SolutionA challengi.docxwhat is the foundation for strategy implementation-SolutionA challengi.docx
what is the foundation for strategy implementation-SolutionA challengi.docx
 
What is the future of cloud computing--SolutionCloud computing occupie.docx
What is the future of cloud computing--SolutionCloud computing occupie.docxWhat is the future of cloud computing--SolutionCloud computing occupie.docx
What is the future of cloud computing--SolutionCloud computing occupie.docx
 
What is the amount of the capitalization of retained earnings for the.docx
What is the amount of the capitalization of retained earnings for the.docxWhat is the amount of the capitalization of retained earnings for the.docx
What is the amount of the capitalization of retained earnings for the.docx
 
What is the formal definition of a CSIRT- What is the difference in th.docx
What is the formal definition of a CSIRT- What is the difference in th.docxWhat is the formal definition of a CSIRT- What is the difference in th.docx
What is the formal definition of a CSIRT- What is the difference in th.docx
 
What is the difference between the creation and the exploitation of te.docx
What is the difference between the creation and the exploitation of te.docxWhat is the difference between the creation and the exploitation of te.docx
What is the difference between the creation and the exploitation of te.docx
 
What is the difference between routing and forwarding- Do routers hav.docx
What is the difference between routing and forwarding-  Do routers hav.docxWhat is the difference between routing and forwarding-  Do routers hav.docx
What is the difference between routing and forwarding- Do routers hav.docx
 
What is the difference between information found on the internet and o.docx
What is the difference between information found on the internet and o.docxWhat is the difference between information found on the internet and o.docx
What is the difference between information found on the internet and o.docx
 
What is the difference between Information System and Information Tech.docx
What is the difference between Information System and Information Tech.docxWhat is the difference between Information System and Information Tech.docx
What is the difference between Information System and Information Tech.docx
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

-- I have two classes One is Carnivore and the other is Herbivore- Eac.docx

  • 1. // 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 { //frame instance declared public JFrame f; //constructor public Environment(){ prepareGUI(); } //main method public static void main(String args[]){ Environment earth = new Environment(); earth.showEnvironment(); }
  • 2. //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(); JPanel panel = new JPanel(); panel.add(s); panel.add(h); f.add(panel); f.setVisible(true); } }