- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我总是用这样的轮廓创建矩形(使用 Graphics
(2D
)):
g.setColor(aColor);
g.fillRect(x, y, width, height);
g.setColor(anotherColor);
g.drawRect(x, y, width, height);
这工作正常,除了某些颜色,如 Color.BLUE
。有些线的粗细不同:
乍一看可能很难看,但仔细看就会发现左边的线太粗,右边的线太细。其他颜色也会发生这种情况,只是不那么明显:(我仍然不确定青色是否会发生这种情况,无法准确判断)
我无法理解这一点,因为黑线只是被绘制到内部蓝色矩形上,内部矩形不应该对其产生影响。 (没有 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 个像素粗。这是因为在线路上从左到右是黑色到蓝色的过渡。由于红色和绿色像素不能被蓝色照亮,我们必须等到线应该结束后的下一个蓝色子像素。
然后我们看右边。这条线大约有 1 个像素粗。这次是从黑到白的过渡,所以红色和绿色像素可以在线条之后立即点亮。
这个问题的出现是因为 Java 图形库不支持子像素(至少对于形状)。对于每台显示器上的某种颜色组合,您总会遇到这个问题。红绿蓝原色最为明显,这就是为什么在您的青色示例中很难分辨的原因。
关于java - drawRect() 在某些颜色上无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32595624/
为了澄清这个问题的目的:我知道如何使用 subview 和使用 drawRect 创建复杂的 View 。我试图完全理解何时以及为什么使用一个而不是另一个。 我也明白提前优化并在进行任何分析之前以更困
我是 Swift 的新手,正在尝试让一个非常简单的应用程序运行。我想要做的就是让 UIView.drawRect 在我按下按钮时更新。它会在应用程序首次加载时更新/绘制,然后无论我尝试什么,它都不会更
我有一个名为 Icon 的基于 UIView 的类。在那里,我想要类似于这样的代码: UIView *finalView = [[UIView alloc] initWithFrame:CGRectM
我正在覆盖 drawRect: 在我的一个 View 中,它甚至在不调用 [super drawrect:rect] 的情况下也能工作。这是如何运作的? - (void)drawRect:(CGRec
我不是很明白CALayer的display和drawInContext与 View 中的drawRect有什么关系。 如果我有一个每 1 秒设置一次 [self.view setNeedsDispla
我在 viewController 上放置了一个 View ,我想在此 View 中绘制一个矩形,但具有自定义高度。我根据 viewController 中的参数确定高度。例如。如果我的参数是 50,
这个非常简单的绘图命令创建了一个 11x11 像素的跟踪红色矩形: _sp.graphics.lineStyle( 1, 0xFF0000, 1, true, LineScaleMode.NORMAL
我每两秒从 NSTimer 函数调用 setNeedsDisplayInRect 一次。这工作得很好,并在随机位置绘制一个具有随机颜色的正方形。然而,在某些情况下,我想在 NSTimer 函数中绘制一
你好,我是新来的,也是 Java 编程的新手。最近我正在尝试在 JPanel 中编写绘图功能。问题是,当我添加新的绘制矩形(只需在 JRadiobutton 中选择绘制矩形,然后拖动空白区域)时,它工
我有一个放在WebView上方的UIView。在这个UIView中,我必须画图,但是应用程序因此错误而崩溃 在LibraryViewController.m中 self.drawView =[[Dr
我正在使用 Java 编写一个文件管理器。我需要将矩形选择添加到我的程序中,就像在 Windows 中一样(以便选择矩形内的多个文件)。我的问题是,每当我向 DrawRect 面板添加布局来放置图标时
我正在使用 UIBezierPath 创建自定义 View 绘画。每当状态根据服务器的响应发生变化时,我想更新自定义 View 填充颜色。我正在使用 fillColor属性作为参数来更新 View 的
我有一个名为 TestView 的 UIView 子类,它重写了 DrawRect ,如下所示: import Foundation import UIKit class TestView: UIVi
我的应用程序中有一个泄漏问题,我正在创建一个颜色的自动释放对象,但通过仪器说问题存在。 Malloc 1 04:12.221.102 32 UIKit +[UIColor allocW
我需要知道是否可以全局覆盖 UI 类的 drawRect。类似于“外观”类属性的东西。本质上,我需要为应用程序中的所有按钮使用我自己的绘图例程。最好我不想继承并且必须在 Storyboard 中的每个
我将绘制一条类似于下图中红色曲线的曲线(可以是贝塞尔曲线或我认为对我的目的最方便的任何曲线)。我想找到曲线上的点(illo 中的蓝点)。这些点很可能是曲线长度的相等部分的划分。 我能找到这些点吗?到目
我有两个 UIView 子类,比如“A 类”和“B 类”。类 A 在其初始化程序中具有代码 [self addSubview:instanceOfClassB];。而在 B 类的 drawRect:
我一直在使用 UITextField 的子类在我的文本字段末尾绘制一个“m3”字符串,但有些东西不起作用。我知道正在调用该方法,因为我已经使用 NSLog 对其进行了测试,我做错了什么? 我的类(cl
我有一个 UITableViewCell 的子类,我正在尝试在 drawRect 中画一条线。 有一个属性separatorColor在子类的标题中: @interface DXDecisionsTa
我将动画代码放在我的 UIView 子类的 drawRect 中。我真的在寻找类似 UIViewController 的 viewWillAppear 的东西。但我想使用 UIView 而不是 UIV
我是一名优秀的程序员,十分优秀!