1

Hi I am creating a java desktop application where I am drawing rectangle. I want to write some text inside rectangle.

How can I achieve this?

Here is my code:

class DrawPanel extends JPanel {

    private void doDrawing(Graphics g) {
        int a=90;
        int b=60;
        int c=10;
        int d=15;


        ArrayList<Graphics2D> g1 = new ArrayList<Graphics2D>();
        for(int i=0;i<=9;i++){

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(new Color(212, 212, 212));
        g2d.drawRect(c, d, a, b);



        d+=65;
        }
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        doDrawing(g);
    }
}
Robert
  • 10,403
  • 14
  • 67
  • 117
user3456343
  • 252
  • 3
  • 7
  • 21
  • 1
    everything inside doDrawing about Graphics2D shoould be in paintComponent, in doDrawing to create only coordinates, Objects, inside paintComponent only to loop inside arrray of prepared Objects – mKorbel Mar 25 '14 at 07:54
  • 1
    Use [`drawString`](http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawString(java.lang.String,int,int)) method. after `drawRect`. – alex2410 Mar 25 '14 at 07:57
  • 1
    Also consider `TextLayout`, seen [here](http://stackoverflow.com/a/4287269/230513). – trashgod Mar 25 '14 at 10:24

1 Answers1

3

Use your Graphics2D object and call drawString(String str, int x, int y). Something like

 g2d.drawRect(c, d, a, b);
 g2d.drawString("Hi", (a+c)/2, (b+d)/2);

Note that the Javadoc specifies

Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.

so you would need to take into account the space the font takes on screen. Use FontMetrics for that.

user1803551
  • 12,965
  • 5
  • 47
  • 74
  • It's the a and c you defined, it's just an example, I don't know if that's where you want your String. – user1803551 Mar 25 '14 at 08:20
  • y parameter of drawString sets the baseline of the string, (b+d)/2 will align the string baseline to the center vertically, not the text itself. – Gee Bee Nov 01 '16 at 23:05
  • @GeeBee Where in the question or the answer does it say anything about setting the string to the center? I even said in the comment above yours that I don't know where OP wanted the string. That's no reason to downvote. – user1803551 Nov 02 '16 at 01:52
  • @user1803551 you are right, I missed the point that centering was not requested. It still worth to mention that the y coordinate in drawString sets the _baseline_ of the text, instead of the top-left corner (as in other drawing operations such as drawRect). You need to add the ascent of the font (from the fontmetrics - then linemetrics structures) if you wish to have the text really rendered inside the rectangle (and not partially above or over the top line). – Gee Bee Nov 02 '16 at 14:32
  • @GeeBee Added the info. – user1803551 Nov 03 '16 at 02:14
  • Great! :) sorry for the confusion. – Gee Bee Nov 03 '16 at 21:17