今天开始写第一个图形程序了...呵呵,感觉JAVA就是有很多优点,

JAVA中不论是触发事件都是以类和接口实现的,因而比起C++来有更具有面向对象.................

呵呵,不过还没有接触事件模型,但是对于事件模型应该是理解了.....

java 代码
  1. /*  
  2.  * FontTest.java  
  3.  *  
  4.  * Created on 2007年9月16日, 下午7:25  
  5.  *  
  6.  * To change this template, choose Tools | Template Manager  
  7.  * and open the template in the editor.  
  8.  */  
  9.   
  10. package visualswt;   
  11. import java.awt.*;   
  12. import java.awt.font.*;   
  13. import java.awt.geom.*;   
  14. import javax.swing.*;   
  15. /**  
  16.  *  
  17.  * @author 龙斌  
  18.  */  
  19. public class FontTest {   
  20.        
  21.     /** Creates a new instance of FontTest */  
  22.     public static void main(String[] args) {   
  23.     FontFrame frame=new FontFrame();   
  24.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  25.     frame.show();   
  26.     }   
  27.        
  28. }   
  29. class FontFrame extends JFrame   
  30. {   
  31.     public FontFrame(){   
  32.         setTitle("字体测试的用户界面程序");   
  33.         setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);   
  34.         FontPanel panel=new FontPanel();   
  35.         Container contentPane=getContentPane();   
  36.         contentPane.add(panel);   
  37.     }   
  38.     public static final int DEFAULT_WIDTH=500;   
  39.     public static final int DEFAULT_HEIGHT=200;   
  40. }   
  41. class FontPanel extends JPanel   
  42. {   
  43.     public void paintComponent(Graphics g){   
  44.         super.paintComponent(g);   
  45.         Graphics2D g2=(Graphics2D)g;   
  46.         String message="我也写个用户界面";   
  47.         Font f=new Font("华文行草",Font.BOLD,50);   
  48.         g2.setFont(f);   
  49.         FontRenderContext context=g2.getFontRenderContext();   
  50.         Rectangle2D bounds=f.getStringBounds(message,context);   
  51.         double x=(getWidth()-bounds.getWidth())/2;   
  52.         double y=(getHeight()-bounds.getHeight())/2;   
  53.         double ascent=-bounds.getY();   
  54.         double baseY=y+ascent;   
  55.         g2.drawString(message,(int)x,(int)baseY);   
  56.         g2.setPaint(Color.RED);   
  57.         g2.draw(new Line2D.Double(x,baseY,(x+bounds.getWidth()),baseY));   
  58.         Rectangle2D rect=new Rectangle2D.Double(x,y,bounds.getWidth(),bounds.getHeight());   
  59.         g2.draw(rect);   
  60.     }   
  61. }   
  62.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值