Solved

adding JButton to painted image in JPanel

Posted on 2010-11-11
4
488 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 250 total points
Comment Utility
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now