创建标签以及绘制线 [英] Creating labels along with drawlines

查看:109
本文介绍了创建标签以及绘制线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个关于自定义小部件的问题,但是我是否需要它以及如何继续这个问题很困惑。

I have asked a question regarding custom widget but confused to whether I need it and how should proceed.

我目前有这个班级

public class GUIEdge {

   public Node node1;
   public Node node2;
   public int weight;
   public Color color;
    public GUIEdge(Node node1, Node node2 , int cost) {
       this.node1 = node1;
       this.node2 = node2;
       this.weight = cost;
       this.color = Color.darkGray;
    }

    public void draw(Graphics g) {
        Point p1 = node1.getLocation();
        Point p2 = node2.getLocation();
        g.setColor(this.color);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g.drawLine(p1.x, p1.y, p2.x, p2.y);

    }
}

目前这在两者之间徘徊但现在我想要一个成本标签也随之创建。

Currently this draws an edge between two points but now I want that a label for the cost also gets created along with it.

我已经添加了拖动节点和边缘的处理,那么最好的方法是什么创建标签

I have already added handling for dragging of node and edges so what is the best way to create the label

我是否需要为此制作自定义小部件?任何人都可以解释一下,假设通过从JComponent扩展来创建组件然后我将通过g.mixed()调用它,其中混合是新的小部件......?

Do I need to make a custom widget for that ? Could anyone explain that suppose making a component by extending from JComponent then I'll call it by g.mixed() where mixed is that new widget...?

推荐答案

工具提示当然值得一看。其他选择包括 drawString() translate() TextLayout 。有很多示例

Tool tips are certainly worth a look. Other choices include drawString(), translate(), or TextLayout. There are many examples available.

附录:下面的示例显示 drawString() setToolTipText(),如@Catalina所示岛。对于简单,端点相对于组件的大小,因此您可以看到调整窗口大小的结果。

Addendum: The example below shows both drawString() and setToolTipText(), as suggested by @Catalina Island. For simplicity, the endpoints are relative to the component's size, so you can see the result of resizing the window.

附录:使用 setToolTipText()只是演示了这种方法。正如@camickr指出此处,您应该覆盖 getToolTipText(MouseEvent)并更新鼠标悬停在线上或选择线时的提示。

Addendum: This use of setToolTipText() merely demonstrates the approach. As @camickr notes here, you should override getToolTipText(MouseEvent) and update the tip when the mouse is over the line or when the line is selected.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JComponent;
import javax.swing.JFrame;

/** @see https://stackoverflow.com/questions/5394364 */
public class LabeledEdge extends JComponent {

    private static final int N = 20;
    private Point n1, n2;

    public LabeledEdge(int w, int h) {
        this.setPreferredSize(new Dimension(w, h));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.n1 = new Point(N, N);
        this.n2 = new Point(getWidth() - N, getHeight() - N);
        g.drawLine(n1.x, n1.y, n2.x, n2.y);
        double d = n1.distance(n2);
        this.setToolTipText(String.valueOf(d));
        g.drawString(String.valueOf((int) d),
            (n1.x + n2.x) / 2, (n1.y + n2.y) / 2);
    }

    private static void display() {
        JFrame f = new JFrame("EdgeLabel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new LabeledEdge(320, 240));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }
}

这篇关于创建标签以及绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