每次调整JFrame大小时,我的JPanel都会保持刷新

时间:2016-05-04 11:20:00

标签: java swing jframe resize jpanel

我一直在努力创建一款类似于其中一款游戏的游戏。我对编码很新,但似乎不知道如何解决某些问题。这是一。 我道歉,但我可能会在下面添加多余的代码。

无论如何,问题是每当我向右上方移动窗户时;所以我将窗口向下拖动到左下角,然后向右拖动。我的图像和文字不断刷新。请记住,它们从列表中设置为随机。因此,文本完全变为另一个随机选择以及图像。

如果我想要它们,我怎么才能让它们刷新? 当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)
     {      
        SwingUtilities.invokeLater(
           new Runnable()
           {
              public void run()
              {
              //Calls the mainGameScene method.
                 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", "Fish", "Griffen", "Cerberus", "Empousai", "Gorgon", "Stymphalian Bird", "Chimera", "Ichthyocentaurs", "Typhon", "Minotaur", "Fury", "Hydra", "Sphinx"};
        String monsterType = monster[rand.nextInt(monster.length)];
        monsterSprite = ImageLoader.loadImage("rec/alpha/monster/" + monsterType + ".png");

        String[] firstName = {"Lucas", "Ben", "Caytlynn", "Fay", "Seth", "Jostien", "Paige", "Luis", "Josefiina", "Patricia", "Angus", "David", "Manjit", "Matt", "Maya", "Richard", "Roman", "Rudy", "Téa", "Fi"};
        String connection1 = " the ";
        String[] secondName = {"Powerful ", "Unstoppable ", "Almighty ", "Vigourous ", "Formidible ", "Mighty ", "Cowardly ", "Intoxicated "};
        String connection2 = " of ";
        String[] thirdName = {"Death", "America", "Pride", "Greed", "Lust", "Envy", "Gluttony"};

        monsterName = firstName[rand.nextInt(firstName.length)] + connection1 + secondName[rand.nextInt(secondName.length)] + monsterType + connection2 +  thirdName[rand.nextInt(thirdName.length)];
     }
  }

1 个答案:

答案 0 :(得分:3)

你让你的绘画方法做得太多了。请理解:

  • 绘画方法(例如paintComponent)用于绘画和绘画
  • 不要在这些方法中读取图像文件或执行任何其他文件I / O,因为这会严重降低渲染速度,使您的GUI看起来响应不佳。无论如何,为什么要继续阅读图像,只需要阅读一次即可。
  • 不要在这些方法中加入任何程序逻辑,例如Monster创建,因为您无法完全控制何时或甚至是否触发绘制方法。将逻辑和对象创建放在别处。