图像显示问题Java

时间:2012-10-27 18:16:41

标签: java

我目前正在努力用java制作我的第一个游戏,我已成功制作了一个侧滚动游戏,直到我想要将地图(背景)更大一点。我开始遇到背景图片的奇怪问题 它的分辨率高达5250px x 5148px,效果很好。 如果我超过这个,图像就无法显示。

我尝试过减小文件大小,但无论文件大小如何,高于此分辨率的图像仍然无法显示。

我的游戏包含三个文件
Frame,Dude和Board,它们如下所示

Frame.java

package OurGame;

import javax.swing.*;

public class Frame {

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Game");

        frame.add(new Board());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

Dude.java

package OurGame;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Dude {
    int x, dx, y, dy, nx2, ny2;
    Image still;

    public Dude()
    {
        ImageIcon i = new ImageIcon("/Users/Connor/Desktop/square.jpg");
        still = i.getImage();
        x = 250;
        y = 250;    
        nx2 = 485;
        ny2 = 485;
    }

    public void move()
    {
        x = x + dx;
        y = y + dy;
        nx2 = nx2 + dx;
        ny2 = ny2 + dy;
    }

    public int getX()
    {
        return x;
    }
    public int getY()
    {
        return y;
    }
    public Image getImage()
    {
        return still;
    }

    public void keyPressed(KeyEvent e)
    {
        int key = e.getKeyCode();

        if(key == KeyEvent.VK_A)
        {
            dx = -1;
        }

        if(key == KeyEvent.VK_D)
        {
            dx = 1;
        }
        if(key == KeyEvent.VK_W)
        {
            dy = -1;
        }

        if(key == KeyEvent.VK_S)
        {
            dy = 1;
        }

    }

    public void keyReleased(KeyEvent e)
    {
        int key = e.getKeyCode();

        if(key == KeyEvent.VK_A)
        {
            dx = 0;
        }

        if(key == KeyEvent.VK_D)
        {
            dx = 0;
        }
        if(key == KeyEvent.VK_W)
        {
            dy = 0;
        }

        if(key == KeyEvent.VK_S)
        {
            dy = 0;
        }

    }

}

Board.java

package OurGame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener{
    private static final long serialVersionUID = 1L;
Dude p;
Image img;
Timer time;

    public Board()
    {
        p = new Dude();
        addKeyListener(new AL());
        setFocusable(true);
        ImageIcon i = new ImageIcon("/Users/Connor/Desktop/background.jpg");
        img = i.getImage();
        time = new Timer(5, this);
        time.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        p.move();
        repaint();
    }

    public void paint(Graphics g)
    {
        super.paint(g);
            Graphics2D g2d = (Graphics2D) g;

            g2d.drawImage(img, 485-p.nx2, 485-p.ny2, null);
            g2d.drawImage(p.getImage(), 200, 200, null);
    }

    private class AL extends KeyAdapter
    {
        public void keyReleased(KeyEvent e)
        {
            p.keyReleased(e);
        }
        public void keyPressed(KeyEvent e)
        {
            p.keyPressed(e);
        }
    }

}

0 个答案:

没有答案