here's the code but I don't know why it doesn't work I've tried adding a revalidate and a repaint but the square won't draw, I'm trying to make the square move over a bit when 2 is entered and the 1 is there as an attempt to force a refresh and draw the square but it never worked unless i just have the square be still but as said i want it to move
import javax.swing.*;
public class App {
public static void main(String[] args){
JFrame frame = new JFrame("test");
frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(new DrawSquare());
System.out.println("end");
while (true==true){
frame.revalidate();
System.out.println("should work");
}
}
}
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawSquare extends JPanel {
@Override
public void paintComponent(Graphics g) {
g.setColor(java.awt.Color.red);
super.paintComponent(g);
Integer x = 90;
Integer y = 0;
JFrame frame = new JFrame("test");
while (y==0){
while (true){
frame.revalidate();
Scanner myObj = new Scanner(System.in);
System.out.println("Enter command 1=draw square 2= move square");
Integer input = myObj.nextInt();
System.out.println("Next step: " + input);
if(input == 2){
x=x+1;
input = 3;
}
if(input == 1 ){
g.setColor(java.awt.Color.red);
g.drawRect(x, x, x, x);
g.fillRect(x,x,x,x);
frame.repaint();
frame.revalidate();
System.out.println("WHY");
break;
}
g.setColor(java.awt.Color.red);
g.drawRect(100, x, 100, 100);
g.fillRect(100,x,100,100);
System.out.println("sqlaie");
frame.revalidate();
}
}
}
}
First...
No it shouldn't (work). Free cycling loops like this are capable of consuming all you CPU cycles which could prevent your program from performing other tasks (ie thread starvation).
revalidatewon't repaint the UI anyway, it's used to update the layout of the components within a given container.Then...
paintComponentwill set the color of theGraphicsto the background color of the component, which means that your previous call will be undone. CallpaintComponentfirst, then perform your custom painting.Then...
Ah, why? Apart from create another
frameeach timepaintComponentis been called, this has nothing to do with what's been presented on the screen and is otherwise just a complete waste of time and resources.And then...
This is going to cause your program to hang, prevent any further updates to the UI and user input being captured.
paintComponentis expected to paint the current state and exit as fast as possible, you should avoid doing extra processing which could otherwise be done else where.and finally...
Don't use
Scannerfor user input in a GUI environment, this isn't how user input is monitored for.Everything speaks volumes of your lack of understanding and appropriate research into how to solve these problems.
Stackoverflow is not a tutorial site and you should spend the time researching your problems and attempting to fix them before posting. Once you've reached this point, SO becomes useful to you.
I would highly recommend taking a look at Painting in AWT and Swing and Performing Custom Painting to get a better understanding of how the paint system works and How to Use Key Bindings for details about how to monitor and respond to user input
I would avoid
KeyListeneras it's problematic.Runnable example