Charlie
Charlie

Reputation: 986

Color.White is getting skipped when using drawString() java

When I was trying to draw white letters on a black background I noticed something weird.

public WhiteOnBlackPanel() {
    setBackground(Color.BLACK);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(new Color(255,255,255));
    g.drawString("Hello World",100,100);
    g.drawLine(0,0,100,100);
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.add(new WhiteOnBlackPanel());
    frame.setTitle("Hello World");
    frame.setSize(600,400);
    frame.setLocation(100,100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true); // The frame is visible from now on
}

! Don't look at the code in the images, just look at the frame !

Gave me this: "White" letters on a black background

Lines, however, were being drawn well.

"White" letters and a white line on a black background

When I took a different, but very close, color (254, 255, 255), I got this

White letters on a black background

Why is java.awt.Graphics blocking pure white (255,255,255) letters from being drawn (even when it is on a black background) ?

Tia, Charlie

Upvotes: 4

Views: 1038

Answers (3)

laune
laune

Reputation: 31300

A bug in jdk1.8.0_20, at least in Linux (Ubuntu): 0xFFFFFFFF appears as BLACK. Changing alpha or one of the RGB values results in "almost white".

jdk1.7.0_67 works fine on the same system.

Checked all forms of setColor.

Later Found that bug is reported: JDK-8054638 : White color is not painted

Affected Versions: 8u11,8u25

This bug only affects Linux; on Windows everything works fine.

Upvotes: 4

Shriram
Shriram

Reputation: 4421

you are adding a panel to a frame. setVisible(true) to the JFrame. Without this it won't be visible.

Upvotes: -1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285440

Call setVisible(true) LAST, after adding all components and setting things up. Don't override paint but rather paintComponent. For instance, this works fine:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;

@SuppressWarnings("serial")
public class ShowColor extends JPanel {
   private static final int PREF_W = 600;
   private static final int PREF_H = 400;

   public ShowColor() {
      setBackground(Color.black);
   }

   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(new Color(255,255,255));
      g.drawString("Hello World",100,100);
   }

   private static void createAndShowGUI() {
      ShowColor paintEg = new ShowColor();

      JFrame frame = new JFrame("ShowColor");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(paintEg);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

Upvotes: 2

Related Questions