joelwenzel
Programmer
- Jun 28, 2002
- 448
I have created a component, obj, which extends JPanel. I will put many of these components in my applet. I have added a MouseListener to the obj class (but not too the main applet class) but it doesn't seem to do anything. When I click the mouse, it should update some text that is drawn on the component but instead nothing happens
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class piece2 extends JPanel implements MouseListener {
Image pic;
int numClicks;
JLayeredPane layeredpane;
piece2(String imgPath, int xPos,int yPos, JLayeredPane LP)
{
layeredpane = LP;
numClicks = 0;
Toolkit toolkit = Toolkit.getDefaultToolkit();
pic = toolkit.getImage(imgPath);
setBounds(xPos, yPos, 70,70);
}
public void paint (Graphics g)
{
g.drawImage(pic,0,0,this);
g.drawString("xx "+numClicks,40,30);
//paintComponents(g);
}
public void mousePressed(MouseEvent e) {
numClicks++;
}
public void mouseMoved (MouseEvent e) {} // ignore these events
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) { }
public void mouseExited(MouseEvent e) {}
}
And in the applet, I just say
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class puzzlev1 extends JApplet{
piece2 mpiece;
JLayeredPane layeredpane;
public void init() {
setSize(300,400);
layeredpane = new JLayeredPane();
layeredpane.setPreferredSize(new Dimension(300, 400));
mpiece = new piece2("img.jpg",0,0,layeredpane);
layeredpane.add(mpiece,JLayeredPane.PALETTE_LAYER);
getContentPane().add(mpiece);
}
}
Note...the layered pane is there because once I see that it is registering clicks, the clicking on the component is going to change its position in the layered pane
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class piece2 extends JPanel implements MouseListener {
Image pic;
int numClicks;
JLayeredPane layeredpane;
piece2(String imgPath, int xPos,int yPos, JLayeredPane LP)
{
layeredpane = LP;
numClicks = 0;
Toolkit toolkit = Toolkit.getDefaultToolkit();
pic = toolkit.getImage(imgPath);
setBounds(xPos, yPos, 70,70);
}
public void paint (Graphics g)
{
g.drawImage(pic,0,0,this);
g.drawString("xx "+numClicks,40,30);
//paintComponents(g);
}
public void mousePressed(MouseEvent e) {
numClicks++;
}
public void mouseMoved (MouseEvent e) {} // ignore these events
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) { }
public void mouseExited(MouseEvent e) {}
}
And in the applet, I just say
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class puzzlev1 extends JApplet{
piece2 mpiece;
JLayeredPane layeredpane;
public void init() {
setSize(300,400);
layeredpane = new JLayeredPane();
layeredpane.setPreferredSize(new Dimension(300, 400));
mpiece = new piece2("img.jpg",0,0,layeredpane);
layeredpane.add(mpiece,JLayeredPane.PALETTE_LAYER);
getContentPane().add(mpiece);
}
}
Note...the layered pane is there because once I see that it is registering clicks, the clicking on the component is going to change its position in the layered pane