editable grids
in
Programming Questions
•
1 year ago
Hi
I have this grid and I have been trying to make a few changes to it.
If any of you guys could help it would be greatly appreciated.
They are simple questions I hope
It is a grid and each box is has an input for one letter.
I need to make this small program be able to do the following.
1. When you press on the box, a box pops up. When you press the ok button with no letter inputed it does nothing.
2. If you do not input a letter in the field there will be no change and all letter must be capital in the boxes.
3. If you drag a box to another box, the input is copied to the box you dragged it to.
4. I want to define a 'class grid' to the two dimensional array of characters.
Thank you to all you guys in advance.
This is greatly appreciated.
Here is my code
import javax.swing.JOptionPane;
String str = "H";
String newStr = "";
int cell1, cell2;
final int CELL_SIZE = 50;
final int NUM_ROW = 10;
final int NUM_COL = 10;
char[][] letters = new char[NUM_ROW][NUM_COL];
int code;
PFont letterFont;
void setup()
{
initLetters(letters);
displayLetters(letters);
size(NUM_ROW * CELL_SIZE, NUM_COL * CELL_SIZE);
letterFont = createFont("Palatino", CELL_SIZE * 0.6);
textFont(letterFont);
textAlign(CENTER, CENTER);
}
void draw () {
background(255);
if (str != null)
{
fill(color(0));
letters[cell1/CELL_SIZE][cell2/CELL_SIZE] = str.charAt(0);
}
drawGrid();
}
void initLetters(char[][] a)
{
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[0].length; j++)
{
a[i][j] = (0);
}
}
}
void displayLetters(char[][] a)
{
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[0].length; j++)
{
print(a[i][j]);
}
}
}
void drawCell(int row, int col, char letter)
{
stroke(0);
noFill();
rect(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE);
fill(0);
text(letter, col * CELL_SIZE + CELL_SIZE/2, row * CELL_SIZE + CELL_SIZE/2);
}
void drawGrid()
{
for (int i = 0; i < letters.length; i++)
{
for (int j = 0; j < letters[0].length; j++)
{
drawCell(i, j, letters[i][j]);
}
}
}
void mouseClicked()
{
cell1=mouseY;
cell2=mouseX;
noLoop();
newStr = (String)JOptionPane.showInputDialog("Enter a Character");
loop();
str = newStr;
}
Thanks again!
1