0

BIG EDIT: I have now a MCVE for this

In my Super Mario 3 clone, my sprite draws correctly upon instantiating the JFrame, but when I press one of the buttons I've set to jump, it gets partially cut off. Everything on the screen is a JLabel.

Here is the code:

//for all
import java.nio.file.*;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.awt.image.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.invoke.MethodHandles.*;
import java.awt.event.*;

//my Mario class (cut down a lot)
class Mario {
    // all numbers multiplied by 2 from OG game

    protected Direction dir;
    protected int x, y;
    protected BufferedImage sprite;

    public Mario() {

        this.x = 54;
        this.y = 808;
        dir = Direction.RIGHT;
        setSprite(MVCE.SMALLSTANDFACERIGHT);

    }

    public void moveRight() {
        if (this.dir == Direction.LEFT) {
            this.dir = Direction.RIGHT;
        } else if (this.dir == Direction.RIGHT) {
            this.x += 2;
        }
    }

    public void jump() {
        this.y -= 46;
    }

    public void setSprite(String spriteName) {
        URL spriteAtLoc = MVCE.urlGenerator(spriteName);
        this.sprite = MVCE.generateAndFilter(sprite, spriteAtLoc);
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(sprite, 0, 0, null); // DO NOT SET x and y TO ANYTHING,
                                          // this sets 0,0 to top left!!
    }

}

// my MarioRender class:
class MarioRender extends JLabel {

    protected Mario marioSprite;

    public MarioRender() {
        marioSprite = new Mario();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        marioSprite.paint(g2);
    }

    public void moveMarioRight() {
        marioSprite.moveRight();
        setLocation(this.marioSprite.x, this.marioSprite.y);
        repaint();
    }

    public void jumpMario() {
        marioSprite.jump();
        setLocation(this.marioSprite.x, this.marioSprite.y);
        repaint();

    }

}

// direction class, solely for moving
enum Direction {
    LEFT, RIGHT
}

// my calling class, which I called MVCE where I make the frame
public class MVCE extends JFrame {

    MarioRender m = new MarioRender();
    JLabel bg;

