有什么方法可以在Swing中以亚像素精度绘制线条?(Is there any way to draw lines with subpixel precision in Swing?)
这个问题之前已经被问过了 ,但答案并不能解决问题,所以我再问一次。
有人建议,不要使用
g2.drawLine
,你可以使用g2.draw(line)
,其中line
是Line2D.Double
。 但是,从屏幕截图中可以看到,线条仍然绘制得像一个整数像素(每组10行完全平行)。import javax.swing.*; import java.awt.*; import java.awt.geom.Line2D; public class FrameTestBase extends JFrame { public static void main(String args[]) { FrameTestBase t = new FrameTestBase(); t.add(new JComponent() { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for (int i = 0; i < 50; i++) { double delta = i / 10.0; double y = 5 + 5*i; Shape l = new Line2D.Double(5, y, 200, y + delta); g2.draw(l); // Base case, with ints, looks exactly the same: // g2.drawLine(5, (int)y, 200, (int)(y + delta)); } } }); t.setDefaultCloseOperation(EXIT_ON_CLOSE); t.setSize(220, 300); t.setVisible(true); } }
那么在Swing中不可能正确渲染那些不完全在像素上结束的行?
This question has been asked before, but the answers don't solve the problem, so I ask it again.
It was suggested that instead of using
g2.drawLine
, you could useg2.draw(line)
, whereline
is aLine2D.Double
. However, as you can see from the screenshot, the lines are still drawn as if they ended on an integer pixel (each set of 10 lines is exactly parallel).import javax.swing.*; import java.awt.*; import java.awt.geom.Line2D; public class FrameTestBase extends JFrame { public static void main(String args[]) { FrameTestBase t = new FrameTestBase(); t.add(new JComponent() { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for (int i = 0; i < 50; i++) { double delta = i / 10.0; double y = 5 + 5*i; Shape l = new Line2D.Double(5, y, 200, y + delta); g2.draw(l); // Base case, with ints, looks exactly the same: // g2.drawLine(5, (int)y, 200, (int)(y + delta)); } } }); t.setDefaultCloseOperation(EXIT_ON_CLOSE); t.setSize(220, 300); t.setVisible(true); } }
So is it impossible in Swing to correctly render lines that don't end exactly on a pixel?
原文:https://stackoverflow.com/questions/12415640
最满意答案
相关问答
更多-
xcode,如何使用storyboard参考连接内部视图控制器?(xcode, how to use storyboard reference connect inner view controller?)[2023-05-25]
步骤1:将id添加到视图控制器中,该视图控制器位于故事板内部(您希望与segue连接)。 步骤2:在storyboard参考中,添加要连接的VC的ID。 Step 1: Add id to the view controller which is inner a storyboard(you want to the segue connect with). Step 2: In storyboard reference, add the id of the VC you want to connect wi ... -
更新(2016年1月6日) :我只是想快速提到,使用Storyboard引用非常简单,将帮助您以更干净,更可维护的方式使用故事板。 一个很好的用例是例如具有多个选项卡的UITabBarController 。 只需为每个标签创建一个故事板,并在Main.Storyboard链接中使用Storyboard引用链接到那些单独的故事板。 用法非常简单:创建故事板参考后,您只需要将要链接的单个故事板的文件名 ,并将该视图控制器设置在该个别故事板中。 而已! :) 以下是现在的原始答案,我给了@ YogeshSuth ...
-
在Xcode中创建一个plist文件,将其根目录设置为数组并将文本添加到其中。 使用此函数从plist中读取(假设您将文件命名为randomstrings.plist): 。 NSString *path = [[NSBundle mainBundle] pathForResource: @"randomstrings" ofType:@"plist"]; NSArray *randomStrings = [NSArray arrayWithContentsOfFile: ...
-
如何在故事板中将视图控制器连接到其委托?(How can I connect a view controller to its delegate in a storyboard?)[2023-08-28]
您的委派决定是正确的模式 - 只对错误的视图控制器。 当用户从PhotoScanViewController保存照片时,让所有数据数据委托给MainScreenViewController。 现在您已拥有数据,当用户点击“列表”按钮时,将新获取的数据提供给PhotoListViewController。 这应该适合你。 以下是一些可以帮助您的代码: class ImageScannerViewController { var delegate:ImageScannerViewControllerDe ... -
您可以通过标签栏控制器访问它。 从标签栏控制器中嵌入的任何控制器,您可以得到如下参考: UIViewController *controller = self.tabBarController.viewControllers[4]; 要设置字段,而不是尝试从另一个控制器设置它们,只需传递#5控制器需要自己执行的数据。 由于#5的视图在第一次查看之前不会被加载,因此您无法从另一个控制器修改其视图。 因此,在ProfileViewController中,创建和整数属性,让我们将其称为fieldValues,并 ...
-
Swift StoryBoard参考segue到导航控制器而不是Root(Swift StoryBoard Reference segue to Navigation controller not Root)[2022-05-02]
在Storyboard中,一个特定的View Controller 可以链接到多个导航结构。 因此,您可以将Controller B作为多个导航控制器的一部分。 在故事板“SB_A”中创建一个到故事板“SB_B”的segue,但不是在SB_B中转到“默认”VC,而是转到标识为“FromSBA”的VC - 在这种情况下,“FromSBA”是第二个导航控制器里面SB_B,其根VC是Controller B In your Storyboard, a particular View Controller can ... -
在xcode中添加和控制storyboard中的视图控制器(Adding and controlling view controllers in storyboard in xcode)[2024-01-23]
1a)在xcode窗口的右上角,有一个燕尾服图标。 这是助理编辑。 选择故事板,选择ViewController,然后选择助理编辑器。 将出现第二个编辑器。 确保它正在编辑ViewController.h。 按控制并从故事板中的进度视图拖动到ViewController接口定义内的一个点。 (在看起来像这样的行之后: @interface ViewController : UIViewController )系统将提示您输入插座名称。 输入类似“progressView”(没有引号)的内容。 1b)参见下文 ... -
好的,解决了。 我可以在Utilities面板中连接连接检查器的插座。 Ok, solved. I'm able to connect outlet from connection inspector in Utilities panel.
-
有没有办法从故事板中完全删除/删除未使用的视图控制器? 是: 打开故事板 选择您不需要的视图控制器 按Delete键。 还有Edit-> Delete菜单命令,它与按Delete键相同。 Is there a way to completely remove/delete an unused view controller from storyboard? Yes: Open the storyboard Select the view controller that you don't need Pres ...
-
如何在视图控制器StoryBoard Xcode之间发送数据(How to Send data between view controllers StoryBoard Xcode)[2021-11-11]
使用此方法在所有视图控制器中传递属性 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"ShowNewVC"]) { NextViewController *nextVC = (NextViewController *)[segue destinationViewController]; nex ...