在java中绘制图形(netbeans ide)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1974041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing graphics in java (netbeans ide)
提问by csiz
I created a new JApplet form in netbeans:
我在 netbeans 中创建了一个新的 JApplet 表单:
public class UI extends javax.swing.JApplet {
//generated code...
}
And a jpanel in design mode named panou:
还有一个名为 panou 的设计模式 jpanel:
// Variables declaration - do not modify
private javax.swing.JPanel panou;
How do I get to draw a line on "panou"? I've been searching for this for 5 hours now so a code snippet and where to place it would be great. (using Graphics2D preferably)
如何在“panou”上画一条线?我已经搜索了 5 个小时,所以代码片段和放置它的位置会很棒。(最好使用 Graphics2D)
Edit: its getting really frustrating, I tried this (big chunk of code) Edit2: thanks to Martijn I made it work:)
编辑:它变得非常令人沮丧,我尝试了这个(大块代码) Edit2:感谢 Martijn 我让它工作了:)
采纳答案by Martijn Courteaux
- Go to design mode
- Right Click on the panel "panou"
- Click "Costumize code"
- In the dialog select in the first combobox "costum creation"
- add after
= new javax.swing.JPanel()
this, so you see this:
- 进入设计模式
- 右键单击面板“panou”
- 点击“装饰代码”
- 在对话框中选择第一个组合框“costum creation”
- 在
= new javax.swing.JPanel()
这之后添加,所以你会看到:
panou = new javax.swing.JPanel(){
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Do the original draw
g.drawLine(10, 10, 60, 60); // Write here your coordinates
}
};
Make sure you import java.awt.Graphics
.
确保您导入java.awt.Graphics
.
The line that you will see is always one pixel thick. You can make it more "line" by doing the following:
您将看到的线条始终为 1 个像素粗。您可以通过执行以下操作使其更“行”:
Create this method:
创建此方法:
public static final void setAntiAliasing(Graphics g, boolean yesno)
{
Object obj = yesno ? RenderingHints.VALUE_ANTIALIAS_ON
: RenderingHints.VALUE_ANTIALIAS_OFF;
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, obj);
}
And add after super.paintComponent(g);
(in your costum creation) this:
并在super.paintComponent(g);
(在您的服装创作中)添加以下内容:
setAntiAlias(g, true);
Edit
编辑
What you are doing wrong is: you paint the line once (by creating the frame).
When you paint the line the frame is also invisible. The first draw is happening when the frame becomes visible. The frame will be REpainted, so everything from the previous paint will disapear.
Always you resize the frame, everything will be repainted. So you have to make sure each time the panel is painted, the line also is painted.
你做错了什么:你画了一次线(通过创建框架)。当您绘制线条时,框架也是不可见的。第一次绘制发生在框架变得可见时。框架将被重新绘制,因此之前绘制的所有内容都将消失。
总是调整框架的大小,一切都将重新粉刷。因此,您必须确保每次绘制面板时,线条也已绘制。
Martijn
马丁
回答by coobird
To do custom painting in a JPanel
, one would need to make a subclass of a JPanel
, and then overload the paintComponent
method:
要在 a 中进行自定义绘制,JPanel
需要创建 a 的子类JPanel
,然后重载该paintComponent
方法:
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
// Perform custom painting here.
}
}
In the example above, the MyPanel
class is a subclass of JPanel
, which will perform whatever custom painting is written in the paintComponent
method.
在上面的示例中,MyPanel
该类是 的子类JPanel
,它将执行paintComponent
方法中编写的任何自定义绘画。
For more information on how to do custom painting in Swing components, Lesson: Performing Custom Paintingfrom The Java Tutorials have some examples.
有关如何在 Swing 组件中进行自定义绘制的更多信息,Java 教程中的课程:执行自定义绘制提供了一些示例。
If one wants to do painting with Java2D (i.e. using Graphics2D
) then one could do some painting on a BufferedImage
first, then draw the contents of the BufferedImage
onto the JPanel
:
如果你想用 Java2D 进行绘画(即使用Graphics2D
),那么你可以先做一些绘画BufferedImage
,然后将 的内容绘制BufferedImage
到JPanel
:
class MyPanel extends JPanel {
BufferedImage image;
public MyPanel() {
Graphics2D g = image.createGraphics();
// Do Java2D painting onto the BufferedImage.
}
public void paintComponent(Graphics g) {
// Draw the contents of the BufferedImage onto the panel.
g.drawImage(image, 0, 0, null);
}
}
Further reading:
进一步阅读:
回答by Martijn Courteaux
I just want to know whether this method can work with netbean IDE ??
我只是想知道这种方法是否可以与 netbean IDE 一起使用??