Text krümmen und spitz zulaufend?

Romsl

Erfahrenes Mitglied
Hi,

bin gerade dran einen simplen Graphen zu realisieren. Soweit so gut. Jetzt hab ich ein Paper gelesen bei dem die Vertices nicht über einen Pfeil (Kante + LinkLabel) sondern direkt mit dem Text verbunden sind.

Der Text ist in die gewünschte Richtung spitz zulaufend und gekrümmt. Zusätzlich wurden die Zeichen noch entsprechend gedreht.

Hier mal der Stand

Code:
package test.graph;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// TODO: document me

/**
 * Test.
 * <p/>
 * User: romsl
 * Date: 23.12.2005
 * Time: 15:51:25
 *
 * @author Romsl
 * @version $Id$
 */
public class Test extends JFrame {

    static protected Label label = new Label(
            "Drag rectangle around within the area");

    public Test() {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        Graph graph = new Graph();
        graph.setSize(400, 200);

        Vertice v1 = new Vertice(10, 10, 30, 30);

        graph.addAdjacent(v1, new Vertice(100, 100, 30, 30));
        graph.addAdjacent(v1, new Vertice(200, 50, 40, 40));
        graph.setBackground(Color.YELLOW);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(graph, BorderLayout.CENTER);

        getContentPane().add(label, BorderLayout.SOUTH);

        pack();
        setSize(new Dimension(550, 250));
        setVisible(true);
    }

    public static void main(String s[]) {
        new Test();
    }
}

Code:
package test.graph;

import javax.swing.*;
import java.util.ArrayList;
import java.awt.*;

// TODO: document me

/**
 * Graph.
 * <p/>
 * User: romsl
 * Date: 24.12.2005
 * Time: 16:58:48
 *
 * @author Romsl
 * @version $Id$
 */
public class Graph extends JComponent {

    private ArrayList<Vertice[]> adjacentList;

    public Graph() {

    }

    public ArrayList<Vertice[]> getAdjacentList() {
        return adjacentList;
    }

    public void setAdjacentList(ArrayList<Vertice[]> adjacentList) {
        this.adjacentList = adjacentList;
    }

    public void addAdjacent(Vertice from, Vertice to) {

        if (adjacentList == null) adjacentList = new ArrayList<Vertice[]>();

        Vertice[] adjacent = new Vertice[]{from, to};

        adjacentList.add(adjacent);

        add(from);
        add(to);
    }

    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        g.setColor(Color.BLACK);

        for (Vertice[] adjacent : adjacentList) {

            Vertice v1 = adjacent[0];
            Vertice v2 = adjacent[1];

            Dimension size1 = v1.getSize();
            int x1 = v1.getX() + (int) (size1.getWidth() / 2);
            int y1 = v1.getY() + (int) (size1.getHeight() / 2);

            Dimension size2 = v2.getSize();
            int x2 = v2.getX() + (int) (size2.getWidth() / 2);
            int y2 = v2.getY() + (int) (size2.getHeight() / 2);

            g.drawLine(x1, y1, x2, y2);
        }
    }
}

Code:
package test.graph;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;

// TODO: document me

/**
 * Vertice.
 * <p/>
 * User: romsl
 * Date: 23.12.2005
 * Time: 15:51:58
 *
 * @author Romsl
 * @version $Id$
 */
public class Vertice extends JComponent implements MouseListener, MouseMotionListener {

    private int width;
    private int height;

    private transient int dragX;
    private transient int dragY;

    boolean selected = false;

    public Vertice(int x, int y, int width, int height) {

        this.width = width;
        this.height = height;

        addMouseMotionListener(this);
        addMouseListener(this);

        setBounds(x, y, width, height);
    }

    public void mousePressed(MouseEvent e) {

        if (contains(e.getX(), e.getY())) {
            selected = true;
            dragX = e.getX();
            dragY = e.getY();
        }
        else {
            Test.label.setText("Drag it.");
        }
    }

    public void mouseDragged(MouseEvent e) {

        if (selected) {
            updateLocation(e);
        }
        else {
            Test.label.setText("Drag it.");
        }
    }

    public void mouseReleased(MouseEvent e) {
        if (contains(e.getX(), e.getY())) {
            selected = false;
        }
        else {
            Test.label.setText("Drag it.");
        }
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void updateLocation(MouseEvent e) {

        int x = e.getX() - dragX;
        int y = e.getY() - dragY;

        ((JComponent) e.getSource()).setLocation(getX() + x, getY() + y);

        getParent().repaint();

        Test.label.setText(getX() + ", " + getY());
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.RED);
        g.fillOval(0, 0, width, height);
    }
}

Wäre super wenn mir jemand einen Tipp oder ein Beispiel geben könnte.

Gruß

Romsl
 

Anhänge

  • arrow.jpg
    arrow.jpg
    28,2 KB · Aufrufe: 36

Neue Beiträge

Zurück