When I resize my app the Jtextfields before the resizement get paint over and it creates an huge mess enter image description here My code is simple I am trying to create a grid from an existing 2d array with the numbered centered in the grid like sudoku but when I resize my app the application it makes this mess here is my code:
package graphics;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Table {
int currentX;
int currentY;
final Color currrentColor = Color.yellow;
final Color pasteColor = Color.red;
int[][] array;
int[][] selectedCase;
int selectedSize;
TableRepresentation tab;
void setRepresentation(Color backgroundColor, int width, int height)
{
tab = new TableRepresentation(backgroundColor,width,height);
}
JPanel getRepresentation()
{
return tab;
}
public Table(int[][] array)
{
this.array = array;
selectedCase = new int[2][array.length * array[0].length];
currentX = 0;
currentY = 0;
selectedSize = 0;
updateCase(currentX , currentY);
}
public void updateCase(int x , int y) {
currentX = x;
currentY = y;
selectedCase[0][selectedSize] = x;
selectedCase[1][selectedSize] = y;
selectedSize++;
}
private class TableRepresentation extends JPanel {
double width;
double height;
Dimension ceil;
TableRepresentation(Color backgroundColor, int width, int height){
setSize(width, height);
setBackground(backgroundColor);
this.width = (double) width / array[0].length - 3;
this.height = (double) height / array.length - 3;
ceil = new Dimension((int)this.width - 2,(int)this.height - 2);
}
void createGrid(Graphics2D g2d) {
g2d.drawRect(0,0,getWidth(), getWidth());
for (int i = 1; i <= array[0].length ; i++)
{
if (i == 1)
{
g2d.drawLine((5+(int) width) * i,0,(5+(int) width) * i,getWidth());
}
g2d.drawLine((4+(int) width) * i,0,(4+(int) width) * i,getWidth());
}
for (int i = 1; i < array[0].length ; i++)
{
if (i == 1)
{
g2d.drawLine(0,((int) height + 5) * i,getHeight(),((int) height + 5) * i);
}
g2d.drawLine(0,((int) height + 4) * i,getHeight(),((int) height + 4) * i);
}
}
Point getCeilAtLocation(int x, int y) {
return new Point(3 + ((int) width + 3) * x,(3 + ((int) height + 3) * y));
}
JTextField setAtributes() {
JTextField text = new JTextField();
text.setSize(ceil);
text.setBorder(null);
text.setEditable(false);
text.setFocusable(false);
text.setForeground(Color.white);
text.setBackground(Color.BLACK);
text.setFont(new Font("Arial", Font.PLAIN,(int) width - 10));
text.setHorizontalAlignment(JTextField.CENTER);
return text;
}
void createNumbers()
{
JTextField text = null;
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[0].length; j++)
{
text = setAtributes();
text.setText(Integer.toString(array[i][j]));
text.setLocation(getCeilAtLocation(j,i));
add(text, -1);
text.setVisible(true);
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.white);
Stroke basic = new BasicStroke(3);
g2d.setStroke(basic);
createGrid(g2d);
createNumbers();
}
}
}
How can I stop the textfields to get paint over please help
Your core problem is you're trying to add more components to the UI within a paint pass. This is not what you should be doing at this phase of the program, and even if you could, you'd need to remove the old components first.
Instead, start with some base properties. For example, set the initial font size to something sensible (or use the default font size of the text fields), then use an appropriate layout manager to handle the actual sizing/positing of the components and forget about the "width"/"height" of the "squares".
If the "width" & "height" are important, then I'd consider starting with a custom component and override it's
getPreferredSizemethod with these values and add a singleJTextFieldto it (maybe using aBorderLayout).Now, the next problem is the font size, there is a significant difference between a fonts "point size" and it's pixel size, which should also include it's width and height of the text been rendered.
What you need to do, is when the component is resized, is calculate the desired "cell size", calculate the largest piece of text you need to render and then increase or decrease the point size until that piece of text fits within the bounds of the cell ... easy
Now, this is FAR from optimised and you should make some time to see if there are ways to improve it's overall performance. For example
componentResizedcan be called a lot of times within a short period of time, so it might be worth while delaying any updates until it stabilizes.I would also recommend taking the time learn and understand the layout API, see Laying Out Components Within a Container as a starting point