[java] New JFrame not appearing correctly

Started by
0 comments, last by CaptainJester 19 years, 4 months ago
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.
Advertisement
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);
.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement