Results 1 to 4 of 4
  1. #1
    chidurihan is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Call paint method from another class using actionevent

    Hello!
    i just want to ask how to call paint() method from another class to draw different shapes when a certain button is clicked in the same frame.

    class Shapes extends JFrame implements ActionListener
    //components
    Jpanel pane=new JPanel();

    public Shapes(){
    pane.setLayout(null);
    //add components
    setContentPane(pane);
    }
    public static void main(String[]args){
    new Shapes
    ......
    }

    public actionPerforemd(ActionEvent e){
    if(e.getSource==btnRect){
    drawRect oRect=new drawRect();
    oRect.repaint() //is this correct ?????
    }
    }
    }
    class drawRect extends JPanel{
    public void paint(Graphics g){
    g.drawRect(10,50,50,50);
    }
    }

    any help will be appreciated. tnx

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default

    Use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    Java Code:
    oRect.repaint() //is this correct ?????
    Does it do what you want it to do?

    Any amount of repainting won't make a difference since your paintComponent(...) override is hardcoded to draw a rectangle at [10, 50, 50, 50] and nothing else.

    db

  3. #3
    chidurihan is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    oRect.repaint()
    no output is being displayed in the frame when i call repaint()
    g.drawRect(10,50,50,50);
    I just tried to put a constant value for the drawRect() for me to determine if the object will display when button is clicked. But still no shapes is being displayed.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default

    1. Did you ever attempt to add the panel to the frame?
    2. Why is a new panel constructed in the ActionListener's actionPerformed?

    I guess you need to start here:
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Other recommended reading:
    SSCCE : Java Glossary

    db

Similar Threads

  1. Inner class method call
    By d915172 in forum New To Java
    Replies: 3
    Last Post: 11-11-2010, 10:40 PM
  2. Trying to call a method from sub class
    By TheNewGuy in forum New To Java
    Replies: 4
    Last Post: 10-17-2010, 08:08 AM
  3. Replies: 2
    Last Post: 05-01-2010, 09:59 AM
  4. Unable to call paint() from inside method
    By Y. Progammer in forum New To Java
    Replies: 3
    Last Post: 03-01-2010, 01:47 PM
  5. How to call a method from another class?
    By jboy in forum New To Java
    Replies: 8
    Last Post: 09-09-2009, 08:29 AM

Posting Permissions

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