How do I get my character to move after adding to JFrame?
Posted
by
A.K.
on Game Development
See other posts from Game Development
or by A.K.
Published on 2012-06-07T00:28:44Z
Indexed on
2012/06/07
4:48 UTC
Read the original article
Hit count: 334
So this is kind of a follow up on my other JPanel question that got resolved by playing around with the Layout...
Now my MouseListener allows me to add a new Board(); object from its class, which is the actual game map and animator itself. But since my Board() takes Key Events from a Player Object inside the Board Class, I'm not sure if they are being started.
Here's my Frame Class, where SideScroller S is the player object:
package OurPackage;
//Made By A.K. 5/24/12
//Contains Frame.
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;
public class Frame implements MouseListener
{
public static boolean StartGame = false;
JFrame frm = new JFrame("Action-Packed Jack");
ImageIcon img = new ImageIcon(getClass().getResource("/Images/ActionJackTitle.png"));
ImageIcon StartImg = new ImageIcon(getClass().getResource("/Images/JackStart.png"));
public Image Title;
JLabel TitleL = new JLabel(img);
public JPanel TitlePane = new JPanel();
public JPanel BoardPane = new JPanel();
JPanel cards;
JButton StartB = new JButton(StartImg);
Board nBoard = new Board();
static Sound nSound;
public Frame()
{
frm.setLayout(new GridBagLayout());
cards = new JPanel(new CardLayout());
nSound = new Sound("/Sounds/BunchaJazz.wav");
TitleL.setPreferredSize(new Dimension(970, 420));
frm.add(TitleL);
frm.add(cards);
cards.setSize(new Dimension(150, 45));
cards.setLayout(new GridBagLayout ());
cards.add(StartB);
StartB.addMouseListener(this);
StartB.setPreferredSize(new Dimension(150, 45));
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(1200, 420);
frm.setVisible(true);
frm.setResizable(false);
frm.setLocationRelativeTo(null);
frm.pack();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Frame();
}
});
}
public void mouseClicked(MouseEvent e)
{
nSound.play();
StartB.setContentAreaFilled(false);
cards.remove(StartB);
frm.remove(TitleL);
frm.remove(cards);
frm.setLayout(new GridLayout(1, 1));
frm.add(nBoard); //Add Game "Tiles" Or Content. x = 1200
nBoard.setPreferredSize(new Dimension(1200, 420));
cards.revalidate();
frm.validate();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
© Game Development or respective owner