Why Doesn't My Mario Sprite (a JComponent) Sho

2019-08-19 12:21发布

This question already has an answer here:

I'm trying to write a Mario 3 level 1 clone, and previously, I was able to get it to show up in JFrames when I had the pulling of the sprite in the constructor of Mario itself. However, because I realized that I'll need to link things like the coordinates and state (small, large) to the moving image itself, I decided to tear it up and have a Mario and MarioComponent. I have a MarioComponent class but it doesn't show up.

public class MarioComponent extends JComponent{


  private Mario m;


  public MarioComponent(){
    m = new Mario();
  }

  public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g2);
    m.draw();


  }

    public void moveMario(){
    m.move();
    repaint();
  }


  public static void main(String[] args){
    JFrame f = new JFrame();
    f.setSize(868,915);
    MarioComponent m = new MarioComponent();
    m.setLocation(100,100);
    f.add(m);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  }


}

my Mario class:

public class Mario{
//all numbers multiplied by 2 from OG game
  protected MarioState state;
  protected int x, y;
  BufferedImage sprite;

  public Mario(){
    this.state = MarioState.SMALL;
    this.x = 54;
    this.y = 806;
  }

  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 jump(){
    this.y -= 46;

  }

  public String getCoordinates(){
    return "Mario coords: " + this.x + ", " + this.y + ".";
  }

  public void draw(){
    URL spriteAtLoc = getClass().getResource("sprites/Mario/SmallStandFaceRight.bmp");

    try{
      sprite = ImageIO.read(spriteAtLoc);

    } catch(IOException e){
      System.out.println("sprite not found");
      e.printStackTrace();
    }
  }
}

When I try to place a MarioComponent on a JFrame, its not there. How can I remedy this?

标签: java swing
1条回答
Anthone
2楼-- · 2019-08-19 12:32

You need to use the Graphics object, g (or g2 -- they're the same object) within your paintComponent as the "pen" with which to draw. You don't do this, so there's no way that anything could or should render. Graphics has a method, g.drawImage(...) that accepts an image as one of its parameters and would be the best way for this to work.

Suggestions:

  • Change draw so that it accepts a Graphics parameter: public void draw(Graphics g){
  • Within paintComponent, pass the Graphics parameter given to the method by the JVM into your draw call: m.draw(g);
  • Don't keep re-reading in the image within the draw method. This is wasteful and will only cause lag. Read it in once and store it into a variable.
  • All the code within the current draw method shouldn't be there. Again, the image should be read in once, likely within the constructor, and draw should instead actually draw the image, not read the file.
  • Don't guess at this, but rather read the appropriate tutorials
查看更多
登录 后发表回答