Avatar of hipal
hipalFlag for United States of America

asked on 

adding JButton to painted image in JPanel

Hi Guys,

Thanks for all the help.

I am painting images on JPanel. (Like code snippet attached .. not complete though)

I am trying to add a JButton to each image such that when I am moving the image .. JButton should move with it ..

My class is currently extending JComponent. and images i am adding are instance of that class.

Code excerpt attached are from Photos class only which extends JComponent.
@Override
public void paint(Graphics g){
    	int w = getWidth();
	int h = getHeight();
	Graphics2D g2 = (Graphics2D) g;
	g2.addRenderingHints(new RenderingHints(
	RenderingHints.KEY_ANTIALIASING,	  
        RenderingHints.VALUE_ANTIALIAS_ON));
	AffineTransform transform = g2.getTransform();
	transform.concatenate(_transform);
	g2.setTransform(transform);
	g2.setClip(0, 0, w, h);
	g2.drawImage(_image, 0, 0, w, h, 0, 0, _image.getWidth(this), _image.getHeight(this), this);
	super.paintComponent(g);
	}

Open in new window

ProgrammingJava

Avatar of undefined
Last Comment
ksivananth
Avatar of hipal
hipal
Flag of United States of America image

ASKER

Constructor code attached here ... if that can be of any help
public Photo(File file) throws Exception {
	try {
		url = file.getPath();
		_image = ImageIO.read(file);
		int imageW = _image.getWidth(), imageH = _image.getHeight(), xPos, yPos;
		double scaleBy, wScale, hScale;
		wScale = MAX_IMAGE_WIDTH / imageW;
		hScale = MAX_IMAGE_HEIGHT / imageH;
		scaleBy = (wScale > 1 && hScale > 1 ? 1 : (wScale < hScale ? wScale: hScale));
		imageW = (int) (scaleBy * imageW);
		imageH = (int) (scaleBy * imageH);
		xPos = (int) (Math.random() * (Client.SCREEN_SIZE.width - imageW));
		yPos = (int) (Math.random() * (Client.SCREEN_SIZE.height - imageH));
		this.setBounds(xPos, yPos, imageW, imageH);
		} catch (IOException e) {
}		_id = ++_numPhotos;
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo