Results 1 to 4 of 4
Thread: Draw String in Rectangle
- 05-18-2009, 03:45 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
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
- 05-18-2009, 04:03 PM #2
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
- 05-18-2009, 10:27 PM #3
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); } }
- 05-20-2009, 07:05 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
Similar Threads
-
non-rectangle JPanel
By itaipee in forum AWT / SwingReplies: 4Last Post: 04-30-2009, 11:58 PM -
how to draw a fill rectangle using mouse and paintComponent?
By java_fun2007 in forum New To JavaReplies: 7Last Post: 04-14-2009, 07:12 PM -
How to draw a rectangle in the JPanel by inserting the X and Y
By ngansamuel in forum New To JavaReplies: 2Last Post: 03-22-2009, 01:53 PM -
How to Draw Unicode String in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:15 PM -
How to Draw a Rectangle in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks