0

I was programming up a nice little utility and all was going well, but then I tried to add some labels. They showed up. Then I made them longer and they disappeared. I tried many experiments (repositioning, changing font, changing the order) but nothing was consistent other than the longer strings were problematic. Thinking that I was using Swing incorrectly, I tried to simplify by moving back to AWT, but I see almost identical problems. I have even stepped through the code using jdb and, again, sometimes the g.drawString works on the longer strings and other times it does not. I tried catching any errors and did not see anything as well.

Clearly, I am missing something fundamental. Can someone explain: 1) why do the shorter strings (e.g. "label") seem to always work? 2) why do the longer strings sometimes not show at all? 3) are the failures due to the sting not being rendered at all or are they getting overwritten by some other image? 4) if #3 is "overwritten", how does one control the order of these other (unknown) images?

I have stripped the code down. I am compiling with javac 1.7.0_79

import java.awt.*;

public class GraphicsQuestion extends Frame {
   public GraphicsQuestion()
   {
       super("Question");  //call to Frame
       setFocusable(true);
   }
   public void DrawLabels(Graphics g)
   {
       g.setFont(new Font("Courier", Font.PLAIN, 16));
       g.setColor(Color.BLACK);

       g.drawString("label", 0, 100);
       String ss = new String("label1234567890123456789");
       g.drawString(ss, 100, 150);

       g.drawString("label12345", 100, 170); //does not print
       g.drawString("label1", 10, 170);

   }
   public void paint(Graphics g)
   {
      super.paint(g);
      DrawLabels(g);
   }
   public static void main(String args[]) {
      GraphicsQuestion gq = new GraphicsQuestion();
      gq.setBackground(Color.WHITE);
      gq.setSize(800, 200);
      gq.setVisible(true);
   }
}

As the comment in the code suggests, the output is missing one of the labels. At times, it is missing both of the longer labels. And if I comment out the printing of the "label12345", the first time I execute, I am missing the "label1234567890123456789" label. And if I make the "label12345" string shorter, like "label123" everything is printed.

I am used to consistent behavior in my programming. If I am having trouble understanding what is going on with this simple example, I am hesitant to build anything more complicated. Can anyone explain the inconsistent behavior?

**** original question above, what follows is based on answers received ****

Thanks to VGR for the quick response. I had been trying to not "mix Swing with AWT" but I tried the code. It seemed to improve things. Unfortunately, I have taken it to the next level and added KeyListener functionality and I see the instabilities again.

Now I wonder if I have something deeper than a Java error. Maybe I have some hardware or graphics driver failure because the font can be distorted. I can compile the code and it will be missing the labels 20 out of 20 times and then recompile the same code the same way and get this distorted output.

http://www.screencast.com/t/rsBjRZlXlWu

I will include my new code below, but this is looking less and less like a Java programming issue and more like an environment issue. I am running Ubuntu 14.04.3 LTS on a Dell E7450

import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;

class GraphicsAnswer extends Frame implements KeyListener {
   public GraphicsAnswer()
   {
       super("Question2");  //call to Frame
       addKeyListener(this);
       setFocusable(true);
   }
   public void DrawLabels(Graphics g)
   {
       g.setFont(new Font("Courier", Font.PLAIN, 16));
       g.setColor(Color.BLACK);

       g.drawString("label", 0, 100);
       String ss = new String("label1234567890123456789");
       g.drawString(ss, 100, 150);            //sometimes distorted

       g.drawString("label123456", 100, 170); //sometimes distorted
       g.drawString("label1", 10, 170);

   }
   public void paint(Graphics g)
   {
      super.paint(g);
      DrawLabels(g);
   }
   public void keyPressed(KeyEvent e)
   {
      int keyCode = e.getKeyCode();
      if (e.getKeyChar() == 'X')
         System.exit(0);
      else
         update(e.getKeyChar());
   }
   public void keyReleased(KeyEvent e)
   {
   }
   public void keyTyped(KeyEvent e)
   {
   }
   public void update( char c)
   {
      Graphics g = this.getGraphics();
      g.drawString("done", 10, 10);
   }
}

public class QMain2 implements Runnable {
    public void run() {
       // Invoked on the event dispatching thread
       // Construct and show GUI
       GraphicsAnswer ga = new GraphicsAnswer();
       ga.setBackground(Color.WHITE);
       ga.setSize(800, 200);
       ga.setVisible(true);
    }
    public static void main(String [] args) {
       SwingUtilities.invokeLater(new QMain2());
    }
}

Anyone know of a font driver test suite for Java so I can test my environment?

JayWH
  • 1
  • 2
  • Swing operations must be performed in the AWT event dispatch thread. Executing them anywhere else causes unpredictable behavior. This is described in the [javax.swing package documentation](https://docs.oracle.com/javase/8/docs/api/javax/swing/package-summary.html#package.description). In short: Put the body of your `main` method inside a call to [EventQueue.invokeLater](https://docs.oracle.com/javase/8/docs/api/java/awt/EventQueue.html#invokeLater-java.lang.Runnable-). – VGR Aug 29 '15 at 19:48
  • Thanks VGR. It certainly seems more stable now. I am reading (and re-reading) the links you pointed me at. I expect it will sink to a greater extent as I continue programming. I appreciate the quick help. – JayWH Aug 29 '15 at 20:27
  • I have updated the question with code as suggested by VGR. Unfortunately, it has uncovered more instabilities as I add complexity. It is possible that this is not a Java coding issue but instead a Java/Linux/graphics environment issue. – JayWH Aug 30 '15 at 16:15
  • That looks like a graphics driver issue, all right. Try some of the settings described [here](http://docs.oracle.com/javase/8/docs/technotes/guides/2d/flags.html#ix). – VGR Aug 31 '15 at 02:06

1 Answers1

0

I have entered, tested, and compiled the exact code from above without modifications and everything seems to appear without flaw very consistently. All four strings were displayed 20/20 test runs that I did on it.

You mention that you "stripped" your code down for this question you posted, so perhaps the issue lies with the remainder of the project, or simply a graphical error on your end.

Here is a screenshot of my results on all 20 test compiles. Best of luck!

Community
  • 1
  • 1
m_callens
  • 6,100
  • 8
  • 32
  • 54
  • Thanks m_callens for the feedback. It does not surprise me that this code would work for some people, possibly even all the time. I certainly expected it to work for ME all of the time. But it is inconsistent for me. I have updated the question with improved code from VGR which now seems consistent. I will now port this to a JFrame and Swing components and see if I have any regressions. – JayWH Aug 29 '15 at 20:19