Sign in to follow this  
Lerp

[java] New JFrame not appearing correctly

Recommended Posts

Lerp    122
In my application I have my first JFrame in which the user can make a selection. When the user makes a selection I setVisible(false); for the current JFrame and bring up a new one. The code for the new one is this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class JCredits extends JFrame {

  static final int maxLines = 100;
  static final int WIDTH = 400;
  static final int HEIGHT = 500;
  
  String lines[];
  Dimension offDimension;
  Image offImage;
  Graphics offGrfx;
  int XPos = 5;
  int spacing = 12;
  int delay = 100;
  int current = HEIGHT;

  public JCredits() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Credits");
    setResizable(false);
    setSize(WIDTH, HEIGHT);
    show();
    
    setBackground(new Color(0, 0, 0));
    setForeground(new Color(255, 255, 255));
    setFont(new Font("Arial", Font.BOLD, 12));

    lines = new String[3];
    lines[0] = "Lead Designer: Marc Fisher";
    lines[1] = "Designer: Carl Soriano";
    lines[2] = "Lead Programmer: Marc Fisher";
    
    Dimension d = getSize();

    offImage = createImage(d.width, d.height);
    offGrfx = offImage.getGraphics();
    
    while (true) {
      repaint();
      current--;
      if((current + (maxLines * spacing)) < 0) {
        current = HEIGHT + spacing;
      }
      try { 
        Thread.sleep(delay); 
      }
      catch (InterruptedException e) { }
    }
  }
  
  public void paint(Graphics g) {
    update(g);
  }

  public void update(Graphics g) {
    Dimension d = getSize();

    // Create the offscreen graphics context
    if( (offGrfx == null) 
        || (d.width != offDimension.width)
        || (d.height != offDimension.height)) {
      offDimension = d;
      offImage = createImage(d.width, d.height);
      offGrfx = offImage.getGraphics();
    }
    
    offGrfx.setColor(new Color(0, 0, 0));
    offGrfx.fillRect(0, 0, WIDTH, HEIGHT);
    
    offGrfx.setColor(new Color(255, 255, 255));
    
    for(int n = 0; n < 3; n++) {
      offGrfx.drawString("Scroll",  XPos, current + (n * spacing));
    }

    g.drawImage(offImage, 0, 0, this);

  }
}

But when I bring up the new JFrame it looks like this: here Any suggestions on why this may be happening? If I create the JCredits in main it works just fine, but if it is created after the first JFrame is then it doesn't.

Share this post


Link to post
Share on other sites
CaptainJester    523
You should not override paint() in Swing componenets. You have incorrectly overridden update(). update() is called by the JVM every time it needs to refresh the app. All the painting code you have in update() should be in paintComponent(). The first line in paintComponent() should be
super.paintComponent(g);
.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this