Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm a beginner in java and right now I'm trying to "solve" this puzzle:
http://zetcode.com/tutorials/javagamestutorial/puzzle/
Everything is clear to me, but I want to add something. As you can see, when you run it, the puzzle is already solved, so I want to add an algorithm that puts the parts of the image randomly. Any hints?
Thanks!
As you initialize the images you should put it into a List and then use Collections::shuffle to shuffle the List:
List<Image> croppedImages = new ArrayList<Image>(4 * 3);
for ( int i = 0; i < 4; i++) {
for ( int j = 0; j < 3; j++) {
croppedImages.add(createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(j*width/3, i*height/4,
(width/3)+1, height/4))));
}
}
Collections.shuffle(croppedImages);
Now in the loop where you initialize the pieces:
for ( int i = 0; i < 4; i++) {
for ( int j = 0; j < 3; j++) {
if ( j == 2 && i == 3) {
label = new JLabel("");
centerPanel.add(label);
} else {
button = new JButton();
button.addActionListener(this);
centerPanel.add(button);
button.setIcon(new ImageIcon(croppedImages.get(0)));
croppedImages.remove(0);
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
I need to sort by cakes made then if if equal by alphabetical order
I need this sorting method to firstly sort my cakesMade which is an int and then secondly by name if cakesmade is equal. The code is working for the cakesMade, I just cant seem to get the second sort by name to work. Could someone please help
public int bubbleSort (){
for (int i = 0; i < team.size(); i++){
for (int j = 0; j < team.size() - 1;j++ ){
Employee t1 = team.get(j);
Employee t2 = team.get(j+1);
if (t1.getCakesMade() < t2.getCakesMade()) {
team.set(j, t2);
team.set(j + 1, t1);
} else if (t1.getCakesMade() == t2.getCakesMade()) {
if (t1.getName().compareTo(t2.getName()) < 0){
team.set(j, t2);
team.set(j + 1, t1);
}
}
}
}
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am making a game-board which requires a grid of buttons. I have a method where I can put in the width and height of the board and draw it like this:
for (int i = 0; i < gameBoardWidth; i++) {
for (int j = 0; j < gameBoardHeight; j++) {
gameLayout.add(tiles[j], j, i);
}
}
The code above works perfectly fine but the problem is, I have an array of buttons. the size of the array is equal to the width * height, Each button within that array needs to have a unique ID incremented from 1 to n. Previously I made the board with this method
for(int i = 0; i < gameBoardDimension; i++) {
tiles[i] = new Button("");
tiles[i].setMinSize(gameButtonWidth, gameButtonHeight);
tiles[i].setId(Integer.toString(i));
Button btn = tiles[i];
btn.setOnAction(e -> {
turn = 1;
int ID = Integer.parseInt(btn.getId());
setMove(ID, turn, btn);
setAIMove();
});
}
But with the method above it is impossible to show them in a grid. How can I show the buttons in a grid so every button in the array has an ID of 1 to n with n being the size of the array?
Create an id as row (i) times width plus column (j) (plus 1 to start from 1 instead of 0) in the loop
for (int i = 0; i < gameBoardWidth; i++) {
for (int j = 0; j < gameBoardHeight; j++) {
Button b = new Button("");
//... other stuff for button
int id = i * gameBoardWidth + j + 1;
b.setId(Integer.toString(id));
//...
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My code is:
int diff = 0;
for (int i = 0; i<listOne.size(); i++)
{
for (int j = 0; j<listTwo.size(); j++)
{
if (listOne.get(i)-listTwo.get(j)>diff)
diff = listOne.get(i)-listTwo.get(j);
if (listTwo.get(j)-listOne.get(i)>diff)
diff = listTwo.get(j)-listOne.get(i);
}
}
return diff;
The task is to find the greatest difference between any two numbers in two inputted lists (the difference must be between a number from list one and a number from list two).
I cannot tell what is wrong with my code.
You may be missing Math.abs executed on the diff. Difference is an absolute value, so difference between 5 to 7 and 7 to 5 is same - 2.
int diff = 0;
for (int i = 0; i<listOne.size(); i++) {
for (int j = 0; j<listTwo.size(); j++) {
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
if (elementDiff>diff) {
diff = elementDiff;
}
}
}
return diff;
So both below lines will produce same results:
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
int elementDiff = Math.abs(listTwo.get(i)-listOne.get(j));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am using java
For example, the following situation:
First, the function is used as a print all possible strings.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
Output:
0,1,2,a,b,c,01,02,0a,0b,0c,10,11,12,1a ..................... ccccc. stop in length = 5
Then, I want to add a loop stopper to fetch the specified string.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
int loopStopper = 3;
Output:
a
Thank you
Use backtracking.
void print_all(char []ch,int maxLen){
for(int i=1;i<=maxLen;i++)
backTrack(ch,i,0,new char[i]);
}
void backTrack(char[] ch,int len,int k,char[] ans){
if(k==len){
System.out.print(new String(ans,0,len)+",");
return;
}
for(int i=0;i<ch.length;i++){
ans[k]=ch[i];
backTrack(ch,len,k+1,ans);
}
}
try this:
String alphabet = "012abc";// for example as your code "012abc"
char[] alphabetSet = alphabet.toCharArray();
int length = 5;
for (int i = 0; i < alphabetSet.length; i++) {
System.out.print(alphabetSet[i] + ",");
}
for (int j = 0; j <= length; j++) {
for (int i = 0; i < alphabetSet.length; i++) {
System.out.printf("%d%c,",j,alphabetSet[i]);
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to transform a number into array.
For example...
num = 7
to
list = [1,2,3,4,5,6,7]
How do I do that?
Try this :
int []list = new int[num];
for (int i = 0; i < list.length; i++) {
list[i] = i + 1;
}
Try this:
Integer[] ints = new Integer[x];
for (int i = 0; i < ints.length; i++) {
ints[i] = i + 1;
}
I'm assuming you're want to create an array from 1 to the number.
Code:
for(int i = 0; i < num; i++) {
list[i] = i+1;
}
In java you have to declare the variables first.
This means that variables are strongly typed.
You cannot convert a variable into an array.
However you can create a new variable that is an array.
int num = 7;
int[] arr = {num}; // arr is an array containing num