为什么我的矩形位会被切去正方形时

为什么我的矩形位会被切去正方形时

本文介绍了为什么我的矩形位会被切去正方形时,我画的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义字段,使一个图像按钮。图像绘制与W和H等于一个盒子,但图像的高度几乎是两倍的宽度。我跑了调试器和正确的W,H正在落实 g.drawBitmap(0,0,W,H,图像,0,0)。是你解决这个问题的方法吗?

公共类cPictureButton扩展字段{   私人位图图像;   公共cPictureButton(位图图像​​,长样式)
   {
       超(样式);       this.image =图像;
   }   公众诠释的get preferredHeight()
   {
       返回image.getHeight();
       //返回的getFont()的getHeight()。
   }   公众诠释的get preferredWidth()
   {
       返回image.getWidth();
       //返回的getFont()getAdvance(标签)+8。
   }   保护无效DRAWFOCUS(图形克,布尔)
   {
   }
   保护无效漆(图形G)
   {
       INT W = image.getWidth();
       INT H = image.getHeight();
       g.drawBitmap(0,0,W,H,图像,0,0);
       如果(isFocus())
           g.drawRect(0,0,image.getWidth(),image.getHeight());
   }   保护无效布局(INT宽度,高度INT){
       setExtent(Math.min(宽度,GET preferredWidth()),
                 Math.min(身高,GET preferredWidth()));
   }
   公共布尔isFocusable(){
       返回true;
   }
   保护布尔navigationClick(INT的地位,诠释时间)
   {
       fieldChangeNotify(0);
       返回true;
   }}


解决方案

复制和粘贴可能在没有你的第二个引setExtent应该调用get preferredHeight():

  setExtent(Math.min(宽度,GET preferredWidth()),
        Math.min(身高,GET preferredWidth()))

I’m creating a custom field to make a image button. The image is drawn as a box with the w and h equal, but the image's height is almost double the width. I ran the debugger and the correct w,h are being put in g.drawBitmap(0, 0, w, h, image, 0, 0). Is thee a way to fix this?

public class cPictureButton extends Field{

   private Bitmap image;

   public cPictureButton( Bitmap image, long style)
   {
       super(style);

       this.image=image;
   }

   public int getPreferredHeight()
   {
       return   image.getHeight();
       //   return getFont().getHeight();
   }

   public int getPreferredWidth()
   {
       return   image.getWidth();
       //   return getFont().getAdvance(label)+8;
   }

   protected void drawFocus(Graphics g, boolean on)
   {
   }


   protected void paint(Graphics g)
   {
       int w=image.getWidth();
       int h=image.getHeight();
       g.drawBitmap(0, 0, w, h, image, 0, 0);
       if (isFocus() )
           g.drawRect(0,0,image.getWidth(), image.getHeight());
   }

   protected void layout(int width, int height) {
       setExtent(Math.min(width, getPreferredWidth()),
                 Math.min(height, getPreferredWidth()));
   }


   public boolean isFocusable() {
       return true;
   }
   protected boolean navigationClick(int status, int time)
   {
       fieldChangeNotify(0);
       return true;
   }

}
解决方案

Copy and paste probably did you in. Second argument to setExtent should call getPreferredHeight():

  setExtent(Math.min(width, getPreferredWidth()),
        Math.min(height, getPreferredWidth()))

这篇关于为什么我的矩形位会被切去正方形时,我画的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:21