0
我在使用矩形和橢圓的這個程序時遇到了麻煩。矩形/橢圓程序
有4個可供選擇的按鈕,矩形/橢圓/邊/標籤,其中標籤具有文本字段 您可以選擇矩形/橢圓進行繪製,然後單擊框架中的某處,然後將其繪製到那裏。邊緣通過拖動鼠標完成。
我不明白的是如何做到既矩形和橢圓,並給定爲一個超類「RectangleNode」等在這裏,樣品抽象類的抽象類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;
}
}
任何幫助將不勝感激,因爲我完全失去了。
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);
}
}
是的,不允許嗎?如果是的話,我會刪除它或什麼 – JA3N 2012-02-23 03:15:11
家庭作業很好。下次將它標記爲家庭作業。 – 2012-02-23 03:51:16
你能發佈矩形和橢圓和矩形節點的代碼嗎? – 2012-02-23 03:52:25