Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi my question pretains to Java programing, What I am creating is a multi lab pa

ID: 3790904 • Letter: H

Question

Hi my question pretains to Java programing, What I am creating is a multi lab pacman type game.

This part 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 and i think use a construtor to pass the movement through the player classe.

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 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.*;

import javax.swing.*;

class DebugGraphicsTest extends JComponent

{

    // Paints a thick line from top left to bottom right.

    public void paintComponent(Graphics g)

    {

     Graphics2D g2d = (Graphics2D)g;

     // This always prints SunGraphics2D instead of DebugGraphics!

     System.out.println("DebugGraphicsTest.paintComponent(" +

                  g.getClass().getName() + ")");

     g2d.setStroke(new BasicStroke(5));

     // And since g2d is not a DebugGraphics, this line does not flash. :-(

     // (and by the way, using an ordinary Graphics does not help)

     g2d.drawLine(0, 0, getWidth(), getHeight());

    }

    public static void main(String[] args)

    {

     DebugGraphicsTest test = new DebugGraphicsTest();

     RepaintManager.currentManager(test).setDoubleBufferingEnabled(false);

     test.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);

     test.setPreferredSize(new Dimension(200, 200));

     System.out.println("test graphics: " +

                  test.getGraphics().getClass().getName()); // OK

     JButton button = new JButton()

         {

          public void paintComponent(Graphics g)

          {

              // This work just fine, prints DebugGraphics.

              System.out.println("JButton.paintComponent(" +

                           g.getClass().getName() + ")");

              super.paintComponent(g);

          }

         };

     button.setText("button");

     // If these two lines are removed, button.paintComponent() also

     // is handed a SunGraphics2D of course.

     RepaintManager.currentManager(button).setDoubleBufferingEnabled(false);

     button.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);

     JFrame frame = new JFrame("DebugGraphics test");

     frame.getContentPane().add(test, BorderLayout.CENTER);

     frame.getContentPane().add(button, BorderLayout.SOUTH);

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     frame.pack();

     frame.setVisible(true);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote