Solved

adding JButton to painted image in JPanel

Posted on 2010-11-11
4
500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

751 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