JAVA学习网首页 把生活查询网加入收藏 把JAVA学习网设为首页 联系方式
    Hi,JAVA学习
JAVA基础 设计模式 数据库 JavaBeans J2EE JavaDelphi 用户界面 综合文章  
如何在按钮上绘画图形(如何利用paintComponent)
时间:22/04/2007
作者:网络
来源:网络
小提示点这里把文章加入您的收藏夹,方便下次查看
设置文章字体大小:[ ]
该组件可以任意在按钮上绘画你需要的形状(Shape),具体代码如下:


import javax.swing.*;
import java.awt.*;

public class JXButton extends JButton
{
    final public static int UP      = 0;
    final public static int DOWN    = 1;
    final public static int LEFT    = 2;
    final public static int RIGHT   = 3;
    
    final private static int SPACES = 3; 
    private Dimension size = new Dimension(20,10);
    private int type = 0;
    public JXButton(Dimension size)
    {
        super();
        this.size = size;
        setFocusPainted(false);
        //setBorderPainted(false);
    }
    public JXButton(int type,int width,double height)
    {
        this(new Dimension(width,(int)height));
        this.type = type;        
    }
    public JXButton(int width,int height)
    {        
        this(new Dimension(width,height));
    }
    public JXButton(int type,int width,int height)
    {
        this(new Dimension(width,height));
        this.type = type;        
    }
    public Dimension getPreferredSize()
    {
        return size;
    }
    public void setDisplay(int type)
    {
        this.type = type;
        repaint();
    }    
    public void paintComponent(Graphics g)
    {
        Dimension ds = getSize();
        
        int x0 = -1,y0 = -1;
        int x1 = -1,y1 = -1;
        int x2 = -1,y2 = -1;
        
        g.clearRect(0,0,ds.width,ds.height);
        switch(type)
        {
            case UP:
                x0 = ds.width / 2      ; y0 = SPACES;
                x1 = SPACES            ; y1 = ds.height - SPACES;
                x2 = ds.width - SPACES ; y2 = ds.height - SPACES;
                break;
            case DOWN:
                x2 = 1;                ; y2 = SPACES;
                x0 = ds.width / 2      ; y0 = ds.height - SPACES;
                x1 = ds.width - SPACES ; y1 = SPACES;
                break;
            case LEFT:
                x0 = SPACES            ; y0 = ds.height / 2;
                x1 = ds.width - SPACES ; y1 = ds.height - SPACES;
                x2 = ds.width - SPACES ; y2 = SPACES;        
                break;
            case RIGHT:    
                x1 = SPACES            ; y1 =SPACES;
                x2 = SPACES            ; y2 = ds.height - SPACES;
                x0 = ds.width - SPACES ; y0 = ds.height / 2;
                break;            
        }
        //根据三点坐标开始画三角形
        if(x1==x2 && x1>0)
        {
            //横坐标相等
            for(int i = Math.min(y1,y2);i<=Math.max(y1,y2);i++)
            {
                g.drawLine(x0,y0,x1,i);
            }
        }
        else if(y1==y2 && y1>0)
        {
            //纵坐标相等
            for(int i = Math.min(x1,x2);i<=Math.max(x1,x2);i++)
            {
                g.drawLine(x0,y0,i,y1);
            }
        }
        g.drawLine(x1,y1,x2,y2);
        //super.paintComponents(g);
    }
};
上一篇:如何实现类似于微软的IP输入框的输入框

下一篇:[Java]:创建接受格式化文本的输入域

  • Class Loading ---(类装载机制,开发者不得不知道的故事) --中篇
  • java多线程,什么时候让我跨过你高高的门槛
  • 六大代码问题检验你的Java知识
  • AOP和Template模式
  • 昨夜未眠--感悟(与程序无关!)
  • 学习和使用继承时先要清楚的三点
  • 如何在windows NT 下安装jboss为服务?(How to install jboss as Windows NT OS Service?)
  • Resin2.1.12的数据库连接池配置
  • Apusic1.1.2安装过程中遇到的jdk相关问题解答
  • 爪哇语言抽象工厂创立性模式介绍
  • 使用dao.Processor管理数据持久化对象
  • JSP和Struts解决用户退出问题
  • 在无线J2ME设备上实现超文本传输协议
  • 如何处理大数据量的查询
  • 一种新的设计方式
  • 关于基于XML描述的GUI应用的一些资源
  • Ball.java---BallGame
  • XWT 是一个我们需要重点关注的项目
  • 返回】 【顶部】 【关闭
    Copyright © 2005-2010 www.594k.com All Rights Reserved.
    版权所有:JAVA学习网 备案序号:皖ICP备06004238号