Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Unusual interaction between keyListener and JMenu on: 2005-12-01 00:48:43
Adding a view.grabFocus() after loading the game board fixed the problem.  Thanks for all the possible solutions!
2  Game Development / Newbie & Debugging Questions / Re: Unusual interaction between keyListener and JMenu on: 2005-11-26 07:15:32

1  
2  
3  
4  
5  
  //Display the window
frame.pack();
frame.setVisible(true);

view .requestFocus();




Thanks!  I tried this, but it didn't fix it.  Any other suggestions?
3  Game Development / Newbie & Debugging Questions / Unusual interaction between keyListener and JMenu on: 2005-11-26 06:49:50
I'm working on a simple sokoban game.  I am using a JMenu which has 3 options: Reset board, Load board, and quit.  Once the board is loaded the user should be able to use the keyboard to move around the game board.  However, after the game is loaded, the keys do not work until I click the "Reset board" option on the menu (which is currently unimplemented and does nothing except a print statement).  I can't figure out what's causing this strange behavior.  Portions of the code are below.  The Board class, which I've not included, implements the character movement functions. 

Just to reiterate, if I "Load board" -> "Reset board", the keys work fine, but I shouldn't have to "Reset board" to get the keys to work.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
 
public class Sokoban extends JFrame implements ActionListener {

Board b;
boolean boardLoaded = false;


   public class View extends JComponent {

      int xVal, yVal;
      ImageIcon manIcon;
      ImageIcon ballIcon;
      Image man, ball;
      int cellHeight, cellWidth;
      int initialW = 500;
      int initialH = 300;
      Color floorColor;
     
      public View()  {
         // ImageIcon code snipped
 
         
         // Set the mouse listener
        addMouseListener(new MouseAdapter() {
            public void mouseClicked (MouseEvent event) {
               xVal = event.getX();
               yVal = event.getY();
            }
         });

         // Set the key listener
        addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
               if(boardLoaded) {
                  char c = e.getKeyChar();
                  if(c == 'h') b.moveLeft();
                  if(c == 'l') b.moveRight();
                  if(c == 'j') b.moveDown();
                  if(c == 'k') b.moveUp();
               }
            }
            public void keyPressed(KeyEvent e) {
               if(boardLoaded) {
                  int code = e.getKeyCode();
                  if(code == KeyEvent.VK_LEFT) b.moveLeft();
                  if(code == KeyEvent.VK_RIGHT) b.moveRight();
                  if(code == KeyEvent.VK_UP) b.moveUp();
                  if(code == KeyEvent.VK_DOWN) b.moveDown();
               }
            }
            public void keyReleased(KeyEvent e) {}
         });
      }

      public void paintComponent(Graphics g) {
         Graphics2D g2 = (Graphics2D) g;
         if(!boardLoaded) {
            //Draw Splash Screen
        }
         else {
            // Code to draw board snipped
                                                                }
      }

      public Dimension getPreferredSize() {
         return new Dimension(initialW, initialH);
      }

   }

public static void main(String[] args)  {
      javax.swing.SwingUtilities.invokeLater(new Runnable()   {
         public void run()  {
            Sokoban s = new Sokoban();
         }
      });
}

////////// Constructor
public Sokoban() {
   showGUI();
}

////////// Sets up JFrame
private void showGUI() {

      // Set up frame
     JFrame.setDefaultLookAndFeelDecorated(true);
      JFrame frame = new JFrame("Sokoban");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create content
     Container content = frame.getContentPane();
                                           BorderLayout layout = new BorderLayout();
      content.setLayout(layout);  

      // Set up board
     View view = new View();
      view.setFocusable(true);
      content.add(view, BorderLayout.CENTER);

      // Set up menu
     JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Actions");
      menuBar.add(menu);
      JMenuItem reset = new JMenuItem("Reset game");
      JMenuItem load = new JMenuItem("Load board");
      JMenuItem quit = new JMenuItem("Quit");
      load.addActionListener(this);
      load.setActionCommand("load");
      reset.addActionListener(this);
      reset.setActionCommand("reset");
      quit.addActionListener(this);
      quit.setActionCommand("quit");
      menu.add(reset);
      menu.add(load);
      menu.add(quit);
      frame.setJMenuBar(menuBar);

      //Display the window
     frame.pack();
      frame.setVisible(true);

   }

   //// Actions for menu selections
  public void actionPerformed(ActionEvent e) {
      if(e.getActionCommand().equals("load")) {
         LinkedList<String> boardList = new LinkedList<String>();
         int rows = 0;
         int cols = 0;

         FileDialog fd = new FileDialog(this, "Choose level file");
         fd.setVisible(true);
         FileInputStream fin;
         String line;
         try {
            fin = new FileInputStream(fd.getDirectory()+fd.getFile());
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));
            while((line = br.readLine()) != null) {
               //System.out.println(line);
              boardList.add(line);
               if(line.length() > cols) cols = line.length();
            }
            fin.close();
         }
         catch (IOException io) {
            System.err.println("Unable to read from file");
         }
         rows = boardList.size();

         b = new Board(rows, cols);
         b.getBoard(boardList);
         boardLoaded = true;
      }
      if(e.getActionCommand().equals("quit")) {
         System.exit(0);
      }
      if(e.getActionCommand().equals("reset")) {
         System.out.println("not implemented yet");
      }
   }
}
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (17 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!