Hello I am trying to draw diagonal lines in Java and this won’t work like it should..
the “value” variable is beeing updated in a for loop each time but it gets the next value
for example if i insert a 1 i get this in my console with a system.out.println(value):
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
but the variable “value” has to contain the value i insert.. the code i use for this you can find below
DrawLines line = new DrawLines();
int value = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
int xPos = 0;
int yPos = getHeight() - (getHeight() / 2);
for(int aantalLines = 0; aantalLines < 10; aantalLines++ ) {
line.drawLines(g, xPos, yPos + value, getWidth(), getHeight() - value );
value += value;
System.out.println(value);
System.out.println(aantalLines);
}
}
public void actionPerformed(ActionEvent e) {
try {
value = Integer.parseInt(tussenRuimte.getText());
repaint();
}
catch(NumberFormatException err) {
JOptionPane.showMessageDialog(null, "Number Format Error: Vul alles goed in s.v.p");
}
}
the problem is that it doesn’t work like this.. can someone explain what i do wrong and how to fix this?
Why modifying the value of
valuewhile you already have a loop variable:which should come down to what @Hovercraft already suggested.
If none of those solutions help, you probably have a problem somewhere else.
Note: do not alter state in the
paint,paintComponent, … methods. You have no control over how many times and when they are invoked