绘制时JPanel上的Java不需要的边框

时间:2014-12-30 13:15:54

标签: java graphics draw

我正在尝试用纯Java制作我的第一个游戏。 我正在使用Graphics绘制项目。更具体的是g.fillRec()。 我稍后会处理真实的图像,但现在这已经足够了。 问题是,当我想绘制一个地形矩形时,我周围有一个边框。 这是边界的图片:

enter image description here

绿色矩形周围的白色物体是我的边界...... 这是我的代码:

BagGame.java

package main;

import java.awt.Container;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

public class BagGame extends JFrame implements WindowListener {
    private static final long serialVersionUID = 1L;
    private static BagPanel panel;

    public BagGame(){
        super("The Bag Collecting");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addWindowListener(this);
        Container c = getContentPane();
        panel = new BagPanel();
        c.add(panel, "Center");
        pack();
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);

    }

    @Override
    public void windowActivated(WindowEvent arg0) {


    }

    @Override
    public void windowClosed(WindowEvent arg0) {

    }

    @Override
    public void windowClosing(WindowEvent arg0) {

    }

    @Override
    public void windowDeactivated(WindowEvent arg0) {

    }

    @Override
    public void windowDeiconified(WindowEvent arg0) {

    }

    @Override
    public void windowIconified(WindowEvent arg0) {

    }

    @Override
    public void windowOpened(WindowEvent arg0) {

    }

    public static void main(String args[]){
        new BagGame();
    }


}

BagPanel.java

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class BagPanel extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private static final int PWIDTH = 720;
    private static final int PHEIGHT = 480;
    private static Thread game;
    private static volatile boolean running = false;
    private static volatile boolean gameOver = false;
    private static volatile boolean paused = false;
    public static Player player;
    public static Terrain terrain;
    public static Graphics g;
    public static Image gImage;
    long lastLoopTime = System.currentTimeMillis();
    long delta;
    int speed = 2;
    private static volatile boolean upPressed = false, downPressed = false, leftPressed = false, rightPressed = false;

    public BagPanel() {

        setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
        setBackground(Color.white);

        setFocusable(true);
        requestFocus();
        waitForTermination();

    }

    public void addNotify() {
        super.addNotify();
        startGame();
    }

    public void waitForTermination() {
        addKeyListener(new KeyListener() {
            //player.y -= (delta * speed) / 10;
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if(keyCode == KeyEvent.VK_ESCAPE){
                    stopGame();
                }
                if(keyCode == KeyEvent.VK_W){
                    upPressed = true;
                }
                if(keyCode == KeyEvent.VK_S){
                    downPressed = true;
                }
                if(keyCode == KeyEvent.VK_A){
                    leftPressed = true;
                }
                if(keyCode == KeyEvent.VK_D){
                    rightPressed = true;
                }

            }

            @Override
            public void keyReleased(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if(keyCode == KeyEvent.VK_W){
                    upPressed = false;
                }
                if(keyCode == KeyEvent.VK_S){
                    downPressed = false;
                }
                if(keyCode == KeyEvent.VK_A){
                    leftPressed = false;
                }
                if(keyCode == KeyEvent.VK_D){
                    rightPressed = false;
                }

            }

            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }


        });
    }

    @Override
    public void run() {
        running = true;
        while (running) {

            delta = System.currentTimeMillis() - lastLoopTime;
            lastLoopTime = System.currentTimeMillis();

            gameUpdate();
            gameRender();
            checkMovement();
            paintScreen();



            try {
                Thread.sleep(5);
            } catch (Exception e) {
                System.out.println("The thread couldn't sleep! Error info: "
                        + e);
            }

        }
        System.exit(0);

    }

    private void checkMovement() {
        if(upPressed){
            Player.y -= (delta * speed) / 10;
        }
        if(downPressed){
            Player.y += (delta * speed) / 10;
        }
        if(leftPressed){
            Player.x -= (delta * speed) / 10;
        }
        if(rightPressed){
            Player.x += (delta * speed) / 10;
        }

    }

    private void paintScreen() {
        Graphics g;
        try {
          g = this.getGraphics();
          if ((g != null) && (gImage != null))
            g.drawImage(gImage, 0, 0, null);
          g.dispose();
        }
        catch (Exception e)
        { System.out.println("Graphics context error: " + e);  }
    }

    private void gameRender() {
        if(gImage == null){
            gImage = createImage(PWIDTH, PHEIGHT);
            if(gImage == null){
                System.out.println("image null after creating it??? Please check the code for any errors!");
            } else {
                g = gImage.getGraphics();
            }
        }
        if(!paused){
            g.setColor(Color.white);
            g.fillRect (0, 0, PWIDTH, PHEIGHT);
            g.setColor(Color.blue);

            if(player != null){
                player.draw(g);
            }
            if(terrain != null){
                terrain.draw(g);
            }
        }
    }

    private void gameUpdate() {
        if(!paused && !gameOver){

        }
    }

    public void startGame() {
        if (game == null) {
            game = new Thread(this);
            if (game == null) {
                System.out.println("Couldn't create the thread!");
            } else {
                System.out.println("Thread created!");
                game.start();
            }
        }
        if(player == null){
            player = new Player();
            if(player == null){
                System.out.println("The player was not created!");
            } else {
                System.out.println("The player is successfully created!");
            }
        }
        if(g == null){
            g = this.getGraphics();
            if(g == null){
                System.out.println("The graphics were not created!");
            } else {
                System.out.println("The graphics are successfully created!");
            }
        }
        if(terrain == null){
            terrain = new Terrain(PWIDTH, PHEIGHT);
            if(terrain == null){
                System.out.println("Couldn't create the terrain!");
            }
        }
        running = true;

    }

    public void stopGame() {
        running = false;

    }
}

Player.java

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Player extends Rectangle {
    private static final long serialVersionUID = 1L;
    private static final int WIDTH = 32;
    private static final int HEIGHT = 32;
    public static int x = 64;
    public static int y = 64;

    public Player(){

    }


    public void draw(Graphics g){
        g.setColor(Color.blue);
        g.fillOval(x, y, WIDTH, HEIGHT);
        g.setColor(Color.white);
    }

}

最后是Terrain.java

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Terrain extends Rectangle {
    private static final long serialVersionUID = 1L;
    private static int TWIDTH = 0;
    private static int THEIGHT = 0;

    public Terrain(int width, int height){
        TWIDTH = width;
        THEIGHT = height;
    }

    public void draw(Graphics g){
        g.setColor(Color.green);
        g.fillRect(0, 0, TWIDTH, THEIGHT);
        g.setColor(Color.white);
    }

}

这是所有代码:)任何帮助都会很棒! 提前谢谢!

1 个答案:

答案 0 :(得分:1)

在致电setResizeable(false)之前,您需要在框架上致电pack()。调用pack()将调整框架大小以适合其组件。之后使框架不可调整大小会使边框更薄,这会增加框架内的空间。