如何使用输入使用 Swing Java 调整绘图大小
How to Resize Drawing using Swing Java using input
我正在尝试从用户那里获取输入并使用此输入来调整 JFrame 中圆的大小。
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class CircleResize extends JFrame {
public static void main(String[] args) {
CircleResize frame = new CircleResize();
frame.setTitle("Tutorial 9");
frame.setSize(800, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
public CircleResize() {
add(new Drawing());
}
static class Drawing extends JPanel {
protected void paintComponent(Graphics g) {
Scanner input = new Scanner(System.in);
System.out.print("Enter x: ");
int a = input.nextInt();
System.out.print("Enter y: ");
int b = input.nextInt();
g.drawOval(200, 50, a, b);
}
}
}
不知道为什么,但它一直要求输入,即使它不是循环。我不明白为什么。
当前输出示例:
Enter x: 50
Enter y: 50
Enter x: 50
Enter y: 50
Enter x: 50
Enter y: 50
您正在覆盖 paintComponent
,每次 Graphics
发生变化时都会调用它。在那里你要求输入并调用 drawOval
。这会导致更改,因此事件调度程序调用 paintComponent
。这就是你的循环。
将您的输入输入 main
。在 paintComponent
和 main 的范围内声明 a
和 b
。然后调用 repaint ()
或 revalidate ()
。这将导致对 paintComponent () 的调用,不是直接或立即,而是很快。
不要在 paintComponent 中调用会改变绘画时所用事物状态的事物。
编辑时:
哦,好吧,我开始只发布一些内容,但很快就变成了大部分内容,所以这是我编写的一些工作代码,不是完美的专业标准,但我认为它会对你有所帮助。
public class SandBox {
public static void main (String [] args) {
CircleResize frame = new CircleResize ();
frame.setTitle ("Tutorial 9");
frame.setSize (800, 400);
frame.setLocation (100, 100);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// frame.setLocationRelativeTo (null); // Center the frame
frame.setVisible (true);
Scanner input = new Scanner (System.in);
System.out.print ("Enter a: ");
frame.a = input.nextInt ();
System.out.print ("Enter b: ");
frame.b = input.nextInt ();
frame.repaint ();
}
}
class CircleResize extends JFrame {
int a = 100;
int b = 100;
public CircleResize () {
add (new Drawing ());
}
class Drawing extends JPanel {
@Override
protected void paintComponent (Graphics g) {
g.drawOval (200, 50, a, b);
}
}
}
我正在尝试从用户那里获取输入并使用此输入来调整 JFrame 中圆的大小。
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class CircleResize extends JFrame {
public static void main(String[] args) {
CircleResize frame = new CircleResize();
frame.setTitle("Tutorial 9");
frame.setSize(800, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
public CircleResize() {
add(new Drawing());
}
static class Drawing extends JPanel {
protected void paintComponent(Graphics g) {
Scanner input = new Scanner(System.in);
System.out.print("Enter x: ");
int a = input.nextInt();
System.out.print("Enter y: ");
int b = input.nextInt();
g.drawOval(200, 50, a, b);
}
}
}
不知道为什么,但它一直要求输入,即使它不是循环。我不明白为什么。 当前输出示例:
Enter x: 50
Enter y: 50
Enter x: 50
Enter y: 50
Enter x: 50
Enter y: 50
您正在覆盖 paintComponent
,每次 Graphics
发生变化时都会调用它。在那里你要求输入并调用 drawOval
。这会导致更改,因此事件调度程序调用 paintComponent
。这就是你的循环。
将您的输入输入 main
。在 paintComponent
和 main 的范围内声明 a
和 b
。然后调用 repaint ()
或 revalidate ()
。这将导致对 paintComponent () 的调用,不是直接或立即,而是很快。
不要在 paintComponent 中调用会改变绘画时所用事物状态的事物。
编辑时:
哦,好吧,我开始只发布一些内容,但很快就变成了大部分内容,所以这是我编写的一些工作代码,不是完美的专业标准,但我认为它会对你有所帮助。
public class SandBox {
public static void main (String [] args) {
CircleResize frame = new CircleResize ();
frame.setTitle ("Tutorial 9");
frame.setSize (800, 400);
frame.setLocation (100, 100);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// frame.setLocationRelativeTo (null); // Center the frame
frame.setVisible (true);
Scanner input = new Scanner (System.in);
System.out.print ("Enter a: ");
frame.a = input.nextInt ();
System.out.print ("Enter b: ");
frame.b = input.nextInt ();
frame.repaint ();
}
}
class CircleResize extends JFrame {
int a = 100;
int b = 100;
public CircleResize () {
add (new Drawing ());
}
class Drawing extends JPanel {
@Override
protected void paintComponent (Graphics g) {
g.drawOval (200, 50, a, b);
}
}
}