In my code, I would like to have the method, paintComponent1 (draw a line) to run based on whether or not the key is pressed. How would I go about that? All help is appreciated, Thanks :D!
Here is my code:
import java.awt.event.KeyEvent;
import java.awt.*;
import java.applet.Applet;
public class Test {
boolean x = false;
public void paintComponent1(Graphics g){
g.drawLine(35, 60, 100, 120);
}
public void keyPressed(KeyEvent e) {
x = true;
}
public void keyReleased(KeyEvent e) {
x = false;
}
public void paintComponent(Graphics g) {
if (x)
paintComponent1();
}
}