今天做了一个关于Graphics 对象的小程序,主要是对java自定义控件的应用,即程序中的圆形,五角形以及月牙形按钮,不过五角星和半圆形存在一些问题,在这里把代码贴上来,希望喜欢java GUI的朋友一起来探讨,也算是向大家请教吧。
程序中的不足:五角星按钮点击周围时有的地方会触发按钮的点击事件,还有月牙左边的弧怎样能够成为一个真正的弧,而非像现在这样感觉像是连接三个点的两条线,再有月牙的标签没能实现居中,具体是坐标算法有问题还是有什么特殊的地方需要处理,还请大家指点。
运行效果如图:
圆形按钮
package MyControls;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/** *//*********************圆形按钮控件******************************/
public class Sun extends JButton {
private Shape shape;
public Sun(String lable) {
super(lable);
Dimension size = this.getPreferredSize();//获取按钮的最佳大小
size.width = size.height = Math.max(size.width,size.height);//使按钮变成一个正方形
this.setPreferredSize(size);
this.setContentAreaFilled(false);//使JButton不画背景
}
//画圆的背景和标签
protected void paintComponent(Graphics g){
if(this.getModel().isArmed()){
g.setColor(Color.BLUE);
}else{
g.setColor(Color.red);
}
g.fillOval(0,0,getSize().width - 1,getSize().height - 1);
super.paintComponent(g);
}
//画按钮的边框
protected void paintBorder(Graphics g){
g.setColor(this.getForeground());
g.drawOval(0,0,getSize().width - 1,getSize().height - 1);
}
//判断鼠标点击坐标是否在按钮上
public boolean contains(int x,int y){
if(shape == null || (!shape.getBounds().equals(getBounds()))){
shape = new Ellipse2D.Float(0,0,getWidth(),getHeight());
}
return shape.contains(x,y);
}
}
月牙按钮代码
package MyControls;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/** *//*********************月牙形按钮控件******************************/
public class Moon extends JButton {
private Shape shape;
public Moon(String lable) {
super(lable);
Dimension size = this.getPreferredSize();//获取按钮的最佳大小
size.width = size.height = Math.max(size.width,size.height);//使按钮变成一个正方形
this.setPreferredSize(size);
this.setContentAreaFilled(false);//使JButton不画背景
}
//画圆的背景和标签
protected void paintComponent(Graphics g){
if(this.getModel().isArmed()){
g.setColor(Color.BLUE);
}else{
g.setColor(Color.yellow);
}
g.fillArc(0,0,getSize().width - 1,getSize().height - 1,-120,250);
super.paintComponent(g);
}
//画按钮的边框
protected void paintBorder(Graphics g){
g.setColor(this.getForeground());
g.drawArc(0,0,getSize().width - 1,getSize().height - 1,-120,250);
}
//判断鼠标点击坐标是否在按钮上
public boolean contains(int x,int y){
if(shape == null || (!shape.getBounds().equals(getBounds()))){
shape = new Ellipse2D.Float(0,0,getWidth(),getHeight());
}
return shape.contains(x,y);
}
}
五星按钮代码
package MyControls;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/** *//*********************星形按钮控件******************************/
public class Star extends JButton {
private Shape shape;
private int r1=50; //外接大圆半径
private int r0=(int)(r1*Math.cos(72*Math.PI/180)); //内圆半径
// 五角星10个点的横坐标
private int[] xrr = {
(int) 0, (int) (r0 * Math.cos(54 * Math.PI / 180)),
(int) (r1 * Math.sin(72 * Math.PI / 180)),
(int) (r0 * Math.cos(18 * Math.PI / 180)),
(int) (r1 * Math.cos(54 * Math.PI / 180)), (int) 0,
(int) ( ( -r1) * Math.cos(54 * Math.PI / 180)),
(int) ( ( -r0) * Math.cos(18 * Math.PI / 180)),
(int) ( ( -r1) * Math.sin(72 * Math.PI / 180)),
(int) ( ( -r0) * Math.cos(54 * Math.PI / 180))
};
//五角星10个点的纵坐标
private int[] yrr = {
(int) - r1, (int) ( -r0 * Math.sin(54 * Math.PI / 180)),
(int) ( -r1 * Math.cos(72 * Math.PI / 180)),
(int) ( (r0) * Math.sin(18 * Math.PI / 180)),
(int) ( (r1) * (Math.sin(54 * Math.PI / 180))), (int) (r0),
(int) ( (r1) * (Math.sin(54 * Math.PI / 180))),
(int) ( (r0) * Math.sin(18 * Math.PI / 180)),
(int) ( -r1 * Math.cos(72 * Math.PI / 180)),
(int) ( -r0 * Math.sin(54 * Math.PI / 180)),
};
public Star(String lable) {
super(lable);
Dimension size = this.getPreferredSize(); //获取按钮的最佳大小
size.width = size.height = Math.max(size.width, size.height); //使按钮变成一个正方形
this.setPreferredSize(size);
this.setContentAreaFilled(false); //使JButton不画背景
this.drawStar();
}
private void drawStar(){
for (int i = 0; i < xrr.length; i++) { //计算大五角星坐标平移
xrr[i] += 50;
yrr[i] += 60;
}
}
//画圆的背景和标签
protected void paintComponent(Graphics g){
if(this.getModel().isArmed()){
g.setColor(Color.BLUE);
}else{
g.setColor(Color.yellow);
}
g.fillPolygon(xrr,yrr,10);
super.paintComponent(g);
}
//画按钮的边框
protected void paintBorder(Graphics g){
g.setColor(this.getForeground());
g.drawPolygon(xrr,yrr,10);
}
//判断鼠标点击坐标是否在按钮上
public boolean contains(int x,int y){
if(shape == null || (!shape.getBounds().equals(getBounds()))){
shape = new Ellipse2D.Float(0,0,getWidth(),getHeight());
}
return shape.contains(x,y);
}
}
窗体代码
package MyControls;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SkyOfNight extends JFrame implements ActionListener{
private JButton btnSun,btnMoon,btnStar;
public SkyOfNight() {
try {
jbInit();
//设置窗体的背景颜色
//this.getContentPane().setBackground(Color.BLACK);
//创建圆形按钮实例并画到窗体中
btnSun = new Sun("Sun");
btnSun.setBackground(Color.RED);
btnSun.setBounds(new Rectangle(50,50,70,70));
btnSun.addActionListener(this);
this.getContentPane().add(btnSun);
//创建月牙形按钮实例并画到窗体中
btnMoon = new Moon("Moon");
btnMoon.setBackground(Color.RED);
btnMoon.setBounds(new Rectangle(250,200,80,80));
btnMoon.addActionListener(this);
this.getContentPane().add(btnMoon);
//创建星形按钮实例并画到窗体中
btnStar = new Star("Star");
btnStar.setBackground(Color.RED);
btnStar.setBounds(new Rectangle(50,200,105,120));
btnStar.addActionListener(this);
this.getContentPane().add(btnStar);
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
this.setResizable(false);
this.setTitle("SkyOfNight");
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == btnSun){
JOptionPane.showMessageDialog(this,"I am Sun!","Prompt",JOptionPane.INFORMATION_MESSAGE);
return;
}else if(e.getSource() == btnMoon){
JOptionPane.showMessageDialog(this,"I am Moon!","Prompt",JOptionPane.INFORMATION_MESSAGE);
return;
}else if(e.getSource() == btnStar){
JOptionPane.showMessageDialog(this,"I am Star!","Prompt",JOptionPane.INFORMATION_MESSAGE);
return;
}
}
public static void main(String[] args) {
SkyOfNight skyofnight = new SkyOfNight();
skyofnight.setSize(400,400);
skyofnight.setLocation(300,200);
skyofnight.setVisible(true);
skyofnight.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
点击下载源代码:/Files/daizhenghenry/skyOfNight.rar