I hope this is my last time asking this question; i am making a tile based game
ID: 674152 • Letter: I
Question
I hope this is my last time asking this question; i am making a tile based game like Carcassonne, i need help figuring out how to draw the pictures;make the tile look something like the pieces of the game carcassonne. github dosen't really tell me how they put the grapics onto the tile. i am really at lost. can you put me on the right direction, act like you are a beginner at something-like coding in java- now think what website or video will help/show you how to make grapics/pictures on the tile. like what you need to show the grapics, how to draw the grapics, how to put the grapics on the on the tile etc. Tell me what i need, like do i need to use jframe, what type of program do i need to draw the tiles, then transfer it from there to eclipse etc. at least i need to be able to make one tile. suggestions are needed!! videos/ coding/websites is needed
Explanation / Answer
Sample Code:
/*********************************************************
* Program to display three cards randomly.In this *
* program a GridLayout class is used in order to setthe*
* images into Grid form.ImageIcon class is to get access*
* of images from the image Files. *
* Math.random() is to generate the random numbers, such *
* that image can be displayed randomly from the set of *
* cards. Each image is added to the JLabel Class. *
*********************************************************/
//importing the packages
import java.awt.*;
import javax.swing.*;
//Class DisplayCards extends the JFrame properties
public class DisplayCards extends JFrame
{
//constructor
public DisplayCards()
{
//setting a layout: GridLayout with 1 row, 3
//columns, with 5 space each
setLayout(new GridLayout(1, 3, 5, 5));
The ImageIcon class of array is initialized with size 54. In the below for loop, each image is assigned to the icon variable.
//declaraing an array of ImageIcon class
ImageIcon[] icon = new ImageIcon[54];
//initialising the array with an icon from the
//imagea/card directory
for (int i = 1; i < 54; i++)
{
icon[i] = new ImageIcon("image/card/" + (i)
+ ".png"); }
A random method of Math class is used. This method will generate a random number so that when the value is passed to icon variable. It picks the random image icon from the image cards.
//generating random numbers
int r = (int) (Math.random() * 54);
int q = (int) (Math.random() * 54);
int p = (int) (Math.random() * 54);
//adding the random image to JLabel
if (r == q && q == p && r == p)
{
add(new JLabel("THE ICONS ARE SAME",
JLabel.CENTER));
}
else
{
add(new JLabel(icon[r]));
add(new JLabel(icon[q]));
add(new JLabel(icon[p]));
}
}
The main method will set the size, title, visible and close operations for the frame.
//main method
public static void main(String args[])
{
DisplayCards cards = new DisplayCards();
//setting a title to a frame
cards.setTitle("Three Cards");
//setting frame size
cards.setSize(300, 200);
//exit frame on close
cards.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
//set the visiblility of frame
cards.setVisible(true);
}
}
Sample Output 1:
When the above program is executed, the following output is generated:
In the output, the three distinct cards are displayed randomly.
Sample Output 2:
In this output, when a program is executed, the following random and distinct cards are displayed:
Sample Output 3:
In this output, when the program is once again executed, the following output is generated:
From the above outputs, each output generated is distinct and random.