我最近开始学习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);
*/
}
}
答案 0 :(得分:0)
有几种不同的选择。就像你有x,y等作为类变量一样,让你的drawShape成为一个类变量。然后在你的绘画方法中,你会知道要画什么。
在某些时候,您可能想要绘制多个形状。
我会为你想要绘制的每个形状创建一个类。它们都应该实现一个具有一个方法“draw”的接口,该方法将Graphics作为参数。
假设您的界面名为MyShapeI。你的班级将有一组MyShapeI对象。
然后,您不是设置类变量,而是创建一个名为MyCircle的新实例,并设置其x,y,r字段,并将其添加到您的集合中。
最后在绘画中,你会遍历你的集合,并调用每个对象的绘制方法。
这可以为您提供无限可能。你可以拥有一个MyElephant类,它会知道如何在指定的x,y点绘制大象。