2

I am doing a project in which I need to print the label/description of the line (drawn using graphics) with respect to orientation of the line. enter image description here

Does anyone know how to do it?

mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

7

Look to the Graphics2D methods such as rotate(), scale() & translate() - as well as the more general translate(AffineTransform) method.

See Transforming Shapes, Text, and Images in the Java tutorial for more details and working examples, especially of using an AffineTransform (which can concatenate scale, rotate, transform & shear operations).


You do not mention how you obtain the Graphics object. The Graphics object passed to Swing components in paintComponent(Graphics) will generally be a Graphics2D instance, and can be cast to one. To get a Graphics2D instance from a BufferedImage, call createGraphics().

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

Make a class called "Labelled line" and make it something like this

class LabeledLine {

    private int x1, y1, x2, y2;
    private String label;

    public void drawOn(Graphics g) {
        // need more features? thickness, etc? add it
        g.drawLine(x1,y1,x2,y2);
        // compute size of text, position of text, angle of text
        // draw that text
    }

}

A quick google for drawing angled text gave me a couple results, so that should be easy to do.

corsiKa
  • 81,495
  • 25
  • 153
  • 204