Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: trying to change image by mouse click

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default 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.

    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);
     
     
    		}
     
    	}
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: trying to change image by mouse click

    but nothing happens.
    Is something supposed to happen? Can you explain what you want the code to do when a button is clicked?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: trying to change image by mouse click

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: trying to change image by mouse click

    ok thxs ill figure it out lol

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default 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
    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.

    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);
     
     
    		 }
    		 }
    		 }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    leonne (January 6th, 2013)

Similar Threads

  1. How to change color of JMenuItem when mouse is over it
    By Bagzli in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2012, 02:09 PM
  2. trying to draw a circle at the location of every mouse click
    By jopeters in forum AWT / Java Swing
    Replies: 0
    Last Post: October 18th, 2011, 05:24 PM
  3. Checkbox doesnt change when click on JTable
    By GodspeedPR in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 02:12 PM
  4. Replies: 7
    Last Post: June 26th, 2011, 11:16 AM
  5. [SOLVED] Change the size of an image
    By subhvi in forum Algorithms & Recursion
    Replies: 4
    Last Post: August 23rd, 2009, 11:44 PM