因为我想调用不同类的绘图方法,并在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);
}
}以上是类的定义及其绘图方法。
在另一个班级:
我调用绘图方法,并且确实会调用它,因为System.out.println(“调用绘制方法?\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);
}
}to_draw.setColor(Color.BLACK);to_draw.drawLine(31,41,131,768);//没有工作.?
“调用不同类的绘图方法并在JPanel上绘制不同的图形,因此我需要将绘图板(JPanel或其他什么)作为参数,将其传递给我的绘图方法。”
发布于 2011-11-07 13:59:52
您应该重写面板的paintComponent(Graphics g)方法。在方法中调用super.paintComponent(g),然后调用draw()方法。
https://stackoverflow.com/questions/8037006
复制相似问题