繁体   English   中英

Java图形-重新绘制图像而不删除另一个

[英]Java graphics - repaint an image without removing another

我正在使用Graphics g为播放器和地图绘制图像。 每当玩家移动时,我都希望将玩家图像更新到新位置,而不更新地图图像。 我怎样才能做到这一点?

预先感谢您的任何答复!

对不起,我的代码混乱。

   Main:
package Main;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Main {
    public static JFrame frame;

    static int size = 32;
    static int width = size*20;
    static int height = size*17;

    static playerObj p = new playerObj(200,200);

    static tileMap grid = new tileMap();

    public static void main(String[] args) {
        frame = new JFrame("Game");
        frame.getContentPane().setPreferredSize(new Dimension(width-9, height-9));
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(new drawGraphics());
        frame.setVisible(true);
    }
}

drawGraphics:
package Main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class drawGraphics extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;

    boolean running = true;
    boolean renderMap = true;

    playerObj p = Main.p;
    int size = Main.size;
    static int x = 0;
    static int y = 0;

    Thread thread = new Thread(this);

    public drawGraphics() {
        setFocusable(true);
        addKeyListener(new controls());
        start();
    }

    public void start() {
        thread.start();
    }

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

        if (renderMap) {
            map(d);
        }

        player(g);

        repaint();
    }

    public void player(Graphics g) {
        try {
            BufferedImage pImg = ImageIO.read(new File("images/player.png"));
            g.drawImage(pImg, p.getX(), p.getY(), null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void map(Graphics g) {
        System.out.println("2dfsdf");
        Graphics2D d = (Graphics2D) g;
        for (int i = 0; i < tileMap.map.length; i++) {
            for (int j = 0; j < tileMap.map[i].length; j++) {
                d.setColor(tileMap.map[i][j].getC());
                d.fillRect(tileMap.map[i][j].getX(), tileMap.map[i][j].getY(),
                        Main.size, Main.size);
                if (tileMap.map[i][j].solid()) {
                    d.setColor(Color.BLACK);
                    d.drawRect(tileMap.map[i][j].getX(),
                            tileMap.map[i][j].getY(), Main.size, Main.size);
                }
            }
        }
    }

    public void move() {
        if (controls.up) {
            controls.goUp();
            y--;
            repaint();
        }
        if (controls.down) {
            controls.goDown();
            y++;
            repaint();
        }
        if (controls.left) {
            controls.goLeft();
            x--;
            repaint();
        }
        if (controls.right) {
            controls.goRight();
            x++;
            repaint();
        }
        if (controls.place) {
            repaint();
        }
    }

    public void run() {
        while (running == true) {
            move();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

不要直接在JFrame中绘制,而要在JFrame中显示的JPanel中绘制。

如果您将Swing用作GUI库,则将背景绘制为BufferedImage,然后首先在paintComponent方法中进行绘制。 然后在接下来需要的任何位置绘制图像精灵:

@Override  // this is in a JPanel extended class
protected void paintComponent(Graphics g) {
   super.paintComponent(g);
   if (backgroundImg != null) {
      g.drawImage(backgroundImg, 0, 0, null);
   }
   if (spriteImg != null) {
      g.drawImage(spriteImg, spriteX, spriteY, null);
   }
}

您的代码存在一些问题:

  • 不要覆盖paint ,然后在其中调用super.paintComponent
  • 而是重写paintComponent并在其中调用相同的super方法。
  • 切勿在paint或paintComponent内部调用repaint() 请改用Swing计时器。
  • 在您的player(...)方法中,每次调用该方法时,您都会重新读取图像,这会使绘画慢下来。 不要这样做,而是一次读取图像,然后将其保存到变量中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM