Question
In Java, I'm trying to write a Mario game, and I have I know the keyListener is working, bc I have debug output in the
In Java, I'm trying to write a Mario game, and I have I know the keyListener is working, bc I have debug output in the console, but only the LevelComponent() appears:
public class MarioLevel extends JFrame {
MarioComponent mc = new MarioComponent(); JLabel background; public MarioLevel(){ background = new LevelComponent(); background.setLayout(null); this.add(background,BorderLayout.CENTER); this.setSize(868,915); this.setFocusable(true); this.setVisible(true); KeyListener kl = new MoveListener(); this.addKeyListener(kl); background.add(mc); mc.setBounds(mc.marioSprite.x, mc.marioSprite.y, mc.marioSprite.sprite.getWidth(), mc.marioSprite.sprite.getHeight()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class MoveListener implements KeyListener{ public void keyPressed(KeyEvent k){ if((k.getKeyCode() == 39)){ mc.moveMario(); System.out.println(mc.marioSprite.getCoordinates()); } if(k.getKeyCode() == 83){ mc.jumpMario(); System.out.println(mc.marioSprite.getCoordinates()); } } public void keyReleased(KeyEvent k){ // if(k.getKeyCode = 83){ // mc.Mario // } } public void keyTyped(KeyEvent k){} }
public static void main(String[] args){ MarioLevel m = new MarioLevel(); }
}
my MarioComponent:
public class MarioComponent extends JComponent{
protected Mario marioSprite; public MarioComponent(){ marioSprite = new Mario(); } public void paintComponent(Graphics g){ //super.paintComponent(g); marioSprite.draw(g); } public void moveMario(){ marioSprite.move(); repaint(); } public void jumpMario(){ marioSprite.jump(); repaint(); } public void getCoordinates(){ System.out.println(marioSprite.getCoordinates()); } @Override public Dimension getPreferredSize() { return new Dimension(marioSprite.sprite.getWidth(), marioSprite.sprite.getHeight()); } }
my Mario class:
public class Mario{ //all numbers multiplied by 2 from OG game protected MarioState state; protected int x, y; protected BufferedImage sprite; public Mario(){ this.state = MarioState.SMALL; this.x = 54; this.y = 806; URL spriteAtLoc = getClass().getResource("sprites/Mario/SmallStandFaceRight.bmp");
try{ sprite = ImageIO.read(spriteAtLoc);
} catch(IOException e){ System.out.println("sprite not found"); e.printStackTrace(); } } public Mario(MarioState s, int x, int y){ this.state = s; this.x = x; this.y = y; } public void move(){ this.x+=2; } public void move(char c, int px){ if(c =='x'){ this.x += px; } if(c == 'y'){ this.y += px; } } public void jump(){ this.y -= 46; } public String getCoordinates(){ return "Mario coords: " + this.x + ", " + this.y + "."; } public void draw(Graphics g){
g.drawImage(sprite, this.x, this.y, null); } }
my LevelMap class:
public class LevelMap{
BufferedImage bg; public LevelMap(){ URL bgAtLoc = getClass().getResource("sprites/Mario/level.bmp"); try{ bg = ImageIO.read(bgAtLoc); } catch(IOException e){ System.out.println("invalid level!"); } } public void draw(Graphics g){ g.drawImage(bg, 0, 0, null); }
public static void main(String[] args){}
}
my LevelComponent class:
public class LevelComponent extends JLabel{ protected LevelMap levelSprite; public LevelComponent(){ levelSprite = new LevelMap(); } public void paintComponent(Graphics g){ levelSprite.draw(g); } @Override public Dimension getPreferredSize() { return new Dimension(levelSprite.bg.getWidth(), levelSprite.bg.getHeight()); }
}
Both MarioComponent and LevelComponent show up in their own JFrames. My format of adding a JLabel and then adding components worked in a previous project I did, but is not working here. What could this be?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started