Link to home
Start Free TrialLog in
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

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial