0

I am trying to make a little mini game, where u can move around. I have written a code, which did work. But when I tried to shorten my code and make it looks nicer, I stumble upon a error I could not solve by myself.

Main:

    package eksamenstest;

import javax.swing.JFrame;

public class Eksamenstest extends JFrame {

    public Eksamenstest() throws InterruptedException
    {
     JFrame SOJ = new JFrame("Sword Of Justice");
     SOJ.pack(); 
     SOJ.setSize(1000,700);
     SOJ.setVisible(true);
     SOJ.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     SOJ.add(new Grafik());
     }

    public static void main(String[] args) throws InterruptedException 
    {
        new Eksamenstest();
        new Musik();
        new SpillerOne();
    }
}

Graphics:

package eksamenstest;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;

public class Grafik extends JPanel
{
    int Menu = 1;
    int Player1Liv = 3;
    int Player2Liv = 3;
    int DødP1 = 1;
    int DødP2 = 1;
    int WeaponON2;
    int WeaponON1;
    int Skjold;
    int Skjold1;
    int x = 100;
    int y = 360;
    int x_1 = 820;
    int y_1 = 360;

    public Grafik(){}    

     public void paint(Graphics g)
     {   
                                    //Grafikken af banen

        Image Bane2 = Toolkit.getDefaultToolkit().getImage("Bane2.png");
        g.drawImage(Bane2, 0, 0, 1000, 800, this);

        Image Plank = Toolkit.getDefaultToolkit().getImage("Plank.jpg");
        g.drawImage(Plank, 100, 500, 800, 10, this);

        Image lava = Toolkit.getDefaultToolkit().getImage("lava.png");
        g.drawImage(lava, 0, 520, 1000, 260, this);

    if(Menu == 1)
        {
        Image Menu2 = Toolkit.getDefaultToolkit().getImage("Menu.png");
        g.drawImage(Menu2, 100, -30, 900, 700, this);
        }

     //Player 1 - Grafikken der viser hvordan spilleren står. 

                        if(WeaponON1 == 0){
                        Image PlayerStå = Toolkit.getDefaultToolkit().getImage("Stå.png");
                        g.drawImage(PlayerStå, x, y, 80, 140, this);
                        }

                        if(WeaponON1 == 1){
                        Image PlayerSværdOP = Toolkit.getDefaultToolkit().getImage("OP.png");
                        g.drawImage(PlayerSværdOP, x, y, 80, 140, this);
                        }

                        if(WeaponON1 == 2){
                        Image PlayerSværdFrem = Toolkit.getDefaultToolkit().getImage("Frem.png");
                        g.drawImage(PlayerSværdFrem, x, y, 80, 140, this);
                        }
                        if(Skjold == 1){
                        Image Player1Skjold = Toolkit.getDefaultToolkit().getImage("Player1Skjold.png");
                        g.drawImage(Player1Skjold, x, y, 80, 140, this);
                        }
                        if(Player1Liv == 0){
                        Image DødP = Toolkit.getDefaultToolkit().getImage("DødP.png");
                        g.drawImage(DødP, x, y, 80, 140, this);
                        }
                        if(Player2Liv == 0){
                        Image Jubel = Toolkit.getDefaultToolkit().getImage("Jubel.png");
                        g.drawImage(Jubel, x, y, 80, 140, this);
                        }


                        //Player 2 - Grafikken der viser hvordan spilleren står. 

                        if(WeaponON2 == 0){
                        Image PlayerStå1 = Toolkit.getDefaultToolkit().getImage("Stå1.png");
                        g.drawImage(PlayerStå1, x_1, y_1, 80, 140, this);
                        }
                        if(WeaponON2 == 1){
                        Image PlayerSværdOP1 = Toolkit.getDefaultToolkit().getImage("OP1.png");
                        g.drawImage(PlayerSværdOP1, x_1, y_1, 80, 140, this);
                        }
                        if(WeaponON2 == 2){
                        Image PlayerSværdFrem1 = Toolkit.getDefaultToolkit().getImage("Frem1.png");
                        g.drawImage(PlayerSværdFrem1, x_1, y_1, 80, 140, this);
                        }
                        if(Skjold1 == 1){
                        Image Player2Skjold = Toolkit.getDefaultToolkit().getImage("Player2Skjold.png");
                        g.drawImage(Player2Skjold, x_1, y_1, 80, 140, this);
                        }
                        if(Player2Liv == 0){
                        Image DødPA = Toolkit.getDefaultToolkit().getImage("DødP.png");
                        g.drawImage(DødPA, x_1, y_1, 80, 140, this);
                        }
                        if(Player1Liv == 0){
                        Image Jubel = Toolkit.getDefaultToolkit().getImage("Jubel.png");
                        g.drawImage(Jubel, x_1, y_1, 80, 140, this);
                        }



                     //Health Bars / Stamina / Navne / Win

                             //Player 1

Image PlayerNavn = Toolkit.getDefaultToolkit().getImage("Player1Navn.png");
g.drawImage(PlayerNavn, 30, 50, 70, 30, this);
      if(Player1Liv == 3){
Image Liv100B = Toolkit.getDefaultToolkit().getImage("Liv100B.png");
g.drawImage(Liv100B, 30, 80, 120, 40, this);
    }
    if(Player1Liv == 2){
Image Liv75B = Toolkit.getDefaultToolkit().getImage("Liv75B.png");
g.drawImage(Liv75B, 30, 80, 120, 40, this);
    }
    if(Player1Liv == 1){
Image Liv50B = Toolkit.getDefaultToolkit().getImage("Liv25B.png");
g.drawImage(Liv50B, 30, 80, 120, 40, this);
    }
    if(Player1Liv == 0){
Image Liv0B = Toolkit.getDefaultToolkit().getImage("Liv0B.png");
g.drawImage(Liv0B, 30, 80, 120, 40, this);
DødP1 = 0;
Image Player2Win = Toolkit.getDefaultToolkit().getImage("Player2Wins.png");
g.drawImage(Player2Win, 350, 80, 350, 110, this);
    }

                             // Player 2

    Image PlayerNavn1 = Toolkit.getDefaultToolkit().getImage("Player2Navn.png");
g.drawImage(PlayerNavn1, 900, 50, 70, 30,this);
    if(Player2Liv == 3){
Image Liv100R = Toolkit.getDefaultToolkit().getImage("Liv100R.png");
g.drawImage(Liv100R, 850, 80, 120, 40,  this);
    }
    if(Player2Liv == 2){
Image Liv75R = Toolkit.getDefaultToolkit().getImage("Liv75R.png");
g.drawImage(Liv75R, 850, 80, 120, 40, this);
    }
    if(Player2Liv == 1){
Image Liv50R = Toolkit.getDefaultToolkit().getImage("Liv25R.png");
g.drawImage(Liv50R, 850, 80, 120, 40,  this);
    }
    if(Player2Liv == 0){
Image Liv0R = Toolkit.getDefaultToolkit().getImage("Liv0R.png");
g.drawImage(Liv0R, 850, 80, 120, 40,  this);
DødP2 = 0;
Image Player1Win = Toolkit.getDefaultToolkit().getImage("Player1Wins.png");
g.drawImage(Player1Win, 350, 80, 350, 110,  this);
    }
    }
}

