Reputation: 2137
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, why
to_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."
Upvotes: 0
Views: 129
Reputation: 57381
You should override the panel's paintComponent(Graphics g)
method. In the method call super.paintComponent(g)
and then your draw()
method.
Upvotes: 2