-
Drawing on a Java JFrame
I need some help with drawing text or rectangles on a Java JFrame. How do you do it?
For an applet it is easy. You set your class to extend JApplet and then use the paint(Graphics g) method. The g object can be used to draw by casting it to a Graphics2D object.
I know you need to do something like getGraphics() to the object necessary to draw, but I can never seem to get it to work.
This is for a system that lets you input some values in to an applet, you click a button, and they should be plotted in a seperate JFrame. I have it working except for the plotting part, and to do this I need to be able to draw on a JFrame.
Thanks a lot!
-
As long as your class extends JFrame you are able to define a pain(Graphics g) method that can paint inside the frame. You don't need to cast it or anything.
Example:
class MyClass extends JFrame
{
public MyClass()
{//do something}
public void paint(Graphics g)
{
g.DrawRect(10,10,10,10);
}
}
If you can't get i working, try searching google using the keywords jframe drawrect & tutorial. Java is one of the best documented languages on the web!
-
Thanks proactive. I already knew about the paint(Graphics g) method but that does not draw allow me to draw data that is entered while using the program. I did a search and came across this link:
http://forum.java.sun.com/thread.jsp...essage=1643593
This points in the direction of putting an Image on the frame and working with the Image. I'm going to do some more research but the example does close to what I am trying to do. Thanks!