5 Replies - 397 Views - Last Post: 28 January 2013 - 06:17 AM Rate Topic: -----

#1 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 645
  • Joined: 24-April 10

My java programe is not displaying all the images correctly

Posted 27 January 2013 - 10:44 AM

Hello,

I have a small problem with my program not displaying images correctly. I have small image(20x20 pixels, white image with a black border)which i want to display 100 time, in a 10x10 grid. Everything looks fine except for the right and bottom hand images which seem to cut off a bit of the image.

I tried to attach the image I am using but I got an error "Error The server returned an error during upload". Like a said it is just a small 20x20 image with a black border.


import java.awt.*;
import javax.swing.*;

public class userGrid{

	JFrame frame;

	JPanel imagePanel;

	GridPanel playerPanel[] = new GridPanel[100]; 	

	public void build(){

		frame = new JFrame();

		imagePanel = new JPanel();
		imagePanel.setLayout(new GridLayout(10,10));

		for(int i = 0; i < 100; i++){
			playerPanel[i] = new GridPanel();
			imagePanel.add(playerPanel[i]);		
		}


		frame.getContentPane().add(BorderLayout.CENTER, imagePanel);

		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(210,210);
		frame.setVisible(true);
	}

	public static void main(String [] args){
		
		userGrid gridTest = new userGrid();
		gridTest.build();
	}
}

class GridPanel extends JPanel{

	public void paintComponent(Graphics g){
		Image image = new ImageIcon("grid.png").getImage();
		g.drawImage(image,0,0,this);
	}
}



line 29 actually reads
frame.setSize(200,200);



I tried adding in extra space but still no joy.

Is This A Good Question/Topic? 0
  • +

Replies To: My java programe is not displaying all the images correctly

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,986
  • Joined: 20-September 08

Re: My java programe is not displaying all the images correctly

Posted 27 January 2013 - 11:02 AM

Why are you

a. adding 100 panels when you could be adding 100 images to one panel?
b. loading the image every time paintComponent is called?
Instead of

Quote

frame.setSize(210,210);

try
frame.pack();
Was This Post Helpful? 1
  • +
  • -

#3 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 645
  • Joined: 24-April 10

Re: My java programe is not displaying all the images correctly

Posted 27 January 2013 - 11:27 AM

View Postg00se, on 27 January 2013 - 11:02 AM, said:

Why are you

a. adding 100 panels when you could be adding 100 images to one panel?
b. loading the image every time paintComponent is called?
Instead of

Quote

frame.setSize(210,210);

try
frame.pack();


a. I need to know which image has been clicked and figured the easiest way to work it out would be to have "clickable" panels and work it out from there.
b. I see your point, it was code I just stole from another class of mine, I'll fix that up at another point.

I have tried packing it instead but I get the same result.

here is a link to the image: http://postimage.org/image/l5cpn9f0l/

Here is what i get: http://postimage.org/image/ewm5lr0l9/

Also, where are my manners, thanks for the reply!
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,986
  • Joined: 20-September 08

Re: My java programe is not displaying all the images correctly

Posted 27 January 2013 - 11:31 AM

Quote

a. I need to know which image has been clicked and figured the easiest way to work it out would be to have "clickable" panels and work it out from there.
It would still be better to avoid that. What i'd do is to add JLabel(yourIcon) instead to one panel. You can add the mouse listener to each label. You should find pack() works then
Was This Post Helpful? 1
  • +
  • -

#5 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 645
  • Joined: 24-April 10

Re: My java programe is not displaying all the images correctly

Posted 28 January 2013 - 04:37 AM

View Postg00se, on 27 January 2013 - 11:31 AM, said:

Quote

a. I need to know which image has been clicked and figured the easiest way to work it out would be to have "clickable" panels and work it out from there.
It would still be better to avoid that. What i'd do is to add JLabel(yourIcon) instead to one panel. You can add the mouse listener to each label. You should find pack() works then


So you mean add the one image to 100 labels and then add those labels to one panel?
Was This Post Helpful? 0
  • +
  • -

#6 ccdan  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 09-December 12

Re: My java programe is not displaying all the images correctly

Posted 28 January 2013 - 06:17 AM

Yeah, that's what you should do. You could create a two dimensional array to hold the JLabels (though one would also do) and set their individual properties, add listeners and so on...
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1