Image 的 getWidth()方法误了我几个小时

今天做了一个边框类,很简单,继承自 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图片,那么肯定是路径的问题,可惜当时没想到。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Android中,给背景图增加圆角的方法有以下几种: 1. 使用XML实现背景圆角效果:可以在res/drawable目录下创建一个圆角矩形的shape文件,并将其设置为View或ViewGroup的背景。例如: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp" /> <solid android:color="#FF0000" /> </shape> // 将shape文件设置为View的背景 view.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rounded)); ``` 2. 使用BitmapShader实现背景圆角效果:可以通过将图片转换为BitmapShader,并设置给Paint的Shader来实现圆角效果。例如: ``` // 将图片转换为Bitmap对象 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); // 将Bitmap对象转换为BitmapShader BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); // 创建一个Paint对象并设置Shader Paint paint = new Paint(); paint.setShader(shader); // 创建一个圆角矩形的Path对象 Path path = new Path(); RectF rectF = new RectF(0, 0, getWidth(), getHeight()); path.addRoundRect(rectF, 10, 10, Path.Direction.CW); // 在onDraw()方法中使用Canvas的drawPath()方法绘制圆角背景 canvas.drawPath(path, paint); ``` 3. 使用第三方库实现背景圆角效果:可以使用一些开源库如RoundedImageView、Glide、Picasso等提供的圆角图片加载方法,使用起来更加方便。例如: ``` // 使用Glide加载圆角图片 Glide.with(context) .load(imageUrl) .transform(new RoundedCorners(10)) .into(imageView); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值