mouseClicked ImageIcon

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 478 Views - Last Post: 21 June 2020 - 11:41 AM Rate Topic: -----

#1 ronald1127   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 22-September 19

mouseClicked ImageIcon

Posted 20 June 2020 - 11:42 AM

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public class Image extends JFrame implements MouseListener
{
     
    public static void main(String[] args) 
        {
         
            JFrame frame = new JFrame();
             
            frame.setTitle(""); 
            frame.setSize(900, 600);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
             
            JPanel panel = new JPanel();
            frame.add(panel);
             
            JLabel jl = new JLabel(new ImageIcon("icons/Button.png"));
            frame.add(jl, BorderLayout.WEST);
             
        }   
     
    public void mouseClicked(MouseEvent me)
    {
         
        JLabel jl1 = new JLabel(new ImageIcon("icons/Femme-Femme.png"));
         
        jl1.setLocation(me.getX(), me.getY()+150);
     
    }
 
    public void mousePressed(MouseEvent me) {
     
    }
 
    public void mouseReleased(MouseEvent me) {
         
    }
 
    public void mouseEntered(MouseEvent me) {
         
    }
 
    public void mouseExited(MouseEvent me) {
         
    }
}


I can't finish this code
when i click on the image button, another image appears next to the first image
Thank you for your help

Is This A Good Question/Topic? 0
  • +

Replies To: mouseClicked ImageIcon

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:06 PM

Can you explain what you want the program to do?
What do you want to change about the what the program does when it currently executes?

Note: Image is the name of a java se class. It is better to give your class a unique name.

This post has been edited by NormR: 20 June 2020 - 12:08 PM

Was This Post Helpful? 1
  • +
  • -

#3 ronald1127   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 22-September 19

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:11 PM

when i click on the button image, the second image appears right next to the first, i hope i made no mistake because the second image does not appear.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:14 PM

Have you posted the code you are working with?
The posted code does not set the mouseListener
and it does not create an instance of the Image class.
Was This Post Helpful? 0
  • +
  • -

#5 ronald1127   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 22-September 19

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:25 PM

yes I published my code with which I work
I even published it on javaranch story of advancing with java
I have been learning on my own for two years and I have the impression that I have not progressed at all
Was This Post Helpful? 0
  • +
  • -

#6 idlewild   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 19-June 20

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:27 PM

MAybe you will want to take thşs pşece of code's approach below

consider getting out of static content first

then add the ImageIconn to a Jbutton and not a JLabel



import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Image extends JPanel implements ActionListener  
{JFrame frame = new JFrame();
JButton jl = new JButton("icons/Femme-Femme.png");
JButton next=new JButton("next");
public static void main(String[] args) 
{
	new Image().go();

}

private void go() {
	// TODO Auto-generated method stub


	frame.setTitle(""); 
	frame.setSize(900, 600);
	frame.setLocationRelativeTo(null);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);

	JPanel panel =this;
	frame.add(this);

	panel.add(jl);
	frame.add(panel);
	panel.add(jl);
	panel.add(next);
	next.setVisible(false);

	jl.addActionListener(this);	
}

@Override
public void actionPerformed(ActionEvent arg0) {

	// TODO Auto-generated method stub
	//next.setBounds(new Rectangle(10,20,10,20));
	this.add(next);
	next.setVisible(true);
	this.repaint();frame.repaint();next.repaint();
}   
}



Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:33 PM

Quote

I published my code with which I work

The posted code will not respond to the mouse. The first thing it needs is a listener added.
No instance of the class is created so there is no listener available.
Was This Post Helpful? 1
  • +
  • -

#8 idlewild   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 19-June 20

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:36 PM

in addition to NormR's advice

you may want to toggle the added image off/on upon ech click

just replace the

public void actionPerformed(ActionEvent arg0) {

	// TODO Auto-generated method stub
	//next.setBounds(new Rectangle(10,20,10,20));

	this.repaint();frame.repaint();next.repaint();
	if(!bool){
		this.add(next);
		//next.setVisible(true);
		next.setVisible(true);
		bool=!bool;
	}
	else if(bool){
		next.setVisible(false);
		bool=!bool;

	}
}   


and add a class instance variable initiated at

boolean bool=false;

Was This Post Helpful? 0
  • +
  • -

#9 ronald1127   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 22-September 19

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:42 PM

thanks for your replies, i will try to do something, i will keep you posted,
thank you
Was This Post Helpful? 0
  • +
  • -

#10 idlewild   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 19-June 20

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:49 PM

you can even use a JLAbel to hover on/off in the fashion of the code below

PS:Added is the MouseListener
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Image extends JPanel implements ActionListener, MouseListener  
{JFrame frame = new JFrame();
boolean bool=false;
JLabel jl = new JLabel("icons/Femme-Femme.png");
//JButton jl = new JButton("icons/Femme-Femme.png");

JButton next=new JButton("next");
public static void main(String[] args) 
{
	new Image().go();

}

private void go() {
	// TODO Auto-generated method stub


	frame.setTitle(""); 
	frame.setSize(900, 600);
	frame.setLocationRelativeTo(null);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);

	JPanel panel =this;
	frame.add(this);

	panel.add(jl);
	frame.add(panel);
	panel.add(jl);
	panel.add(next);
	next.setVisible(false);

	jl.addMouseListener(this);	
}

@Override
public void actionPerformed(ActionEvent arg0) {

	// TODO Auto-generated method stub
	//next.setBounds(new Rectangle(10,20,10,20));

	this.repaint();frame.repaint();next.repaint();
	if(!bool){
		this.add(next);
		//next.setVisible(true);
		next.setVisible(true);
		bool=!bool;
	}
	else if(bool){
		next.setVisible(false);
		bool=!bool;

	}
}

@Override
public void mouseClicked(MouseEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
	// TODO Auto-generated method stub
	if(arg0.getSource()==jl){
		//OptionPane.showMessageDialog(null, "true");
		this.repaint();frame.repaint();next.repaint();
		if(!bool){
			this.add(next);
			//next.setVisible(true);
			next.setVisible(true);
			bool=!bool;
		}
		else if(bool){
			next.setVisible(false);
			bool=!bool;

		}
	}

}

@Override
public void mouseExited(MouseEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
	// TODO Auto-generated method stub

}   
}

Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:53 PM

@idlewild - Please do not post full code. The idea is to help OPs learn how to write programs, not to provide full code.
Was This Post Helpful? 0
  • +
  • -

#12 ronald1127   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 22-September 19

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:55 PM

OK no worries
Was This Post Helpful? 0
  • +
  • -

#13 idlewild   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 19-June 20

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 12:58 PM

@NormR

thank you for your gentle caveat

i will sure keep in mind
Was This Post Helpful? 0
  • +
  • -

#14 ronald1127   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 22-September 19

Re: mouseClicked ImageIcon

Posted 20 June 2020 - 06:50 PM

 ImageIcon icon = new ImageIcon("icons/Button.png");
		    JLabel label = new JLabel();
		    frame.add(label);
		    label.setIcon(icon);
		    
		    
		}
	
	public void actionPerformed(ActionEvent ae) {
		
		ImageIcon icon1 = new ImageIcon("icons/Femme-Femme.png");
		JLabel label1 = new JLabel();
		label1.setIcon(icon1);
		
		//label1.addMouseListener(this);
		
		}

here is part of the code, after I don't go out anymore
Thank you for your help
Was This Post Helpful? 0
  • +
  • -

#15 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: mouseClicked ImageIcon

Posted 21 June 2020 - 03:43 AM

Please post all of your code. The code you just posted does not fit with the other code that was posted.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2