Calling the paint method via button click?
Page 1 of 19 Replies - 6608 Views - Last Post: 09 February 2011 - 07:39 PM
#1
Calling the paint method via button click?
Posted 09 February 2011 - 06:45 PM
Replies To: Calling the paint method via button click?
#2
Re: Calling the paint method via button click?
Posted 09 February 2011 - 06:47 PM
#3
Re: Calling the paint method via button click?
Posted 09 February 2011 - 06:54 PM
If you use another class (even inner) you will have to pass your JPanel as parameter to your Listener constructor
class MyClass implements ActionListener {
JPanel panel;
MyClass() {
panel = new JPanel();
....
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// easy I can see panel
panel.repaint();
}
}
with inner class
class MyClass {
MyClass() {
JPanel panel = new JPanel();
....
MyListener ml = new Listener(panel);
button.addActionListener(ml);
}
class MyListener implements ActionListener {
JPanel panel;
// constructor
MyListener(JPanel panel) {
this.panel = panel; // save panel to repaint
}
public void actionPerformed(ActionEvent e) {
// use JPanel saved when instance was built
panel.repaint();
}
}
}
This post has been edited by pbl: 09 February 2011 - 06:56 PM
#4
Re: Calling the paint method via button click?
Posted 09 February 2011 - 06:56 PM
Edit:
pbl, if i call the repaint all it will do is call the current paint method and not change which image that is drawn
This post has been edited by Dev1462: 09 February 2011 - 06:58 PM
#5
Re: Calling the paint method via button click?
Posted 09 February 2011 - 06:59 PM
japanir, on 09 February 2011 - 08:47 PM, said:
If you are in an inner class you need more than just repaint() you need access to the instance object of the class you ar in as I have showed with my code
This post has been edited by pbl: 09 February 2011 - 07:43 PM
Reason for edit:: My bad
#6
Re: Calling the paint method via button click?
Posted 09 February 2011 - 07:05 PM
Dev1462, on 09 February 2011 - 08:56 PM, said:
So you will have to overload paint() or paintComponent() and react based to a variable set in your actionPerformed
class MyPanel extends JPanel implements ActionListener {
boolean drawOtherImage = false;
MyPanel() {
button.addActionListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(drawOtherImage) {
// actionPerformed has been called
g.drawOtherImage(....
}
}
public void actionPerformed(ActionEvent e) {
drawOtherImage = true;
repaint();
}
}
#7
Re: Calling the paint method via button click?
Posted 09 February 2011 - 07:19 PM
pbl, on 09 February 2011 - 10:54 PM, said:
If you use another class (even inner) you will have to pass your JPanel as parameter to your Listener constructor
Sorry pbl, but not true. Non-static Inner classes can still access outer class fields and methods. There are just some nuances with anonymous inner classes and local variables.
As proof of concept, note how the inner class can still access the outer instance variable.
class Outer{
int x;
Outer(){
x = 3;
new Inner();
new Inner();
}
class Inner{
Inner(){
x++;
System.out.println(x);
}
}
public static void main(String[] args){new Outer();}
}
#8
Re: Calling the paint method via button click?
Posted 09 February 2011 - 07:30 PM
macosxnerd101, on 09 February 2011 - 09:19 PM, said:
Yes, and use it many times but in that case you will need to save the instance in a variable
May be I was not clear enough. In that case you need a reference to your instance in the instance variables
class MyPanel extends JPanel {
MyPanel() {
...
}
class MyListener extends ActionListener {
public void ActionPerformed(ActionEvent e) {
... cannot access MyPanel from here unless
}
}
class MyPanel extends JPanel {
MyPanel instance;
MyPanel() {
instance = this;
}
class MyListener extends ActionListener {
public void ActionPerformed(ActionEvent e) {
... OK can access instance here
}
}
You will have to save instance and instnce.repaint()
This post has been edited by pbl: 09 February 2011 - 07:40 PM
Reason for edit:: My bad
#9
Re: Calling the paint method via button click?
Posted 09 February 2011 - 07:31 PM
class Outer{
int x;
Outer(){
x = 3;
new Inner();
new Inner();
}
class Inner{
Inner(){
x++;
System.out.println(x);
print();
}
}
void print(){System.out.println("Testing");}
public static void main(String[] args){new Outer();}
}
#10
Re: Calling the paint method via button click?
Posted 09 February 2011 - 07:39 PM
This compiles
public class Radio extends JPanel {
Radio() {
}
class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
}
}
}
My apologies... and a few +1 for you and japanir I guess
|
|

New Topic/Question
Reply




MultiQuote






|