Results 1 to 4 of 4
- 03-14-2011, 03:58 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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
- 03-14-2011, 04:52 AM #2
Use code tags to post codes -- [code]CODE[/code] will display as
Java Code:CODE
Does it do what you want it to do?Java Code:oRect.repaint() //is this correct ?????
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
- 03-14-2011, 05:24 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
no output is being displayed in the frame when i call repaint()oRect.repaint()
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.g.drawRect(10,50,50,50);
- 03-14-2011, 09:45 AM #4
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
-
Inner class method call
By d915172 in forum New To JavaReplies: 3Last Post: 11-11-2010, 09:40 PM -
Trying to call a method from sub class
By TheNewGuy in forum New To JavaReplies: 4Last Post: 10-17-2010, 07:08 AM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
Unable to call paint() from inside method
By Y. Progammer in forum New To JavaReplies: 3Last Post: 03-01-2010, 12:47 PM -
How to call a method from another class?
By jboy in forum New To JavaReplies: 8Last Post: 09-09-2009, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks