Jump to content
  • Advertisement
Sign in to follow this  

[java] New JFrame not appearing correctly

This topic is 4958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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() {
    setSize(WIDTH, HEIGHT);
    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) {
      if((current + (maxLines * spacing)) < 0) {
        current = HEIGHT + spacing;
      try { 
      catch (InterruptedException e) { }
  public void paint(Graphics 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
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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!