Player Movement

    package eksamenstest;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;

    public class SpillerOne extends JFrame implements ActionListener, KeyListener {

        int x = 100;
        int y = 360;
        int xHøjre;
        int xVenstre;
        int DødP1;
        int Player1Liv;
        int iLava;
        int x_1;
        int Player2Liv;
        int Skjold1;
        int WeaponON1;
        int Menu = 1;
        int SværdTid;
        int Sværd;
        int Skjold;


       public SpillerOne()
       {
         addKeyListener(this);
         setFocusable(true);
         setFocusTraversalKeysEnabled(false);
       }


           @Override
        public void actionPerformed(ActionEvent e) 
        {
            PlayerOneMove();
            repaint();
        }

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
        int c = e.getKeyCode();   

                    //Menu
        if(c == KeyEvent.VK_O){
           Menu = 1;
           }
           if(c == KeyEvent.VK_P){
           Menu = 0;
           }


                // Player

                if(DødP1 == 1){

           if(c == KeyEvent.VK_A){
               xVenstre = 1;
               WeaponON1 = 0;
               Sværd = 0;
           }

           if(c == KeyEvent.VK_D){
               xHøjre = 1;
               WeaponON1 = 0;
           }

           if(WeaponON1 == 1){ 
               if(SværdTid < 3){
           if(c == KeyEvent.VK_W){
           WeaponON1 = 2;
           SværdTid++;
           }
               }
           }

           if(c == KeyEvent.VK_S){
           WeaponON1 = 1;
           Sværd = 1;
           }

           if(c == KeyEvent.VK_Q){
           Skjold = 1;
           SværdTid++;
           }
           }

           if(c == KeyEvent.VK_E){
           SværdTid--;
           WeaponON1 = 0;
           }

        }

        @Override
        public void keyReleased(KeyEvent e) 
        {
             int c = e.getKeyCode();

                    // Spiller 1

           if(c == KeyEvent.VK_A){
               xVenstre = 0;
           }

           if(c == KeyEvent.VK_D){
               xHøjre = 0;
           }

           if(c == KeyEvent.VK_Q){
           Skjold = 0;
           SværdTid = SværdTid;
           }

           if(c == KeyEvent.VK_W){
             if(Sværd == 1)
               WeaponON1 = 1;
           }


           if(c == KeyEvent.VK_E){
           SværdTid = SværdTid;
           WeaponON1 = 0;
           }
        }


       public void PlayerOneMove()
       {
             // Spiller et Bevægelse

        x = x + xHøjre; //Højre
        x = x - xVenstre; //Venstre



        if(WeaponON1 == 2){ //Tjekker om spilleren har våbnet fremme og hvis ja, så tjekker den om modspilleren har skjold på, hvis ikke, mister person 2 liv.
        if(Skjold1 == 0){ 
         if(x > x_1-80 && x < x_1 + 80){
        x = 100;
        x_1 = 820;
        Player2Liv--;
         }
        }

         if(WeaponON1 == 2){
        if(Skjold1 == 1){
         if(x > x_1-80 && x < x_1 + 80){
        x_1 = x_1 + 60;
         }
        }
    }

    }   



                        // Falder ned i lava

if(x < 100 && 100 > x + 80){ //Tjekker om player 1 er i lava ved venstre side.
DødP1 = 0;
Player1Liv = 0;
iLava = 1;
if(y < 500){
y++;
}
}

if(x > 900){ //Tjekker om player 1 er i lava ved højre side.
DødP1 = 0;
Player1Liv = 0;
iLava = 1;
if(y < 500){
y++;
}
}
   }
}

