今天做了一个边框类,很简单,继承自 AbstractBorder, 一般如果我们要写自己的边框类,大部分情况下我们都会选从这个类开始继承。
AbstractBorder类是Border接口的默认实现,Border接口有三个抽象方法:
(1)
public void paintBorder(Component c,int x, int y,int width,int height);
(2)
public Insets getBorderInset(Component c);
(3)
public abstract boolean isBorderOpaque()
这里需要说明一点的是,第一个方法的p是小写的,因为这个在今天下午也误了我一些时间。
如果我们写边框类的话,一般我们要重写第一个方法。
以下是我从书中看到的一个例子:
永远不要在组件区域绘制边框,是创建边框的一条黄金准则。
public class WrongBorder extends AbstractBorder{
public WrongBorder( ){
}
public void paintBorder(Component c,Graphics g,int x,int y,int width,int height){
g.setColor(Color.BLACK);
g.fillRect(x,y,width,height); //bad
}
public boolean isBorderOpaque(){
retrun true;
}
public Insets isBorderInserts(Componet c){
return new Insets(20,20,20,20);
}
}
应该为以下这样:
public void paintBorder(Componet c,Graphic g,int x,int y,int width,int height){
Insets insets=getBorderInsets(c);
g.setColor(Color.BLACK);
g.fillRect(x,y,width,insets.top);
g.fillRect(x,y,insets.left,height);
g.fillRect(x+width-insets.right,y,insets.right,height);
g.fillRect(x,y+height-insets.bottom,width,insets.bottom);
}
今天下午我做的不是这样例子:做成的边框如下
code:
import javax.swing.border.AbstractBorder;
import java.awt.*;
import java.awt.image.BufferedImage;
public class ImageBorder extends AbstractBorder{
Image top_center,top_left,top_right;
Image left_center,right_center;
Image bottom_left,bottom_center,bottom_right;
Insets insets;
public ImageBorder(Image top_left, Image top_center, Image top_right,
Image left_center, Image right_center,
Image bottom_left, Image bottom_center,Image bottom_right) {
super();
this.top_center = top_center;
this.top_left = top_left;
this.top_right = top_right;
this.left_center = left_center;
this.right_center = right_center;
this.bottom_left = bottom_left;
this.bottom_center = bottom_center;
this.bottom_right = bottom_right;
}
public void setInsets(Insets insets){
this.insets=insets;
}
public Insets getBorderInsets(Component c){
if(insets!=null){
return insets;
}else{
return new Insets(top_center.getHeight(null),left_center.getWidth(null),
bottom_center.getHeight(null),right_center.getWidth(null));
}
}
public BufferedImage createBufferedImage(Image img){
BufferedImage buff=new BufferedImage(img.getWidth(null), img.getHeight(null),BufferedImage.TYPE_INT_ARGB);
Graphics gfx=buff.createGraphics();
gfx.drawImage(img, 0, 0 , null);
gfx.dispose();
return buff;
}
public void fillTexture(Graphics2D g2,Image img,int x,int y, int w,int h){
BufferedImage buff=createBufferedImage(img);
Rectangle anchor=new Rectangle(x,y,img.getWidth(null),img.getHeight(null));
TexturePaint paint=new TexturePaint(buff,anchor);
g2.setPaint(paint);
g2.fillRect(x, y, w, h);
}
public void paintBorder(Component c,Graphics g,int x,int y,int width,int height){
Insets insets=getBorderInsets(c);
g.setColor(Color.black);
g.fillRect(x, y, width, height);
Graphics2D g2=(Graphics2D)g;
int tlw=top_left.getWidth(null);
int tlh=top_left.getHeight(null);
int tcw=top_center.getWidth(null);
int tch=top_center.getHeight(null);
int trw=top_right.getWidth(null);
int trh=top_right.getHeight(null);
int lcw=left_center.getWidth(null);
int lch=left_center.getHeight(null);
int rcw=right_center.getWidth(null);
int rch=right_center.getHeight(null);
int blw=bottom_left.getWidth(null);
int blh=bottom_left.getHeight(null);
int bcw=bottom_center.getWidth(null);
int bch=bottom_center.getHeight(null);
int brw=bottom_right.getWidth(null);
int brh=bottom_right.getHeight(null);
fillTexture(g2,top_left,x,y,tlw,tlh);
fillTexture(g2,top_center,x+tlw,y,width-tlw-trw,tch);
fillTexture(g2,top_right,x+width-trw,y,trw,trh);
fillTexture(g2,left_center,x,y+tlh,lcw,height-tlh-blh);
fillTexture(g2,right_center,x+width-rcw,y+trh,rcw,height-trh-brh);
fillTexture(g2,bottom_left,x,y+height-blh,blw,blh);
fillTexture(g2,bottom_center,x+blw,y+height-bch,width-blw-brw,bch);
fillTexture(g2,bottom_right,x+width-brw,y+height-brh,brw,brh);
}
}
测试code:
import javax.swing.*;
public class TestImageBorder {
public static void main(String[] args){
JFrame frame=new JFrame("ImageBorder");
JPanel panel=new JPanel();
JButton button=new JButton("Test Image Border ");
panel.add(button);
ImageBorder imageBorder=new ImageBorder(
new ImageIcon("images/top_left.png").getImage(), new ImageIcon("images/top_center.png").getImage(),
new ImageIcon("images/top_right.png").getImage(), new ImageIcon("images/left_center.png").getImage(),
new ImageIcon("images/right_center.png").getImage(), new ImageIcon("images/bottom_left.png").getImage(),
new ImageIcon("images/bottom_center.png").getImage(), new ImageIcon("images/bottom_right.png").getImage()
);
panel.setBorder(imageBorder);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
就因为我把这八张图片放错了位置,所以img.getWidth()和img.getHeight()方法返回的全是否-1。
所以程序一直报错。这也只怪自己没有好好理解相对位置的概念。
如果我们在eclipse下建了一个项,比如说项目名不CSDN,那么我们上面的程序用到了相对位置为
images/....这个images文件夹应该在哪里呢?
给你三个选择:
(1).CSDN 目录下
(2).CSDN/src目录下
(3).CSDN/bin下
我一直以为是在bin下,因为编译后的文件就放在这个文件夹下。
但结果答案却是一。在CSDN目录下。
这也就是误了我的原因,只怪自己以前没好好理解这个问题。
想想自己也真笨, img.getWidth()这个方法返回-1,肯定是没找到这个img图片,那么肯定是路径的问题,可惜当时没想到。