trying to change image by mouse click
Hey, i'm making a tic tac toe game and having some problems when the user clicks on a button. Theres no compile error just nothing happens and debugge mode is not really helpful.
I also tried making the buttons into an array but got some error, so just ditched that idea.
Right now, all I wanted it to do is when I click on a button, it changes to the image on an X but nothing happens.
Code :
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class tic extends JFrame {
private JButton a = new JButton();
private JButton b = new JButton();
private JButton c = new JButton();
private JButton d = new JButton();
private JButton e = new JButton();
private JButton f = new JButton();
private JButton g = new JButton();
private JButton h = new JButton();
private JButton i = new JButton();
private click slick = new click();
public tic(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,3));
p1.add(a);
p1.add(b);
p1.add(c);
p1.add(d);
p1.add(e);
p1.add(f);
p1.add(g);
p1.add(h);
p1.add(i);
JPanel p2 = new JPanel(new BorderLayout());
p2.add(new JTextField("test"),BorderLayout.SOUTH);
p2.add(p1,BorderLayout.CENTER);
add(p2);
a.addActionListener(new clicks());
b.addActionListener(new clicks());
c.addActionListener(new clicks());
d.addActionListener(new clicks());
e.addActionListener(new clicks());
f.addActionListener(new clicks());
g.addActionListener(new clicks());
h.addActionListener(new clicks());
i.addActionListener(new clicks());
}
public static void main(String[] args) {
tic frame = new tic();
frame.setTitle("test");
frame.setSize(400, 250);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class clicks implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
slick.x();
}
}
class click extends JPanel{
private ImageIcon imagei = new ImageIcon("C:/Users/chris/Desktop/x.JPG");
private ImageIcon imageii = new ImageIcon("C:/Users/chris/Desktop/circle.JPG");
Image image;
public Image x(){
Image image = imagei.getImage();
return image;
}
public Image o(){
Image image = imageii.getImage();
return image;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0,this);
//g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
}
Re: trying to change image by mouse click
Quote:
but nothing happens.
Is something supposed to happen? Can you explain what you want the code to do when a button is clicked?
Re: trying to change image by mouse click
o lol, well I wanted a image on an X to appear on the button,but I think might know how to fix it, going to see later
Re: trying to change image by mouse click
Quote:
wanted a image on an X to appear on the button
The code needs to get a reference to the button and call one of its methods.
Re: trying to change image by mouse click
ok thxs ill figure it out lol
Re: trying to change image by mouse click
EDIT: ok I figured it out, I did something similar to what I said here with some tweaks.
ok here is what I got so far, I was able to change the image on the button, but I have a question about the other buttons.
does each button need its own ActionListener class or can I just have one? right now, no mater what button you press, the first button changes images. Is there a way that
Code :
class CirclePanel extends JPanel {
private int radius = 5;
public void x() {
a.setIcon(new ImageIcon(image));
can detect which button is clicked and change a.setIcon(new ImageIcon(image)); to like b.setIcon(new ImageIcon(image)); ?
I was thinking of something like this
a.addActionListener(new EnlargeListener(a)); where it sends the variable to public void x(xx) and then have like
this.x = xx;
xx.setIcon(new ImageIcon(image));
or something.
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mouse extends JFrame {
private ImageIcon imagei = new ImageIcon("C:/Users/chris/Desktop/x.JPG");
private ImageIcon imageii = new ImageIcon("C:/Users/chris/Desktop/circle.JPG");
Image image = imagei.getImage();
Image imagee = imageii.getImage();
private JButton a = new JButton();
private JButton b = new JButton();
private JButton c = new JButton();
private JButton d = new JButton();
private JButton e = new JButton();
private JButton f = new JButton();
private JButton g = new JButton();
private JButton h = new JButton();
private JButton i = new JButton();
private CirclePanel canvas = new CirclePanel();
public mouse() {
JPanel p1 = new JPanel(); // Use the panel to group buttons
p1.setLayout(new GridLayout(3,3));
p1.add(a);
p1.add(b);
p1.add(c);
p1.add(d);
p1.add(e);
p1.add(f);
p1.add(g);
p1.add(h);
p1.add(i);
JPanel p2 = new JPanel(new BorderLayout());
p2.add(new JTextField("test"),BorderLayout.SOUTH);
p2.add(p1,BorderLayout.CENTER);
add(p2);
//this.add(canvas, BorderLayout.CENTER); // Add canvas to center
//this.add(p1, BorderLayout.SOUTH); // Add buttons to the frame
a.addActionListener(new EnlargeListener());
b.addActionListener(new EnlargeListener());
c.addActionListener(new EnlargeListener());
d.addActionListener(new EnlargeListener());
e.addActionListener(new EnlargeListener());
f.addActionListener(new EnlargeListener());
g.addActionListener(new EnlargeListener());
h.addActionListener(new EnlargeListener());
i.addActionListener(new EnlargeListener());
}
/** Main method */
public static void main(String[] args) {
JFrame frame = new mouse();
frame.setTitle("ControlCircle2");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
class EnlargeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
canvas.x();
}
}
class CirclePanel extends JPanel {
private int radius = 5;
public void x() {
a.setIcon(new ImageIcon(image));
//radius++;
//repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
}
Re: trying to change image by mouse click
The Event object passed to the action listener method has a reference to the component (JButton) that was clicked. Get that reference, cast it to a JButton and you can access that button's methods as needed.
Don't hard code a reference in the x() method. Pass it as an arg to the method.
Or your solution of giving the listener the reference when it is created should work.