Seth Falco
Seth Falco

Reputation: 620

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.

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)];
    }
}

Upvotes: 1

Views: 218

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're making your painting method do too much. Please understand that:

  • A painting method (e.g., paintComponent) is for painting and painting only.
  • 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. 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.

Upvotes: 3

Related Questions