简体   繁体   English

每次调整 JFrame 大小时 JPanel 都会刷新

[英]JPanel refreshes everytime the JFrame is resized

Whenever I move my window on the display;每当我在显示器上移动窗口时; the images and text constantly refresh.图片和文字不断刷新。

Because some content is generated randomly and then drawn, it regenerates and redraws the randomly generated parts on each refresh.由于某些内容是随机生成然后绘制的,因此每次刷新时都会重新生成并重绘随机生成的部分。

How can I make them only refresh when I want them too?我怎样才能让它们只在我想要它们时刷新?
In this case, when monsterHealth reaches 0.在这种情况下,当monsterHealth达到 0 时。

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
      
public class RPGClicker {

    static int attackLevel = 1;
    static int defenceLevel = 1;
    static int hitpointsLevel = 1;
            
    static int xp = 0;
    static int gold = 0;
    static int dps = 0;
    static int clickDamage = attackLevel;
    static int health = hitpointsLevel * 100;
    static int healthRecovery = 1;
    static int room = 1;
             
    public static void main(String[] args) n{      
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                mainGameScene();
            }
        });
    }
      
    public static void mainGameScene() {              
        JFrame window;
        mainGamePanel mainGameInstance;
            
        window = new JFrame("RPGClicker: An Unknown Quest!");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new BorderLayout());
        window.setResizable(false);
            
        mainGameInstance = new mainGamePanel();
            
        window.add(mainGameInstance, BorderLayout.CENTER);
            
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }
}
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;
      
class mainGamePanel extends JPanel {

    BufferedImage background, user;

    public Dimension getPreferredSize() {
        return new Dimension(1280, 720);
    }
      
    public void paintComponent(Graphics pencil) {
        super.paintComponent(pencil);
            
        background = ImageLoader.loadImage("rec/alpha/background0.png");
            
        user = ImageLoader.loadImage("rec/alpha/user.png");
                       
        pencil.drawImage(background, 0, 0, null);
        pencil.drawImage(user, 100, 450, null);
            
        Monster monster = new Monster();
        pencil.drawImage(monster.monsterSprite, 900, 50, null);
        pencil.drawString(monster.monsterName, 900, 60);
    }
         
    public mainGamePanel() {
         
    }
}
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;
      
public class Monster {

    static final double BASE_MONSTER_HEALTH = 10;
         
    static double monsterHealth = Math.pow(RPGClicker.room, 2) * BASE_MONSTER_HEALTH;
    static double monsterDamage = RPGClicker.room + 1 - RPGClicker.defenceLevel;
            
    BufferedImage monsterSprite;
    String monsterName;
    Random rand = new Random();
         
    public Monster() {
        String monster[] = {"Ork", "Mermaid", "Goblin"};
        String monsterType = monster[rand.nextInt(monster.length)];
        monsterSprite = ImageLoader.loadImage("rec/alpha/monster/" + monsterType + ".png");
            
        String[] firstName = {"Oliver", "George", "Harry"};
        String connection1 = " the ";
        String[] secondName = {"Powerful ", "Unstoppable ", "Almighty "};
        String connection2 = " of ";
        String[] thirdName = {"Death", "America", "Pride"};
            
        monsterName = firstName[rand.nextInt(firstName.length)] + connection1 + secondName[rand.nextInt(secondName.length)] + monsterType + connection2 +  thirdName[rand.nextInt(thirdName.length)];
    }
}

You're making your painting method do too much.你让你的绘画方法做得太多了。 Please understand that:请理解:

  • A painting method (eg, paintComponent ) is for painting and painting only .涂装方法(例如, paintComponent )可以画和画
  • Do not read in image files or do any other file I/O within these methods as this will critically slow down rendering, making your GUI seem poorly responsive.不要在这些方法中读入图像文件或执行任何其他文件 I/O,因为这会严重减慢渲染速度,使您的 GUI 看起来响应很差。 Why keep re-reading in images anyway when they only need to be and should be read in once.当图像只需要并且应该被读取一次时,为什么还要继续重新读取图像。
  • Do not put in any program logic, such as Monster creation, within these methods since you do not have full control over when or even if a painting method is fired.不要在这些方法中放入任何程序逻辑,例如创建怪物,因为您无法完全控制绘制方法何时或是否被触发。 Put the logic and object creation elsewhere.将逻辑和对象创建放在别处。

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

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