Hello,

I have a 800 x 800 board where I post several pictures by using the Image class and the drawImage() method. Each image is 100 x 100 in size. If I click at any location of this board, I can easily identify the top left coordinates(x,y) and the length and width of the square.

I have two questions:

a) After I click and the square has been identified, how do I find out if this square contains an image or is blank?

b) How can I set the color of any square that was clicked?

Thank you!

You should design a way to find out where each image is located on the board.
Simple way - use two dimensional array where element position in array it is it ID and coordinates
Advanced way - create specific image object that can hold various informations related to image like image x and y coordinates, colour options, image url, visible/hidden-true/false(like pexeso game when image is visible or turned face down)

As for b) it depends on what you doing, how you representing selection. However generally speaking drawing new rectangle above square with fill of new colour can be start...

Hello Peter,

Thanks for the reply.

I am trying to program a game of chess. 2D array sounds good. Will this be of type Image? If yes, do I simply check to see if the Image[][] is null or not to see if it contains an Image?

If I wish to change colors using 2D arrays, how do I change the color of any cell in that array?

Thank you!

You could use an EmptyCell object that renders itself in different colors under certain conditions, such as one color for mouse-over and another for "selected".

Hello Ezzaral,

Thank you for the reply!

Right now, I have the image of a board in a JFrame on which I have painted the chess peices using drawImage(topleft_x, topleft_y, length, width) method.

If I do not use a 2d array, is there a way I can check if a certain square in the chess board represented by (topleft_x, topleft_y, length, width) contains an Image variable or not?

If not, I plan to declare an Image 2D array of 8 x 8 size and then color the cells and add pictures accordingly. The problem for me then is :

a) What is the method I would need to use to color a cell in a 2d array of Image?
b) How do I display the entire board(2D array) once the coloring and the chess pieces have been added?

Thank you!

If I do not use a 2d array, is there a way I can check if a certain square in the chess board represented by (topleft_x, topleft_y, length, width) contains an Image variable or not?

No, not really because when you draw the image you are rendering those pixels onto some area in your graphics context. There is no reference back to where those pixels came from that you can refer to later - hence the need for the 2D reference array or a custom glyph object that keeps track of its own coordinates.

The 2D array is probably the easiest way to go. You could make a BoardTile component that extends JComponent and overrides paintComponent() to render either a chess piece or an empty background color depending on what is current at that spot in your grid.

Hello Ezzaral,

Makes a lot of sense. Thank you! I will mostly make a custom class like you suggested.

If possible, I have an unrelated question. For some reason, no matter what I try, I am not able to set the background color of an applet. Following is the code:

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

public class Tetris extends JApplet
{
  
    private Random random = new Random();
    
     public void init()
    {
     
      setBackground(new Color(random.nextInt(255), random.nextInt(255),  
      random.nextInt(255)));
    }
   }

Where am I going wrong here?

Thank you!

JApplet is different than Applet. It utilizes a content pane. Try

getContentPane().setBackground(new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255)));

Hello Ezzaral,

That works. Thank you! If I have any questions with the custom class for the chess game, would you mind if I post here?

Regards

Sure, no need to start a new thread if they are still related to the original question.

Hello,

Sounds good. Thank you!

Hello Ezzaral,

If I have a custom class (call it X) that extends from JPanel and I wish to display an Image, can the drawImage() method be used only in the paintComponent() method of X? Is it possible to get a Graphics2D object in the constructor of X and then use drawImage() to display an Image?

Thank you!

You can leave the drawImage() call in paintComponent() and set an Image reference variable in the class either with the constructor or through a setImage() method you create on the class.

Hello Ezzaral,

Thank you for the reply.

When you say, "set an Image reference variable in the class either with the constructor or through a setImage() method you create on the class", could you show me the syntax of that in a line or so?

Thank you!

Structurally like this

class GamePiece {
    Image image;

    public GamePiece(Image image) {
        this.image = image;
    }

    public void draw(Graphics g){
        g.drawImage(image, 0, 0, null);
    }
}

Hello Ezzaral,

Thank you! That was helpful.

Regards,
sciprog1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.