我最近开始学习java编程,并且开始做一些图形。我正在为图形制作 GUI,并且在传递使用 JOptionPane.showInputDialog 从数组中选择的形状时遇到了一些问题。是否建议为我要绘制的每个 2D 形状创建方法,然后在另一个类文件中使用 paint 方法并重载它?请给我一些帮助。我的代码如下所示。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class test extends JFrame {
String x1; // starting x co-ordinate
String y1; // starting y co-ordinate
String r1; // radius for circle
String l1; // length for shape
String h1; // height for shape
String draw;
int x = 0;
int y = 0;
int l = 0;
int h = 0;
int r = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test frame = new test();
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(2000, 2000);
}
};
// frame.getContentPane().add(new JScrollPane(panel),
// BorderLayout.CENTER);
frame.setVisible(true);
frame.setSize(1200, 600);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public test() {
String[] shapes = { "Circle", "Ellipse", "Line", "Rectangle" };
String drawShape = (String) JOptionPane.showInputDialog(null,
"Select a shape to be drawn: ", "Drawing 2D Shapes",
JOptionPane.QUESTION_MESSAGE, null, shapes, shapes[0]);
if (drawShape == "Circle") {
System.out.println("Shape selected is: " + drawShape);
x1 = JOptionPane.showInputDialog(null,
"Input center (x value): ");
y1 = JOptionPane.showInputDialog(null,
"Input center (y value): ");
r1 = JOptionPane.showInputDialog(null, "Input radius: ");
// parsing the variables from String to Integer
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
r = Integer.parseInt(r1);
}
if (drawShape == "Ellipse") {
System.out.println("Shape selected is: " + drawShape);
x1 = JOptionPane.showInputDialog(null,
"Input x starting point: ");
y1 = JOptionPane.showInputDialog(null,
"Input y starting point: ");
l1 = JOptionPane.showInputDialog(null, "Please enter Width: ");
h1 = JOptionPane
.showInputDialog(null, "Please enter Height: ");
// parsing the variables from String to Integer
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
l = Integer.parseInt(l1);
h = Integer.parseInt(h1);
}
if (drawShape == "Line") {
System.out.println("Shape selected is: " + drawShape);
x1 = JOptionPane.showInputDialog(null, "Input point 1: ");
y1 = JOptionPane.showInputDialog(null, "Input point 2: ");
l1 = JOptionPane.showInputDialog(null, "Input point 3: ");
h1 = JOptionPane.showInputDialog(null, "Input point 4: ");
// parsing the variables from String to Integer
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
l = Integer.parseInt(l1);
h = Integer.parseInt(h1);
}
if (drawShape == "Rectangle") {
x1 = JOptionPane.showInputDialog(null,
"Input x starting point: ");
y1 = JOptionPane.showInputDialog(null,
"Input y starting point: ");
l1 = JOptionPane.showInputDialog(null, "Please enter Width: ");
h1 = JOptionPane
.showInputDialog(null, "Please enter Height: ");
// parsing the variables from String to Integer
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
l = Integer.parseInt(l1);
h = Integer.parseInt(h1);
}
}
public void paint(Graphics g) {
super.paint(g);
// drawing the shape
Graphics2D g2 = (Graphics2D) g;
Rectangle r = new Rectangle(x, y, l, h);
g2.draw(r);
/*
* Ellipse2D.Double circle = new Ellipse2D.Double(x, y, r, r);
* g2.draw(circle); g2.drawString("This is a Circle", (x + (r / 2)), y);
*
* g2.setColor(new Color(0, 0, 255));
* System.out.println("Shape selected is: " + type);
*
* Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, l, h); //
* defining // the // object // graphically g2.draw(ellipse);
* g2.drawString("This is a ellipse", x, y);
*
* g2.setColor(new Color(0, 0, 255));
* System.out.println("Shape selected is: " + type);
*
* Line2D.Double line1 = new Line2D.Double(x, y, l, h); g2.draw(line1);
* g2.drawString("Point 1", x, y); g2.drawString("Point 2", l, h);
*/
}
}