Posted By

DrPepper on 01/05/11


Tagged


Versions (?)

[JAVA] CH2 Rectangle


 / Published in: Java
 

if you want to display numbers on the screen with drawString, you need to say "" + variableName. the "" stand for an empty string, therefore you can use drawString for displaying other than string

  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Rectangle;
  5. import javax.swing.JComponent;
  6.  
  7. public class PC1 extends JComponent
  8. {
  9. public void paintComponent(Graphics g)
  10. {
  11. Rectangle box = new Rectangle(60,90,100,150);
  12. g2.setColor(Color.GREEN);
  13. g2.fill(box);
  14.  
  15. double width = box.getWidth();
  16. double height = box.getHeight();
  17.  
  18. g2.setColor(Color.BLUE);
  19. g2.drawString("Width : ",50,260);
  20. g2.drawString("" + width,100,260);
  21. g2.drawString("Height : ",50,270);
  22. g2.drawString("" + height,100,270);
  23.  
  24. }
  25. }

Report this snippet  

You need to login to post a comment.