无法让 Java2D 将简单的线条渲染到 Graphics 对象
因为我想调用不同类的绘图方法并在JPanel上绘制不同的图形,所以我需要将绘图板(JPanel或其他东西)作为参数,将其传递给我的绘图方法。(但我不知道是否可以做或不做......这里的情况是另一种尝试......)
这是我的实现部分。
我创建一个类class_diagram,如下所示:
public class class_diagram extends Object
{
private final int width = 60;
private final int height = 80;
private final int first_separate_line_distance= 30;
private final int second_separate_line_distance= 55;
private int left_up_x = 0;
private int left_up_y = 0;
public void setLeft_up(int left_up_x,int left_up_y)
{
this.left_up_x = left_up_x;
this.left_up_y = left_up_y;
}
//private Graphics to_draw ;
//private JPanel place_to_draw;
public class_diagram()
{
// instance variable "point to" the reference which was passed in.
}
@Override
//the parameters stands for the left-up point's coordinate.
public void draw(Graphics to_draw) {
// TODO Auto-generated method stub
System.out.println("Call draw method?\n");
to_draw.setColor(Color.BLACK);
to_draw.drawLine(31, 41, 131, 768);
}
}
上面是类定义及其绘制方法。
在另一个类中:
我调用了draw方法,它确实被调用了,因为 System.out.println("调用draw方法?\n");在该绘制方法中向我显示了该消息。
尽管如此!!!我的 JPanel 上的图画……让我精疲力竭。 因为我已经尝试了至少 4-5 种方法......
import java.awt.BorderLayout;
public class UML_Editor_13 extends JFrame {
private Edit_panel canvas = new Edit_panel();
public static void main(String[] args) {
UML_Editor_13 frame = new UML_Editor_13();
frame.setVisible(true);
Graphics m= frame.canvas.getGraphics();
Object n = new class_diagram();
n.draw(m);
}
}
请有人告诉我为什么这一行“Graphics m=frame.canvas.getGraphics();” 不起作用...如果 m 引用画布,为什么
to_draw.setColor(Color.BLACK); to_draw.drawLine(31, 41, 131, 768); // 不起作用...?
任何其他方法来满足我的要求:
“调用不同类的绘图方法并在JPanel上绘制不同的图形,所以我需要将绘图板(JPanel或其他东西)作为一个参数,将其传递给我的绘图方法。”
Because I want to invoke different classes's drawing method and draw different graphs on the JPanel so I need to take the drawing plate(JPanel or something) as an argument, passing it to my drawing method.(But I don't know if I can do that or not.... The case here is another try...)
Here is my part of the implementation.
I create a class class_diagram as follows:
public class class_diagram extends Object
{
private final int width = 60;
private final int height = 80;
private final int first_separate_line_distance= 30;
private final int second_separate_line_distance= 55;
private int left_up_x = 0;
private int left_up_y = 0;
public void setLeft_up(int left_up_x,int left_up_y)
{
this.left_up_x = left_up_x;
this.left_up_y = left_up_y;
}
//private Graphics to_draw ;
//private JPanel place_to_draw;
public class_diagram()
{
// instance variable "point to" the reference which was passed in.
}
@Override
//the parameters stands for the left-up point's coordinate.
public void draw(Graphics to_draw) {
// TODO Auto-generated method stub
System.out.println("Call draw method?\n");
to_draw.setColor(Color.BLACK);
to_draw.drawLine(31, 41, 131, 768);
}
}
The above the is the class definition and its' drawing method.
And in the another class:
I call the draw method, and it indeed be invoked, because
System.out.println("Call draw method?\n"); in that draw method shows the message to me.
Nevertheless!!! The drawing on my JPanel... It wore me out.
Because I have tried at least 4-5 methods....
import java.awt.BorderLayout;
public class UML_Editor_13 extends JFrame {
private Edit_panel canvas = new Edit_panel();
public static void main(String[] args) {
UML_Editor_13 frame = new UML_Editor_13();
frame.setVisible(true);
Graphics m= frame.canvas.getGraphics();
Object n = new class_diagram();
n.draw(m);
}
}
Please somebody tell me why this line "Graphics m= frame.canvas.getGraphics();"
doesn't work... If m references to the canvas, whyto_draw.setColor(Color.BLACK);
to_draw.drawLine(31, 41, 131, 768); //didn't work...?Any other method to satisfy my requirements:
" invoke different classes's drawing method and draw different graphs on the JPanel so I need to take the drawing plate(JPanel or something) as an argument, passing it to my drawing method."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该重写面板的
paintComponent(Graphics g)
方法。在方法中调用super.paintComponent(g)
,然后调用draw()
方法。You should override the panel's
paintComponent(Graphics g)
method. In the method callsuper.paintComponent(g)
and then yourdraw()
method.