summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/graph/view/DefaultVertexRenderer.java
blob: 6f57185bc9bcb9edd8b6d195bc39cf95983fa309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.c2kernel.graph.view;

import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Polygon;

import com.c2kernel.graph.model.GraphPoint;
import com.c2kernel.graph.model.Vertex;


public class DefaultVertexRenderer implements VertexRenderer
{
    private Paint mLinePaint = null;
    private Paint mTextPaint = null;
    private Paint mFillPaint = null;


    public DefaultVertexRenderer(Paint linePaint, Paint textPaint, Paint fillPaint)
    {
        mLinePaint = linePaint;
        mTextPaint = textPaint;
        mFillPaint = fillPaint;
    }


    public void draw(Graphics2D g2d, Vertex vertex)
    {
        GraphPoint[]    outlinePoints = vertex.getOutlinePoints();
        GraphPoint      centrePoint   = vertex.getCentrePoint();
        Polygon         outline       = new Polygon();

        String          vertexName    = vertex.getName();
        FontMetrics     metrics       = g2d.getFontMetrics();
        int             textWidth     = metrics.stringWidth(vertexName);
        int             textHeight    = metrics.getHeight();
        int             textX         = centrePoint.x - textWidth/2;
        int             textY         = centrePoint.y + textHeight/3;

        int             i             = 0;


        // Construct a shape in the outline of the vertex
        for(i=0; i<outlinePoints.length; i++)
        {
            outline.addPoint(outlinePoints[i].x, outlinePoints[i].y);
        }

        // Fill and then draw the outline
        g2d.setPaint(mFillPaint);
        g2d.fill(outline);
        g2d.setPaint(mLinePaint);
        g2d.draw(outline);

        // Write the name of the vertex in the centre of the outline
        g2d.setPaint(mTextPaint);
        g2d.drawString(vertexName, textX, textY);
    }
}