SlideShare a Scribd company logo
1 of 5
Download to read offline
This is Java, What I am creating is a multi lab pacman type game.
This one covers movement. I basically need my code below to meet the labs requiremnts. IE I
need this game to allow two players to move.
Also anymous person answering this with my same code stop it you have been reported for spam
Thanks for the help
included is some codes
import javax.swing.JFrame;
public class MainFrame {
public static void main(String[] args)
{
// create the frame
JFrame myFrame = new JFrame("Platformer");
// set up the close operation
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create panel
MainPanel myPanel = new MainPanel();
// add panel
myFrame.getContentPane().add(myPanel);
// pack
myFrame.pack();
// set visibility to true
myFrame.setVisible(true);
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class MainPanel extends JPanel implements KeyListener{
int x = 0;
int y = 0;
ImageIcon myIcon = new ImageIcon("./src/TreasureChest.png");
public MainPanel()
{
setPreferredSize(new Dimension(1000,1000));
addKeyListener(this);
setFocusable(true);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.drawImage(myIcon.getImage(), x, y, null);
}
@Override
public void keyPressed(KeyEvent arg0) {
int keyCode = arg0.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT)
{
x -= 100;
}
else if (keyCode == KeyEvent.VK_RIGHT)
{
x += 100;
}
else if (keyCode == KeyEvent.VK_UP)
{
y -= 100;
}
else if (keyCode == KeyEvent.VK_DOWN)
{
y += 100;
}
repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
This is the overall goal for the project for this part of the lab I basically just neeed to have two
player movemet across the Jpanel so I believe I just need to use a consturer to make two
instances of the player class that uses different movement
Solution
import javax.swing.JFrame;
public class MainFrame {
public static void main(String[] args)
{
// create the frame
JFrame myFrame = new JFrame("Platformer");
// set up the close operation
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create panel
MainPanel myPanel = new MainPanel();
// add panel
myFrame.getContentPane().add(myPanel);
// pack
myFrame.pack();
// set visibility to true
myFrame.setVisible(true);
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class MainPanel extends JPanel implements KeyListener{
int x = 0;
int y = 0;
ImageIcon myIcon = new ImageIcon("./src/TreasureChest.png");
public MainPanel()
{
setPreferredSize(new Dimension(1000,1000));
addKeyListener(this);
setFocusable(true);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.drawImage(myIcon.getImage(), x, y, null);
}
@Override
public void keyPressed(KeyEvent arg0) {
int keyCode = arg0.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT)
{
x -= 100;
}
else if (keyCode == KeyEvent.VK_RIGHT)
{
x += 100;
}
else if (keyCode == KeyEvent.VK_UP)
{
y -= 100;
}
else if (keyCode == KeyEvent.VK_DOWN)
{
y += 100;
}
repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}

More Related Content

Similar to This is Java, What I am creating is a multi lab pacman type game.T.pdf

Working with the Calculator program Once imported run the.pdf
Working with the Calculator program Once imported  run the.pdfWorking with the Calculator program Once imported  run the.pdf
Working with the Calculator program Once imported run the.pdf
iconsystemsslm
 
Exercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera CymbronExercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera Cymbron
cymbron
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdf
fashionfolionr
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
anjandavid
 
Programming in java_-_17_-_swing
Programming in java_-_17_-_swingProgramming in java_-_17_-_swing
Programming in java_-_17_-_swing
josodo
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
sotlsoc
 
Java!!!!!Create a program that authenticates username and password.pdf
Java!!!!!Create a program that authenticates username and password.pdfJava!!!!!Create a program that authenticates username and password.pdf
Java!!!!!Create a program that authenticates username and password.pdf
arvindarora20042013
 

Similar to This is Java, What I am creating is a multi lab pacman type game.T.pdf (20)

Oop lecture2
Oop lecture2Oop lecture2
Oop lecture2
 
Working with the Calculator program Once imported run the.pdf
Working with the Calculator program Once imported  run the.pdfWorking with the Calculator program Once imported  run the.pdf
Working with the Calculator program Once imported run the.pdf
 
Exercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera CymbronExercícios Netbeans - Vera Cymbron
Exercícios Netbeans - Vera Cymbron
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdf
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
 
Maze
MazeMaze
Maze
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
 
Joomla! Components - Uma visão geral
Joomla! Components - Uma visão geralJoomla! Components - Uma visão geral
Joomla! Components - Uma visão geral
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
Programming in java_-_17_-_swing
Programming in java_-_17_-_swingProgramming in java_-_17_-_swing
Programming in java_-_17_-_swing
 
Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradle
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعةشرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعة
 
From Swing to JavaFX
From Swing to JavaFXFrom Swing to JavaFX
From Swing to JavaFX
 
Grain final border one
Grain final border oneGrain final border one
Grain final border one
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
 
Advance jquery-plugin
Advance jquery-pluginAdvance jquery-plugin
Advance jquery-plugin
 
Java!!!!!Create a program that authenticates username and password.pdf
Java!!!!!Create a program that authenticates username and password.pdfJava!!!!!Create a program that authenticates username and password.pdf
Java!!!!!Create a program that authenticates username and password.pdf
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
 

More from fashionscollect

In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdfIn the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
fashionscollect
 
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdfIn 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
fashionscollect
 
how slavery build the united states economySolutionChattel sla.pdf
how slavery build the united states economySolutionChattel sla.pdfhow slavery build the united states economySolutionChattel sla.pdf
how slavery build the united states economySolutionChattel sla.pdf
fashionscollect
 
How did Woodrow Wilson impact public administrationSolutionWo.pdf
How did Woodrow Wilson impact public administrationSolutionWo.pdfHow did Woodrow Wilson impact public administrationSolutionWo.pdf
How did Woodrow Wilson impact public administrationSolutionWo.pdf
fashionscollect
 
Describe the three most well-known types of ethical decision making .pdf
Describe the three most well-known types of ethical decision making .pdfDescribe the three most well-known types of ethical decision making .pdf
Describe the three most well-known types of ethical decision making .pdf
fashionscollect
 
With regard to the SDLC and the Systems Engineering processThe SD.pdf
With regard to the SDLC and the Systems Engineering processThe SD.pdfWith regard to the SDLC and the Systems Engineering processThe SD.pdf
With regard to the SDLC and the Systems Engineering processThe SD.pdf
fashionscollect
 
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdfWeaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
fashionscollect
 

More from fashionscollect (20)

Interpersonal tactics, motivation, planning and leadership style can.pdf
Interpersonal tactics, motivation, planning and leadership style can.pdfInterpersonal tactics, motivation, planning and leadership style can.pdf
Interpersonal tactics, motivation, planning and leadership style can.pdf
 
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdfIn the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
 
In which sentence is the underlined group of words a prepositional ph.pdf
In which sentence is the underlined group of words a prepositional ph.pdfIn which sentence is the underlined group of words a prepositional ph.pdf
In which sentence is the underlined group of words a prepositional ph.pdf
 
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdfIn 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
 
Identify and explain the characteristics that distinguish government.pdf
Identify and explain the characteristics that distinguish government.pdfIdentify and explain the characteristics that distinguish government.pdf
Identify and explain the characteristics that distinguish government.pdf
 
how slavery build the united states economySolutionChattel sla.pdf
how slavery build the united states economySolutionChattel sla.pdfhow slavery build the united states economySolutionChattel sla.pdf
how slavery build the united states economySolutionChattel sla.pdf
 
How did Woodrow Wilson impact public administrationSolutionWo.pdf
How did Woodrow Wilson impact public administrationSolutionWo.pdfHow did Woodrow Wilson impact public administrationSolutionWo.pdf
How did Woodrow Wilson impact public administrationSolutionWo.pdf
 
Describe the three most well-known types of ethical decision making .pdf
Describe the three most well-known types of ethical decision making .pdfDescribe the three most well-known types of ethical decision making .pdf
Describe the three most well-known types of ethical decision making .pdf
 
Central banks in advanced economies have gradually moved away from m.pdf
Central banks in advanced economies have gradually moved away from m.pdfCentral banks in advanced economies have gradually moved away from m.pdf
Central banks in advanced economies have gradually moved away from m.pdf
 
Cell division in the apical meristems Cell division in the apical me.pdf
Cell division in the apical meristems  Cell division in the apical me.pdfCell division in the apical meristems  Cell division in the apical me.pdf
Cell division in the apical meristems Cell division in the apical me.pdf
 
Asteroids X, Y, and Z have equal mass of 4.0 kg each, They orbit aro.pdf
Asteroids X, Y, and Z have equal mass of 4.0 kg each, They orbit aro.pdfAsteroids X, Y, and Z have equal mass of 4.0 kg each, They orbit aro.pdf
Asteroids X, Y, and Z have equal mass of 4.0 kg each, They orbit aro.pdf
 
Biochemistry-Explain the differences in the ease of lateral movem.pdf
Biochemistry-Explain the differences in the ease of lateral movem.pdfBiochemistry-Explain the differences in the ease of lateral movem.pdf
Biochemistry-Explain the differences in the ease of lateral movem.pdf
 
4. Ammonia and calcium chlorate react to yield nitrogen gas, water, a.pdf
4. Ammonia and calcium chlorate react to yield nitrogen gas, water, a.pdf4. Ammonia and calcium chlorate react to yield nitrogen gas, water, a.pdf
4. Ammonia and calcium chlorate react to yield nitrogen gas, water, a.pdf
 
With regard to the SDLC and the Systems Engineering processThe SD.pdf
With regard to the SDLC and the Systems Engineering processThe SD.pdfWith regard to the SDLC and the Systems Engineering processThe SD.pdf
With regard to the SDLC and the Systems Engineering processThe SD.pdf
 
What is the conjugate acid of each of the following Brønsted–Lowry b.pdf
What is the conjugate acid of each of the following Brønsted–Lowry b.pdfWhat is the conjugate acid of each of the following Brønsted–Lowry b.pdf
What is the conjugate acid of each of the following Brønsted–Lowry b.pdf
 
What is the importance of phosphoryl-transfer potential and what fac.pdf
What is the importance of phosphoryl-transfer potential and what fac.pdfWhat is the importance of phosphoryl-transfer potential and what fac.pdf
What is the importance of phosphoryl-transfer potential and what fac.pdf
 
What is emphysema What effect does emphysema have on breathing Wha.pdf
What is emphysema What effect does emphysema have on breathing Wha.pdfWhat is emphysema What effect does emphysema have on breathing Wha.pdf
What is emphysema What effect does emphysema have on breathing Wha.pdf
 
What are cyclinsand How do cyclins oscillateSolutionCyclines.pdf
What are cyclinsand How do cyclins oscillateSolutionCyclines.pdfWhat are cyclinsand How do cyclins oscillateSolutionCyclines.pdf
What are cyclinsand How do cyclins oscillateSolutionCyclines.pdf
 
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdfWeaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
 
13. The auditors report on Other Financial Information (OFI) accomp.pdf
13. The auditors report on Other Financial Information (OFI) accomp.pdf13. The auditors report on Other Financial Information (OFI) accomp.pdf
13. The auditors report on Other Financial Information (OFI) accomp.pdf
 

Recently uploaded

Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 

This is Java, What I am creating is a multi lab pacman type game.T.pdf

  • 1. This is Java, What I am creating is a multi lab pacman type game. This one covers movement. I basically need my code below to meet the labs requiremnts. IE I need this game to allow two players to move. Also anymous person answering this with my same code stop it you have been reported for spam Thanks for the help included is some codes import javax.swing.JFrame; public class MainFrame { public static void main(String[] args) { // create the frame JFrame myFrame = new JFrame("Platformer"); // set up the close operation myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create panel MainPanel myPanel = new MainPanel(); // add panel myFrame.getContentPane().add(myPanel); // pack myFrame.pack(); // set visibility to true myFrame.setVisible(true); } } import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.ImageIcon; import javax.swing.JPanel; public class MainPanel extends JPanel implements KeyListener{ int x = 0; int y = 0; ImageIcon myIcon = new ImageIcon("./src/TreasureChest.png");
  • 2. public MainPanel() { setPreferredSize(new Dimension(1000,1000)); addKeyListener(this); setFocusable(true); } public void paintComponent(Graphics page) { super.paintComponent(page); page.drawImage(myIcon.getImage(), x, y, null); } @Override public void keyPressed(KeyEvent arg0) { int keyCode = arg0.getKeyCode(); if (keyCode == KeyEvent.VK_LEFT) { x -= 100; } else if (keyCode == KeyEvent.VK_RIGHT) { x += 100; } else if (keyCode == KeyEvent.VK_UP) { y -= 100; } else if (keyCode == KeyEvent.VK_DOWN) { y += 100; } repaint(); } @Override
  • 3. public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } This is the overall goal for the project for this part of the lab I basically just neeed to have two player movemet across the Jpanel so I believe I just need to use a consturer to make two instances of the player class that uses different movement Solution import javax.swing.JFrame; public class MainFrame { public static void main(String[] args) { // create the frame JFrame myFrame = new JFrame("Platformer"); // set up the close operation myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create panel MainPanel myPanel = new MainPanel(); // add panel myFrame.getContentPane().add(myPanel); // pack myFrame.pack(); // set visibility to true myFrame.setVisible(true); } } import java.awt.Dimension; import java.awt.Graphics;
  • 4. import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.ImageIcon; import javax.swing.JPanel; public class MainPanel extends JPanel implements KeyListener{ int x = 0; int y = 0; ImageIcon myIcon = new ImageIcon("./src/TreasureChest.png"); public MainPanel() { setPreferredSize(new Dimension(1000,1000)); addKeyListener(this); setFocusable(true); } public void paintComponent(Graphics page) { super.paintComponent(page); page.drawImage(myIcon.getImage(), x, y, null); } @Override public void keyPressed(KeyEvent arg0) { int keyCode = arg0.getKeyCode(); if (keyCode == KeyEvent.VK_LEFT) { x -= 100; } else if (keyCode == KeyEvent.VK_RIGHT) { x += 100; } else if (keyCode == KeyEvent.VK_UP) { y -= 100; }
  • 5. else if (keyCode == KeyEvent.VK_DOWN) { y += 100; } repaint(); } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub }