gpt4 book ai didi

java - drawRect() 在某些颜色上无法正常工作

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:51 25 4
gpt4 key购买 nike

我总是用这样的轮廓创建矩形(使用 Graphics(2D)):

g.setColor(aColor);
g.fillRect(x, y, width, height);
g.setColor(anotherColor);
g.drawRect(x, y, width, height);

这工作正常,除了某些颜色,如 Color.BLUE。有些线的粗细不同:

enter image description here

乍一看可能很难看,但仔细看就会发现左边的线太粗,右边的线太细。其他颜色也会发生这种情况,只是不那么明显:(我仍然不确定青色是否会发生这种情况,无法准确判断)

enter image description here

我无法理解这一点,因为黑线只是被绘制到内部蓝色矩形上,内部矩形不应该对其产生影响。 (没有 fillRect() 线条粗细均匀)

我在下面提供了一个示例,可能会帮助您更好地了解差异。我的问题:为什么某些 RGB 颜色会发生这种情况,我该如何解决?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.WindowConstants;

public class LineExample {

Color colors[] = new Color[] { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN,
Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };
String colorNames[] = new String[] { "Black", "Blue", "Cyan", "Dark Gray", "Gray", "Green", "Light Gray", "Magenta",
"Orange", "Pink", "Red", "White", "Yellow" };
HashMap<String, Color> hashMap = new HashMap<String, Color>();
Color currentColor = colors[2];

public LineExample() {

fillHashMap(hashMap);

JFrame frame = new JFrame();

JPanel mainPanel = new JPanel(new BorderLayout());
JPanel northPanel = new JPanel(new FlowLayout());
JPanel centerPanel = new JPanel(new GridLayout(1, 2));
CustomPanel customPanel = new CustomPanel();
BluePanel bluePanel = new BluePanel();

JComboBox<String> comboBox = new JComboBox<String>();
addItems(comboBox);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
currentColor = hashMap.get(comboBox.getSelectedItem());
centerPanel.repaint();
}
});

JToggleButton toggleButton = new JToggleButton("Switch");
toggleButton.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
centerPanel.removeAll();
if (e.getStateChange() == ItemEvent.SELECTED) {
centerPanel.add(bluePanel);
centerPanel.add(customPanel);
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
centerPanel.add(customPanel);
centerPanel.add(bluePanel);
}
centerPanel.revalidate();
centerPanel.repaint();
}
});

northPanel.add(comboBox);
northPanel.add(toggleButton);
centerPanel.add(customPanel);
centerPanel.add(bluePanel);
mainPanel.add(northPanel, BorderLayout.NORTH);
mainPanel.add(centerPanel, BorderLayout.CENTER);

frame.setContentPane(mainPanel);

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(250, 250);
frame.setVisible(true);

}

public void addItems(JComboBox<String> comboBox) {
for (int i = 0; i < colors.length; i++) {
comboBox.addItem(colorNames[i]);
}
comboBox.setSelectedIndex(2);
}

public void fillHashMap(HashMap<String, Color> hashmap) {
for (int i = 0; i < colors.length; i++) {
hashMap.put(colorNames[i], colors[i]);
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new LineExample();
}
});
}

public class BluePanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = 100;
int height = 100;
int x = ((this.getWidth() - width) / 2);
int y = ((this.getHeight() - height) / 2);
g.setColor(Color.BLUE);
g.fillRect(x, y, width, height);
g.setColor(Color.BLACK);
g.drawRect(x, y, width, height);
}
}

public class CustomPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = 100;
int height = 100;
int x = ((this.getWidth() - width) / 2);
int y = ((this.getHeight() - height) / 2);
g.setColor(currentColor);
g.fillRect(x, y, width, height);
g.setColor(Color.BLACK);
g.drawRect(x, y, width, height);
}
}

}

最佳答案

这是一个有趣的问题,是由于 LCD 显示器中的子像素排列而产生的。我用手机拍了一些显示器的照片来解释这种效果。

首先我们看左边。这条线看起来大约有 2 个像素粗。这是因为在线路上从左到右是黑色到蓝色的过渡。由于红色和绿色像素不能被蓝色照亮,我们必须等到线应该结束后的下一个蓝色子像素。

enter image description here

然后我们看右边。这条线大约有 1 个像素粗。这次是从黑到白的过渡,所以红色和绿色像素可以在线条之后立即点亮。

enter image description here

这个问题的出现是因为 Java 图形库不支持子像素(至少对于形状)。对于每台显示器上的某种颜色组合,您总会遇到这个问题。红绿蓝原色最为明显,这就是为什么在您的青色示例中很难分辨的原因。

关于java - drawRect() 在某些颜色上无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32595624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com