矩形/椭圆程序
我在使用这个适用于矩形和椭圆形的程序时遇到了问题。
有 4 个按钮可供选择,矩形/椭圆/边缘/标签,其中标签具有文本字段 您选择要绘制的矩形/椭圆形并单击框架中的某处,它将在此处绘制。边缘是通过拖动鼠标来完成的。
我不明白的是如何做矩形和椭圆,以及作为“矩形节点”等的超类的示例抽象类。这是抽象类 GraphElement 的代码:
import java.awt.Graphics2D;
abstract public class GraphElement
{
private double xPos;
private double yPos;
protected String label;
public GraphElement()
{
xPos = 0;
yPos = 0;
}
public GraphElement(double x, double y)
{
xPos = x;
yPos = y;
}
public final double getXPos()
{
return xPos;
}
public final double getYPos()
{
return yPos;
}
public void moveTo (double xLoc, double yLoc)
{
xPos = xLoc;
yPos = yLoc;
}
public String toString()
{
String str = "(X,Y) Position: (" + xPos + "," + yPos + ")\n";
return str;
}
abstract void draw(Graphics2D g2);
abstract boolean isSelected(double x, double y);
boolean applyLabel()
{
return true;
}
public String getLabel()
{
return label;
}
public void setLabel(String label)
{
this.label = label;
}
}
任何帮助将不胜感激,因为我'我完全迷失了。
图形绘制查看器:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GraphDrawViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 1000;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Graph Draw");
frame.setLayout(new BorderLayout());
//Panel
JPanel panel = new JPanel();
frame.add(panel, BorderLayout.NORTH);
//Buttons
JButton rectangleButton = new JButton("Rectangle");
JButton ellipseButton = new JButton("Ellipse");
JButton edgeButton = new JButton("Edge");
JButton labelButton = new JButton("Label");
//Text Field
final int FIELD_WIDTH = 10;
final JTextField labelField = new JTextField(FIELD_WIDTH);
//Add all buttons
panel.add(rectangleButton);
panel.add(ellipseButton);
panel.add(edgeButton);
panel.add(labelButton);
panel.add(labelField);
frame.setVisible(true);
}
}
I am having trouble with this program that works with Rectangles and Ellipses.
There are 4 Buttons to select, Rectangle/Ellipse/Edge/Label, with Label having a text field
You select either Rectangle/Ellipse to draw and click somewhere in the frame, and it will draw it there. The edge is done by dragging the mouse.
What I don't understand is how to do both rectangles and Ellipses, and the sample abstract class given to be a superclass to "RectangleNode" and etc. Here is the code for the abstract class GraphElement:
import java.awt.Graphics2D;
abstract public class GraphElement
{
private double xPos;
private double yPos;
protected String label;
public GraphElement()
{
xPos = 0;
yPos = 0;
}
public GraphElement(double x, double y)
{
xPos = x;
yPos = y;
}
public final double getXPos()
{
return xPos;
}
public final double getYPos()
{
return yPos;
}
public void moveTo (double xLoc, double yLoc)
{
xPos = xLoc;
yPos = yLoc;
}
public String toString()
{
String str = "(X,Y) Position: (" + xPos + "," + yPos + ")\n";
return str;
}
abstract void draw(Graphics2D g2);
abstract boolean isSelected(double x, double y);
boolean applyLabel()
{
return true;
}
public String getLabel()
{
return label;
}
public void setLabel(String label)
{
this.label = label;
}
}
Any help would be appreciated as I'm totally lost.
GraphDrawViewer:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GraphDrawViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 1000;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Graph Draw");
frame.setLayout(new BorderLayout());
//Panel
JPanel panel = new JPanel();
frame.add(panel, BorderLayout.NORTH);
//Buttons
JButton rectangleButton = new JButton("Rectangle");
JButton ellipseButton = new JButton("Ellipse");
JButton edgeButton = new JButton("Edge");
JButton labelButton = new JButton("Label");
//Text Field
final int FIELD_WIDTH = 10;
final JTextField labelField = new JTextField(FIELD_WIDTH);
//Add all buttons
panel.add(rectangleButton);
panel.add(ellipseButton);
panel.add(edgeButton);
panel.add(labelButton);
panel.add(labelField);
frame.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我对你的问题的理解,你似乎不确定如何创建一个具有两个子类的类,这两个子类做的事情看起来“本质上”不同。如果您访问
Java
文档并查看Graphics
类,特别是drawOval
、drawRectangle、
fillOval
和fillRectangle
方法,您将看到方法名称中的椭圆实际上创建了一个适合给定矩形大小的省略号< /强>。我认为这足以为您开始编写程序提供足够的提示。From what I understand about your question, it seems that you're uncertain how to create a class that has two subclasses which do things that seem "inherently" different. If you go to the
Java
docs and take a look-see in theGraphics
class, particularly at thedrawOval
,drawRectangle
,fillOval
, andfillRectangle
methods, you will see that the oval in the name of the method actually creates an ellipsis that fits in the given rectangle size. I think that this suffices as enough of a hint for you to start working on your program.