    public MVCE() {
        bg = new JLabel();
        this.setSize(868, 915);
        this.setVisible(true);
        this.add(bg, BorderLayout.CENTER);
        bg.setLayout(null);

        bg.add(m);
        m.setBounds(m.marioSprite.x, m.marioSprite.y, m.marioSprite.sprite.getWidth(),
                m.marioSprite.sprite.getHeight());
        KeyListener kl = new MoveListener();
        this.addKeyListener(kl);
        this.setFocusable(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static final String SMALLSTANDFACERIGHT = "SmallStandFaceRight.bmp"; // 30
                                                                                // x
                                                                                // 32
    public static final String SMALLJUMPFACERIGHT = "SmallJumpFaceRight.bmp"; // 32
                                                                              // x
                                                                              // 32

    // generate URL
    public static URL urlGenerator(String name) {
        URL u = lookup().lookupClass().getResource(name);
        return u;
    }

    // return image with filtered color
    public static BufferedImage generateAndFilter(BufferedImage b, URL u) {

        try {
            b = ImageIO.read(u);
            int width = b.getWidth();
            int height = b.getHeight();
            int[] pixels = new int[width * height];
            b.getRGB(0, 0, width, height, pixels, 0, width);
            for (int i = 0; i < pixels.length; i++) {
                // System.out.println(pixels[i]);
                if (pixels[i] == 0xFFff00fe) {
                    pixels[i] = 0x00ff00fe;
                }
            }
            BufferedImage newSprite = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            newSprite.setRGB(0, 0, width, height, pixels, 0, width);
            b = newSprite;
        } catch (IOException e) {
            System.out.println("sprite not found");
            e.printStackTrace();
        }
        return b;
    }

    // key listener
    class MoveListener implements KeyListener {
        public void keyPressed(KeyEvent k) {

            if ((k.getKeyCode() == 39)) {
                m.marioSprite.setSprite(SMALLSTANDFACERIGHT);
                m.moveMarioRight();

            }

            if (k.getKeyCode() == 83) { // S key

                m.marioSprite.setSprite(SMALLJUMPFACERIGHT);
                m.jumpMario();
            }

        }

        public void keyReleased(KeyEvent k) {
        }

        public void keyTyped(KeyEvent k) {
        }
    }

    public static void main(String[] args) {
        MVCE m = new MVCE();
    }

}

sprites can be found here and here tho the downloads are in .jpg, whereas in my code, they're .bmp but you can just download, open in another app, save as bmp, or change the code

what happens

Derry
  • 371
  • 1
  • 13
  • 3
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Aug 05 '17 at 20:16
  • yesterday, I tried but I got told my post was TL;DR – Derry Aug 05 '17 at 20:16
  • 1
    what precisely do people need to help me with this? It is kind of a complex program. I'm new to coding so I really don't like being talked down to. – Derry Aug 05 '17 at 20:19
  • I'm previous posts, when I had the Mario sprite as a JComponent, I got told that it should be a JLabel. Is there some other specific Java class I should use? – Derry Aug 05 '17 at 20:23
  • what would be useful to show so that I could get a solution? my paintComponent method? – Derry Aug 05 '17 at 20:30
  • 1
    What would probably help most is that you create and post a MCVE as apparently has been asked 2 days in a row. – DontKnowMuchBut Getting Better Aug 05 '17 at 20:37
  • what consisely and succinctly does that mean?? In a format that isn't a very long page! Or please tell me what can I add that would make this post satisfy that? – Derry Aug 05 '17 at 20:43
  • It's a self-contained small program, small enough to fit in its entirety here, that demonstrates your problem. [For example](https://stackoverflow.com/a/6887354/522444) from one of my past answers -- or any of the other answers to that question. – Hovercraft Full Of Eels Aug 05 '17 at 20:53
  • Or [here's another mcve taken from one of my prior answers](https://stackoverflow.com/a/6887354/522444), this one involving moving a sprite. Again, the program is fully contained, again, it may have more than one class, but it all can fit in one file. – Hovercraft Full Of Eels Aug 05 '17 at 21:00
  • OK now the only code in this question is code that if copied, will do what I'm asking about, no bells and whistles, only necessary methods. – Derry Aug 05 '17 at 21:45
  • should be good now edit: THANK YOU – Derry Aug 05 '17 at 21:50
  • Again, voting to re-open and have up-voted, but note that the images are not available to us. Any possibility of using publicly available (online) images? Again thanks for your updates! – Hovercraft Full Of Eels Aug 05 '17 at 21:56
  • 2
    @AndrewThompson: please note improvements to question – Hovercraft Full Of Eels Aug 05 '17 at 21:56
  • https://farm5.staticflickr.com/4412/35556920484_bdbecdc96a_o_d.jpg and https://farm5.staticflickr.com/4431/35556920434_09cb569409_o_d.jpg but those are jpgs, I have bmps, so you could open them in another app and save as bmp. Also, can you get rid of "[on hold]" in the title? – Derry Aug 05 '17 at 22:02
  • We can't directly get rid of the hold, but rather enough need to vote to reopen the question – Hovercraft Full Of Eels Aug 06 '17 at 00:30
  • We still can't answer your question, but again, you should not draw your sprite within a JLabel. Please see the edit that I created to [this answer](https://stackoverflow.com/a/6887354/522444). It shows two classes and an enum, and uses a sprite sheet available online, and shows swapping images while listening to the key press, and **not** using a JLabel. – Hovercraft Full Of Eels Aug 06 '17 at 04:10
  • I finally fixed it! in the paintComponent, I tried resetting the bounds and that did the trick: setBounds(marioSprite.x, marioSprite.y, marioSprite.sprite.getWidth(), marioSprite.sprite.getHeight()); – Derry Aug 06 '17 at 20:40

1 Answers1

3

This is most likely a result of the horizontal and vertical constraints on your JLabel objects. From the look of the picture, the Mario that is jumping is slightly wider horizontally than the Mario standing on the ground.

deko
  • 337
  • 3
  • 12
  • it is; the default at the start, as small, is 24 x 30, jumping is 32 x 32 – Derry Aug 05 '17 at 20:29
  • putting this in paintComponent: setBounds(marioSprite.x, marioSprite.y, marioSprite.sprite.getWidth(), marioSprite.sprite.getHeight()); did it! – Derry Aug 06 '17 at 20:41