Solved

adding JButton to painted image in JPanel

Posted on 2010-11-11
4
498 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:hipal
  • 2
4 Comments
 

Author Comment

by:hipal
ID: 34117936
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

0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 34117989
you don't add the button to the image directly, you add the component that is responsible for painting the image.
0
 

Author Comment

by:hipal
ID: 34118005
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 250 total points
ID: 34118451
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question