绘图和 2D API 入门
我正在尝试将一些绘图功能集成到我的程序中。
我有一个 JLabel,上面设置了图像。
我想编写一个方法来返回我的图像:
public Graphics getImage(){
Graphics g = currentImage;
return g
}
但我不知道如何将其从 JLabel 转换为图形对象。然后作为一个简单的测试,我想在这个图像上画一条线:
public void paint(Graphics g) {
g.drawLine(20, 500, 700, 600);
}
一些帮助开始这将是伟大的。
I'm trying to integrate some drawing functionality into my program.
I have a JLabel that has an image set on it.
I want to write a method to return my image:
public Graphics getImage(){
Graphics g = currentImage;
return g
}
But I don't know how to convert it from a JLabel to a graphics object. Then as a simple test I want to draw a line on this image:
public void paint(Graphics g) {
g.drawLine(20, 500, 700, 600);
}
Some help with getting started on this would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重写
JLabel
的paintComponent(Graphics g)
方法并将所有绘图代码放在那里。Override
paintComponent(Graphics g)
method ofJLabel
and place all the drawing code there.创建图像的副本 (
BufferedImage image2..
) 并将image2
放入标签中。当您需要绘制时,为
Graphics
对象调用image2.getGraphics()
,或为Graphics2D 调用
image2.createGraphics()
对象。有关创建和使用图像的示例,请参阅此答案。
Create a copy of the image (
BufferedImage image2..
) and putimage2
in the label.When you need to draw, call
image2.getGraphics()
for aGraphics
object, orimage2.createGraphics()
for aGraphics2D
object.See this answer for examples of creating and using images.