Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java rectangle images

Tags:

java

swing

Hi I am a bit new to Java and also to programming and in order to get beter at both the language and programming I decided to build a Monopoly Game.

I should also mention that this is my first project ever as a programmer so my way of doing things may be very well the worst way.

I am using the Swing library and each Rectangle is drawn using graphics

I am building the layout using rectangles and I was wondering if there is a way to add diferent Images to each rectangle?

Thank you

like image 703
Nistor Alexandru Avatar asked May 18 '26 15:05

Nistor Alexandru


2 Answers

Create your panel with suitable layout, for ease I'd suggest you to use JPanels with borders rather than drawing rectangles.and follow these,

 image = ImageIO.read(new File(path));
  JLabel picLabel = new JLabel(new ImageIcon(image));

Yayy! Now your image is a swing component ! add it to a frame or panel or anything like you usually do! Probably need a repainting too , like

  jpanel.add(picLabel);
  jpanel.repaint(); 

Do this for as many pics you want :) happy coding...cheers :)

like image 115
COD3BOY Avatar answered May 20 '26 04:05

COD3BOY


It seems that you are trying to layout the board using Swing. If so, you can simply set the image icon of a JLabel.

JLabel label = new JLabel(new ImageIcon( image ));

However, personally I would design the Monopoly board as a JPanel and write custom paint methods for it. In that case, you could proceed by simply using the drawImage() method of the Graphics class.

like image 25
tskuzzy Avatar answered May 20 '26 04:05

tskuzzy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!