I have a feeling that this error is caused because I have not repainted somewhere.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • `I tried to shorten my code and make it looks nicer` actually would be nicer if it didn't have all those [magic numbers](https://stackoverflow.com/questions/3518938/what-are-magic-numbers-in-computer-programming). – Juan Carlos Mendoza Mar 20 '18 at 19:30
  • oh, yes sorry about that, I should have called it Player.Length or StartPositionX, so it would be easier to figure out, my bad. –  Mar 20 '18 at 19:35
  • 1
    *"I tried to shorten my code and make it looks nicer"* - Would have been nicer if you could have created [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – MadProgrammer Mar 20 '18 at 19:40
  • I will look into that, thanks for the tips on how to make my code look nicer. –  Mar 20 '18 at 19:57

1 Answers1

1

So, a list of issues...

First

There are two JFrames - which one is actually been used for what? As far as I can tell, SpillerOne is never displayed, so there is no possible way for it to be able to react to key events. Equally, your ActionListener doesn't seem to be attached to anything which can actually generate actions.

This also raises a bunch of questions about the relationship between SpillerOne and Grafik. They seem to be sharing variable names, but there is no way for them to be sharing state, so even if the KeyListener worked, Grafik would never reflect the changes in SpillerOnes state

Second

You've implemented your custom painting the wrong way. It's highly discouraged to override paint, instead, you should be overriding paintComponent AND calling super.paintComponent to ensure that requirements of the paint chain are upheld

See Performing Custom Painting and Painting in AWT and Swing for more details

You also seem to be putting a lot of state logic into your Grafik class, this is going to become exponentially more difficult to maintain as the complexity increases. Instead, each distinct operation should be it's own class, focused on performing as few dedicated operations as possible

Thirdly

KeyListener is a poor choice for monitoring for key events. While there are "hacks" that attempt to "solve" the focus related issues with KeyListener, none of them can achieve a reliable result.

If you do any research into KeyListener related issues, you will quickly find that the Key bindings API is often sighted as the most reliable solution to the problem

Overall

You code makes no sense. Why are there two frames? What is the ActionListener for and how is it attached to something that generates actions? Why doesn't the Grafik act as the key/action listener?

I think you have some significant thinking and redesigning to do

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Okay so... 1. In the graphics I extend JFrame for getting the graphics inside of my frame that I create in `Eksamenstest();`. In `private class SpillerOne` I extend JFrame, because I thought that, by doing that the ActionListener would react to the JFrame, and thereby make it work. (which it did not). And in the `Grafik` I only wanted to create the graphics in that method, and then calling the keylistener to move the graphic. I did write a similar code, that did work, but I wrote it all in the main with 570 lines, which is just more of a mess than this code. –  Mar 20 '18 at 19:54
  • and yes this "This also raises a bunch of questions about the relationship between SpillerOne and Grafik. They seem to be sharing variable names, but there is no way for them to be sharing state, so even if the KeyListener worked, Grafik would never reflect the changes in SpillerOnes state" is what I want to fix. How do I make the two classes share variables with each others? and thereby maybe fix it. –  Mar 20 '18 at 19:57
  • @Neahle Do some research into the "model-view-controller" paradigm. First, `KeyListener` (and the Key Bindings API) only work on components which are visible on the screen, so you're initial "key" handling MUST be done on through `Grafik` component - it could then trigger a update to some model which manages the overall state – MadProgrammer Mar 20 '18 at 21:00
  • Okay I will look into that. –  Mar 20 '18 at 21:10