2017-08-05 44 views
0

BIG編輯:我現在有一個MCVE此爲什麼當我嘗試改變它時,我的馬里奧克隆遊戲精靈會被切斷?

在我的超級馬里奧3克隆,我的精靈繪製在正確實例化的JFrame,但是當我按我設置跳轉按鈕中的一個,它部分地得到隔斷。屏幕上的所有內容都是JLabel。

下面是代碼:

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

} 

個精靈可以發現herehere壽下載是爲.jpg,而在我的代碼,他們.BMP,但你可以下載,在另一個應用程序打開,保存爲bmp,或更改代碼

what happens

+3

1)爲了更好地提供幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 2)獲取圖像的一種方法是通過[本問答](http://stackoverflow.com/q/19209650/418556)中的圖像進行熱鏈接。 –

+0

昨天,我嘗試了,但我被告知我的帖子是TL; DR – Derry

+1

人們需要幫助我做些什麼?這是一個複雜的程序。我是新來的編程,所以我真的不喜歡被人討論。 – Derry

回答

3

這很可能是JLabel對象水平和垂直約束的結果。從圖片的外觀來看,跳躍的馬里奧水平比馬里奧站在地面上稍寬。

+0

它是;在開始的默認值爲24 x 30,跳躍爲32 x 32 – Derry

+0

將其放置在paintComponent中:setBounds(marioSprite.x,marioSprite.y,marioSprite.sprite.getWidth(),marioSprite.sprite.getHeight() );做到了! – Derry

相關問題