为什么我的 Mario Sprite(一个 JComponent)不出现? [英] Why Doesn't My Mario Sprite (a JComponent) Show Up?

查看:29
本文介绍了为什么我的 Mario Sprite(一个 JComponent)不出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 Mario 3 level 1 克隆,以前,当我在 Mario 本身的构造函数中拉动精灵时,我能够让它显示在 JFrames 中.然而,因为我意识到我需要将坐标和状态(小、大)等内容与运动图像本身联系起来,所以我决定将其拆解并拥有一个 Mario 和 MarioComponent.我有一个 MarioComponent 类,但它没有出现.

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);
  }


}

我的马里奥课:

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();
    }
  }
}

当我尝试在 JFrame 上放置 MarioComponent 时,它不在那里.我该如何补救?

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

推荐答案

您需要使用 Graphics 对象,g(或 g2 -- 它们是相同的object) 在您的paintComponent 中作为用于绘制的笔".你不这样做,所以没有办法可以或应该呈现任何东西.Graphics 有一个方法,g.drawImage(...),它接受一个图像作为它的参数之一,这将是它工作的最佳方式.

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.

建议:

  • 更改 draw 以使其接受 Graphics 参数:public void draw(Graphics g){
  • 在paintComponent 中,将JVM 提供给方法的Graphics 参数传递到您的绘图调用中:m.draw(g);
  • 不要在 draw 方法中不断重新读取图像.这是一种浪费,只会导致滞后.一次读取它并将其存储到一个变量中.
  • 当前绘制方法中的所有代码都不应该存在.同样,图像应该被读取一次,可能是在构造函数中,而 draw 应该实际绘制图像,而不是读取文件.
  • 不要猜测,而是阅读适当的教程
    • 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
      • Lesson: Performing Custom Painting: introductory tutorial to Swing graphics
      • Painting in AWT and Swing: advanced tutorial on Swing graphics

      这篇关于为什么我的 Mario Sprite(一个 JComponent)不出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