Results 1 to 4 of 4
  1. #1
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default Draw String in Rectangle

    hey guys,

    I need to be able to create multiple Rectangles (no problems) and draw them onto a Panel (again no problem), the issue I have is that I want to be able to draw a String in each of the Rectangles and set the background colour of the Rectangle

    How would I go about this?

    Thanks in advance,

    David

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Extend the class and override the painting method using Graphics.drawString().

    Alternatively, JLabel/Label might be what you're looking for (but you'll have to switch from AWT to Swing to use JLabel.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.AffineTransform;
    import javax.swing.*;
    
    public class TextRect extends JPanel {
        Rectangle[] rects = {
            new Rectangle( 60,  75, 100, 50),
            new Rectangle(100, 165, 200, 40),
            new Rectangle(165, 250, 150, 50)
        };
        String s = "hello world";
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            FontRenderContext frc = g2.getFontRenderContext();
            Font font = g2.getFont().deriveFont(16f);
            g2.setFont(font);
            float sw = (float)font.getStringBounds(s, frc).getWidth();
            LineMetrics lm = font.getLineMetrics(s, frc);
            float sh = lm.getAscent() + lm.getDescent();
            for(int i = 0; i < rects.length; i++) {
                Rectangle r = rects[i];
                g2.setPaint(Color.pink);
                g2.fill(r);
                g2.setPaint(Color.black);
                g2.draw(r);
                if(i < 2) {
                    // center text in r
                    float sx = r.x + (r.width - sw)/2;
                    float sy = r.y + (r.height + sh)/2 - lm.getDescent();
                    g2.drawString(s, sx, sy);
                } else {
                    // scale text to fit and center in r
                    double xScale = r.width/sw;
                    double yScale = r.height/sh;
                    double x = r.x + xScale*(r.width - xScale*sw)/2;
                    double y = r.getMaxY() - yScale*lm.getDescent();
                    AffineTransform at =
                        AffineTransform.getTranslateInstance(x, y);
                    at.scale(xScale, yScale);
                    g2.setFont(font.deriveFont(at));
                    g2.drawString(s, 0, 0);
                }
            }
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new TextRect());
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }

  4. #4
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    [SOLVED]
    hey mate,

    cheers for the code, really appreciate your help!

    David

Similar Threads

  1. non-rectangle JPanel
    By itaipee in forum AWT / Swing
    Replies: 4
    Last Post: 05-01-2009, 12:58 AM
  2. Replies: 7
    Last Post: 04-14-2009, 08:12 PM
  3. Replies: 2
    Last Post: 03-22-2009, 02:53 PM
  4. How to Draw Unicode String in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:15 AM
  5. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •