Computer Science Canada

Graphics in GUI?

Author:  Ashi_Mashi2 [ Thu Dec 29, 2005 10:56 am ]
Post subject:  Graphics in GUI?

I want to make a Graphing calculator..so, I should somehow implement a Graphics object into GUI. Does anyone have any ideas?
I researched a little, found a paintComponent method....dont know how it works or how to implement it...thanks a lot

Author:  Ashi_Mashi2 [ Fri Dec 30, 2005 9:07 pm ]
Post subject: 

ok..can someone plz tell me what's wrong with this code?..thx

code:

import java.awt.*;
import javax.swing.*;

public class GraphTest
{
    public static void main (String[] args)
    {
        new GraphTest ();
    }


    public GraphTest ()
    {
        JFrame f = new JFrame ("Grpahing Test");
        f.setDefaultCloseOperation (3);
        Container c = f.getContentPane ();
        c.setLayout (new BorderLayout ());
        JPanel p = new JPanel ();
        p.add (new PaintComponenet ());
        c.add (p, BorderLayout.NORTH);
        f.setSize (200, 200);
        f.show ();
    }


    private class PaintComponenet extends JComponent
    {
        public void paintComponent (Graphics g)
        {
            g.drawOval (50, 50, 100, 100);
        }
    }
}

Author:  rizzix [ Sat Dec 31, 2005 12:58 am ]
Post subject: 

extend java.awt.Canvas and ovrride paint(Graphics g) and not paintComponent(Graphics g)

Author:  Ashi_Mashi2 [ Sat Dec 31, 2005 11:42 am ]
Post subject: 

thanks...do you mean like this:

code:

private class PaintComponenet extends java.awt.Canvas
    {
        public void paint (Graphics g)
        {
            g.drawOval (50, 50, 100, 100);
        }
    }

it still does not work Crying or Very sad

Author:  rizzix [ Sat Dec 31, 2005 7:23 pm ]
Post subject: 

add the component to BorderLayout.CENTER
Also, do not add unnecessary containers (in this case the extra JPanel).


: