Color.White is skipped when using drawString () java

When I tried to draw white letters on a black background, I noticed something strange.

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
}

! Do not look at the code in the images, just look at the frame!

Give it to me: "White" letters on a black background

The lines, however, are well drawn.

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

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

White letters on a black background

Why is java.awt.Graphicsblocking pure white (255,255,255) letters from drawing (even if it is on a black background)?

Tia, Charlie

+4
source share
3 answers

jdk1.8.0_20, Linux (Ubuntu): 0xFFFFFFFF BLACK. - RGB " ".

jdk1.7.0_67 .

setColor.

, : JDK-8054638:

: 8u11,8u25

Linux; Windows .

+4

setVisible (true) LAST, . , paintComponent. , :

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();
         }
      });
   }
}
+2

. setVisible (true) JFrame. .

-1

Source: https://habr.com/ru/post/1569206/


All Articles