This is Java, What I am creating is a multi lab pacman type game. This one cover
ID: 3790635 • Letter: T
Question
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
Explanation / Answer
import java.awt.Color; import java.awt.Dimension; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.JLabel; import java.awt.Font; import javax.swing.SwingConstants; public class InputDialogPanel { public static void main(String[] args) { JPanel panel = new JPanel(); panel.setBackground(new Color(0, 0, 0)); panel.setSize(new Dimension(250, 32)); panel.setLayout(null); JLabel label = new JLabel("It's your choice! :)"); label.setForeground(new Color(255, 255, 0)); label.setHorizontalAlignment(SwingConstants.CENTER); label.setFont(new Font("Arial", Font.BOLD, 11)); label.setBounds(0, 0, 250, 32); panel.add(label); UIManager.put("OptionPane.minimumSize",new Dimension(270, 120)); Object[] options = {2, "No", 5.6, true}; Object l = JOptionPane.showInputDialog(null, panel, "Mix & Match", JOptionPane.PLAIN_MESSAGE, null, options, options[3]); if(l instanceof Integer){ System.out.println("You picked an Integer!"); }else if(l instanceof String){ System.out.println("You picked a String!"); }else if(l instanceof Double){ System.out.println("You picked a Double!"); }else if(l instanceof Boolean){ System.out.println("You picked a Boolean!"); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.