首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java图形-在不删除另一个图像的情况下重新绘制图像

Java图形-在不删除另一个图像的情况下重新绘制图像
EN

Stack Overflow用户
提问于 2015-05-16 18:57:54
回答 1查看 2.2K关注 0票数 2

我正在使用图形g为我的球员绘制图像和地图。每当玩家移动时,我想要更新播放器图像到新的位置,而不更新地图图像。我该怎么做?

谢谢您的回复!

抱歉,我的密码乱七八糟。

代码语言:javascript
复制
   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();
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-16 19:03:07

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

如果使用Swing作为GUI库,那么将背景绘制为BufferedImage,并在paintComponent方法中首先绘制它。然后在接下来需要的任何位置画出你的图像精灵:

代码语言:javascript
复制
@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并调用内部相同的超级方法。
  • 不要在油漆或repaint()内部调用paintComponent。使用摆动计时器代替。
  • 在您的player(...)方法中,每次调用该方法时都会重新读取该图像,这将使您的绘画速度减慢到爬行。不要这样做,而是一次读取图像,并将其保存到变量中。
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30279584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档