0

I have 4 different classes of Code, but I had a problem with the main game part. Basically, as a cube you move around to get to a specific area, but for some reason using keybound labels it allows the label to leave the frame and basically disappear. Here's the code for the NewWindow class which essentially contains the window and key.

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Desktop.Action;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.AbstractAction;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.border.Border;

public class NewWindow implements ActionListener{
    JFrame frame;
    JLabel label; 
    JButton quit = new JButton();

    NewWindow.UpAction upAction;
    NewWindow.LeftAction leftAction;
    NewWindow.DownAction downAction;
    NewWindow.RightAction rightAction;

    NewWindow(){
        JPanel level1 = new JPanel();
        level1.setBackground(Color.CYAN);
        level1.setBounds(810,410,75,75);

         JPanel start = new JPanel();
        start.setBackground(Color.CYAN);
        start.setBounds(0,410,75,75);

        Border border1 = BorderFactory.createLineBorder(Color.RED);

        frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.setSize(900,900);
    frame.setLayout(null);
    frame.getContentPane().setBackground(Color.BLACK); //c
    frame.add(quit);
    frame.setResizable(false);

    label = new JLabel();
    label.setBackground(Color.RED);
    label.setBounds(0, 450, 25, 25);
    label.setOpaque(true);

    upAction = new UpAction();
    downAction = new DownAction();
    leftAction = new LeftAction();
    rightAction = new RightAction();

    label.getInputMap().put(KeyStroke.getKeyStroke('w'), "upAction");
    label.getActionMap().put("upAction", upAction);

    label.getInputMap().put(KeyStroke.getKeyStroke('s'), "downAction");
    label.getActionMap().put("downAction", downAction);

    label.getInputMap().put(KeyStroke.getKeyStroke('a'), "leftAction");
    label.getActionMap().put("leftAction", leftAction);

    label.getInputMap().put(KeyStroke.getKeyStroke('d'), "rightAction");
    label.getActionMap().put("rightAction", rightAction);

    quit.setBounds(0,0,100,40);
    quit.setFocusable(false);
    quit.setText("Quit");
    quit.setFont(new Font("Comic Sans",Font.BOLD,25));
    quit.setForeground(Color.RED);
    quit.addActionListener(this);
    quit.setBorder(border1);
    quit.setBackground(Color.black);

    ImageIcon image1 = new ImageIcon("3dCube.png");
    frame.setVisible(true);
    frame.add(label);
    frame.add(start);
    frame.setIconImage(image1.getImage());
    frame.add(level1);
}

public class UpAction extends AbstractAction{
    @Override
    public void actionPerformed(ActionEvent e) {
        label.setLocation(label.getX(), label.getY()-10);
    }
}

public class LeftAction extends AbstractAction{
    @Override
    public void actionPerformed(ActionEvent e) {
        label.setLocation(label.getX()-10, label.getY());
    }
 }

 public class DownAction extends AbstractAction{
    @Override
    public void actionPerformed(ActionEvent e) {
        label.setLocation(label.getX(), label.getY()+10);
    }
 }

 public class RightAction extends AbstractAction{
    @Override
    public void actionPerformed(ActionEvent e) {
        label.setLocation(label.getX()+10, label.getY());
    }
    }
@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==quit) {
        frame.dispose();
    Launch myWindow = new Launch();
    }
}
}

    
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Where do you do your bounds checking? No where are you checking to see if the position of the label is outside it's container's viewable area – MadProgrammer Aug 04 '21 at 23:02
  • See: [Motion Using the Keyboard](https://tips4java.wordpress.com/2013/06/09/motion-using-the-keyboard/). the `MotionWithKeyBindings` shows one way to do bounds checking so you can't move the label outside the bounds of the parent. – camickr Aug 05 '21 at 00:06
  • 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have a keyboard shortcut specifically for formatting code. 3) `NewWindow` Give attributes, classes and methods meaningful, descriptive names! – Andrew Thompson Aug 05 '21 at 02:09
  • What is the meaning of this line of code? `Launch myWindow = new Launch();` What is a `Launch` & why is it being set visible (presumably) when this frame is closed? – Andrew Thompson Aug 05 '21 at 02:19
  • Hey. Thanks for everyone responding. To answer your question Andrew, I'm attempting to make a really simple game, so as I had no idea how to stop the label from moving outside the main frame I made a title screen which can take you to where the game is and in turn a "quit" button that'll take you back to said main screen for fun (Or to practice using JButtons and such). When you click the quit button it'll get rid of the game frame, but in turn go back to the main screen. And thanks for your first comment as well. I'm quite new to code, so I just kind of went along with the spaces as I went. – SteelBlue Aug 06 '21 at 16:14
  • **General tips:** 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). I started trying to help, buut gave up as soon as I realized the `Launch` class was missing. 2) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). It will help flip between the different screens (which each should be coded in a `JPanel`). 3) I would not approach this using components added to the game panel, but as entirely custom painted. .. – Andrew Thompson Aug 06 '21 at 16:34
  • 4) Tip: Add @MadProgrammer (or whoever, the `@` is important) to *notify* the person of a new comment. It was only luck I came back to this. 5) I expect to see all suggestions fixed before I'll devote more time to this. – Andrew Thompson Aug 06 '21 at 16:35

0 Answers0