矩形/椭圆程序

发布于 2025-01-08 10:18:39 字数 2645 浏览 0 评论 0原文

我在使用这个适用于矩形和椭圆形的程序时遇到了问题。

有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶舞 2025-01-15 10:18:39

根据我对你的问题的理解,你似乎不确定如何创建一个具有两个子类的类,这两个子类做的事情看起来“本质上”不同。如果您访问 Java 文档并查看 Graphics 类,特别是 drawOvaldrawRectangle、fillOvalfillRectangle 方法,您将看到方法名称中的椭圆实际上创建了一个适合给定矩形大小的省略号< /强>。我认为这足以为您开始编写程序提供足够的提示。

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 the Graphics class, particularly at the drawOval, drawRectangle, fillOval, and fillRectangle 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文