Results 1 to 3 of 3

Thread: Drawing on a Java JFrame

  1. #1
    Senior Member
    Join Date
    Nov 2002
    Posts
    186

    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!

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    472
    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!
    ---
    proactive

  3. #3
    Senior Member
    Join Date
    Nov 2002
    Posts
    186
    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!
    \"When you say best friends, it means friends forever\" Brand New
    \"Best friends means I pulled the trigger
    Best friends means you get what you deserve\" Taking Back Sunday
    Visit alastairgrant.ca

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •