user3298897
user3298897

Reputation: 23

Java: Minimizing a Jframe Window without causing the program to run again

My program uses a Jframe, whenever I minimize the window and bring it back up, the program runs itself again. How can I cause it to not do this when minimized?

Code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.util.Random;

public class AB extends Component {
    public void paint(Graphics g) {
        Random r = new Random();
        g.setColor(Color.black);
        g.fillRect(r.nextInt(100), r.nextInt(100), 100, 100);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Load Image Sample");
        f.getContentPane().setBackground(Color.white);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        f.setSize(dim.width, dim.height);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.add(new AB());
        f.setVisible(true);
    }
}

Upvotes: 2

Views: 496

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

My guess: you have program logic present in your paint(...) or paintComponent(...) method override. If so, get the logic out of these methods, because as you're finding out, you do not have full control over when or even if they are called. The logic belongs elsewhere, perhaps in a Swing Timer, hard to say without code or a more detailed question.

If this doesn't help, then you'll need to show us the bugs in your code by creating and posting a minimal, runnable, example program as well as tell us more of the details of your program and its misbehavior.


After looking at your code, I see that my assumption/guess was correct: you are selecting your random numbers inside of the paint method, and so they will change with each repaint. You will want to

  • Create a class that extends JPanel.
  • Create your random numbers in this class's constructor, and use the random numbers to set class instance fields.
  • Override this class's paintComponent method
  • Don't forget to call the super's paintComponent method inside this method.
  • Use the numbers generated in the constructor in the paintComponent method override to draw your rectangle.
  • Place your JPanel into a JFrame.

e.g.,

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.*;

@SuppressWarnings("serial")
public class RandomRect extends JPanel {
   private static final int MAX_INT = 100;
   private static final Color RECT_COLOR = Color.black;
   private Random random = new Random();
   private int randomX;
   private int randomY;

   public RandomRect() {
      randomX = random.nextInt(MAX_INT);
      randomY = random.nextInt(MAX_INT);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(RECT_COLOR);
      g.fillRect(randomX, randomY, MAX_INT, MAX_INT);
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("RandomRect");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new RandomRect());
      //frame.pack();
      // frame.setLocationRelativeTo(null);
      frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Upvotes: 2

Related Questions