i am making a game and i trying to make bitmaps dissapear or appear anyway for some reason when i use paint as 1 object it works but when i add a array paint(fro each bitmap) it crashes
here is the lines i used with it:
//in MainActivity class
Paint[] paintanswer;
//in oncreate
paintanswer=new Paint[answerlength];
//in the draw func
for(int i=0;i<answerlength;i++){
paintanswer[i].setAlpha(answeralpha[i]);
canvas.drawBitmap(answerbitmapscaled[i],(float) (((cwidth/2)-((answerlength*answersize)/4)-answersize/4)+i*(answersize/2)),(float) (cwidth/2), paintanswer[i]);
the answer length is the length i get from other activity accordingly i set the array
An array in Java is just a container with a certain amount of slots for elements of a certain type. In your case a container that can store for example 20 Paint elements, however, it is completely empty. You have to fill the array with elements if you want to use them later on:
paintanswer = new Paint[answerlength];
for (int i = 0; i < answerlength; i++)
paintanswer[i] = new Paint();
Related
I'm making an autoclicking project in java for personal use, and using the following method to get coordinates of a click from a class that extends MouseAdapter. The click is being done on a JFrame.
int[] mouseCoordinates = new int[2]; //The coordinates of the click
mouseCoordinates = mouseListenerExample.getCoordinates();
final int[] baseCoordinates = mouseCoordinates; //The base coordinates (no click) which is this problem//
int[][] totalCoordinates = new int[4][2]; //An array that collects all coordinates of 4 mouse clicks
for (int i = 0; i < 4; i++){ //the goal is to get the coordinates of 4 clicks
while (mouseCoordinates[0] == baseCoordinates[0]){
mouseCoordinates = mouseListenerExample.getCoordinates(); //The problem occurs here: when mouseListenerExample.getCoordinates() changes, mouseCoordinates is changed, baseCoordinates is also changing, which it shouldnt, since there are no statements that say so.
if (mouseCoordinates[0] != baseCoordinates[0]) {
break;
}
}
totalCoordinates[i] = mouseListenerExample.getCoordinates();
mouseListenerExample.setCoordinates(baseCoordinates);
mouseCoordinates = baseCoordinates;
}
Is there some statement that is changing baseCoordinates that I am missing?
Your baseCoordinates variable has been declared final, and so it cannot change, but since it is an int[] or int-array, it is a reference variable, and so what cannot change is the reference itself, not the state of the reference, and so the ints held within the array can (and in your case -- do) change.
You're changing the values held by mouseCoordinates. Since the baseCoordinates refers to the exact same int[] object, then this will likewise change the values for baseCoordinates. Best to create a completely new int object for the final variable if you don't want it changed.
Do something like:
final int[] baseCoordinates = new int[mouseCoordinates.length];
System.arraycopy( mouseCoordinates, 0, baseCoordinates , 0, mouseCoordinates.length );
So after some looking around, i just replaced the arrays with individual ints, and added a print statement right before the break, and it made it work.
how can I make on a button press a new deep copy of a 2 dimensional array?
Basically I created a game field with buttons. The game is called sokoban and it's a puzzle. The player is moving from one button to the other with arrow keys on a fixed map (8x8 buttons).
I want to implement an undo function. So I thought that I just create a deep copy of the JButton array before each move and save it into a stack. So when I press the undo button it calls the pop function of my stack. The problem is that I need to declare and initialize another JButton[][] where I can save the game field to before each move. Since I want infinite possible moves and also undos it seems impossible to me. I can't declare and initalize infite diffrent JButton[][] arrays. Any idea on how I can solve that?
That's how I copy a 2d object array:
JButton[][] tempArray = new JButton[jbArray.length][jbArray[0].length];
for (int i = 0; i < getJbArray().length; i++) {
for (int j=0;j<getJbArray()[0].length;j++) {
tempArray[i][j]=jbArray[i][j];
}
}
movesStack.push(tempArray);
Unfortunately you can't clone swing components in general, as they do not implement the Cloneable interface. As I see it you have two options:
Create a new JButton inside your double loop and copy whatever properties (like alignment, color etc.) you have set to the new JButton
Write your own class that extends JButton and implement the Cloneable interface
The first way is somewhat of a hack and not very robust or reusable. The second way is much better practice. In this case you'll have to define how the deep copy is supposed to happen, and ensure that all relevant properties are copied over.
You've got the right idea. You're not quite going deep enough.
public JButton[][] copy(JButton[][] jbArray) {
JButton[][] tempArray = new JButton[jbArray.length][jbArray[0].length];
for (int i = 0; i < jbArray.length; i++) {
for (int j = 0; j < jbArray[0].length; j++) {
tempArray[i][j] = new JButton(jbArray[i][j].getText());
}
}
return tempArray;
}
Rather than copying JButtons, you should have a model that you use to set the JButtons. Maybe a ModelClass[][] array?
As per the title.
I have a 2D array with size of 1000 x 1000.
I tried adding in to a gridpane and it went out of memory(haha...)
I am just wondering what would be the best way to go about this?
Requirements:
1) All container for the items in the array has to be of same size
2) I would like to show colour being applied to selected position of the 2D array.
Any guide to the right direction is greatly appreciated.
Really new to JavaFX.
*Contemplating canvas but...
Code:
private String[][] dataFromTxtFile;
GridPane gridpane = new GridPane();
private void initialize() {
TextFileData txtFileData = new TextFileData();
//txtFileData.getTxtFileData() gets a [1000][1000] array
dataFromTxtFile = txtFileData.getTxtFileData();
//Gridpane.add(dataon the box, column, row)
for (int i =0; i<dataFromTxtFile.length;i++){
for (int j=0; j<dataFromTxtFile[i].length;j++){
Text data = new Text(dataFromTxtFile[i][j]);
System.out.println("HERE: "+dataFromTxtFile[i][j]);
gridpane.add(data,i,j);
}
}
}
I am trying to display the data on a 1000 x 1000 grid.
The items in it are just numbers.
If you are only storing numbers, use an Array of ints. It might still be not enough memory, but ints take up less memory than strings. Use Integer.parseInt("string") to convert from string to int.
I am trying to make a very simple grid in which a user can move through. Through my research i have concluded that the best way to achieve this would be using a two-dimensional array to represent the grid. However i am unsure of how to draw this array to a Jframe or Jpanel if i were to make an array of rectangles.
Many Stackoverflow questions seem to ask simmilar queries but unforunately i have found non that entirely explains how to draw a simple grid of rectangles.
You can make arrays out of everything. Notice how the way to implement an array is
*datatype* [] *arrayname* = new *datatype* [*lengthOfArray*];
Lets say the name of the class that contains the rectangles is RECTANGLE. So if you want an Array, that contains, lets say, 5 rectangles, it would look somewhat like that:
RECTANGLE [] rectangelArray = new RECTANGLE [5];
If you want to take that to a 2 dimensional Level, just add another bracket:
RECTANGLE [][] rectangelMatrix = new RECTANGLE [4][5];
Assuming that by "drawing to the JPanel" you mean that you want to put the rectangles onto the screen, you would then have a for-loop in a for-loop, that would for example call each rectangle to draw itself:
for(int i = 0; i<rectangleMatrix.length; i++){
for(int j = 0; j<rectangleMatrix[i].length; j++){
rectangleMatrix[i][j].draw();
}
}
rectangleMatrix.draw() calls a method that will draw the rectangle based on its coordinates and size. you could also in a similar fashion call a method that will read the information of each rectangle and then draw it based on that information. This will help you seperate between information and drawing purposes in your classes, which is always a good thing to do:
for(int i = 0; i<rectangleMatrix.length; i++){
for(int j = 0; j<rectangleMatrix[i].length; j++){
drawRectangle(rectangleMatrix[i][j]);
}
}
drawRectangle(RECTANGLE toDraw) is in the same class that you have the method with the for-loop in.
In your parent component of grid set layout like
gamePanel.setLayout(new GridLayout(ix, iy));
where ix and iy is dimension size
you also need array of cells private Cell[][] cells; in this case cell is simply
public class Cell extends JPanel{
//some game specific code, fields, constructors
}
now use it
for (int i = 0; i < ix; i++) {
for (int j = 0; j < iy; j++) {
progres.setValue(progres.getValue() + 1);
cells[i][j] = new Cell(i, j, passer);
gamePanel.add(cells[i][j]);
}
}
now if in constructor of cell you will set a border this.setBorder(new LineBorder(Color.GRAY)); grid will appear. in cell constructor or setter method you can also pass whole cells so each cell will be aware of another, and be able to interact with them. but be aware- if you pass it in loop that also create cells, you might encounter a null values.
this way you can use swing components so each cell can be clickable etc.
You can extend a JComponent and overriding its paint(Graphics) method to draw the grid using Graphics.drawLine.
If you need the grid cells to be interactive in some way, then you should probably just create components with rectangular borders and position them using a grid layout.
Assume we defined a 2d vector in Java. Size of the first dimension is 10 and size of second dimension is 1. Now if we want to increase size of first dimension of this 2d vector what should we do?
Assume we want add number 1 to 30th cell (i.e. v1[29][0]=1), but our vector size is 10.
Here is my code in Java:
Vector [][] v1 = new Vector [10][];
for (int i=0 ; i<10; i++)
if (i==0) {
v1[i]=new Vector[1];
}
else
v1[i]=v1[0];
v1[0][0]=new Vector(1);
You need to assign new, bigger array, for example v1 = new Vector[30][].
Note that Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.
Also, make sure that you understand what are you doing. If you only need to "add number 1 to 30th cell (i.e. v1[30][0]=1)", then int v1 = new int[31][] should be enough, without any vectors. By the way, 30th cell is v1[29][0], not v1[30][0], watch out for this common mistake-by-one.