首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

J2ME 调用Graphics的copyArea()抛出错误IllegalStateException

2012-03-07 
J2ME 调用Graphics的copyArea()抛出异常IllegalStateException编译环境如下:winXPSP2+JBuilder2006jdk1.5,

J2ME 调用Graphics的copyArea()抛出异常IllegalStateException
编译环境如下:
winXP   SP2   +   JBuilder2006
jdk1.5,j2mewtk2.2,全部JBuilder2006自带


源代码如下:
package   iavatest;

import   javax.microedition.midlet.MIDlet;
import   javax.microedition.midlet.MIDletStateChangeException;
import   javax.microedition.lcdui.*;
import   java.lang.*;


/**
  *   <p> Title:   </p>
  *
  *   <p> Description:   </p>
  *
  *   <p> Copyright:   Copyright   (c)   2007 </p>
  *
  *   <p> Company:   </p>
  *
  *   @author   not   attributable
  *   @version   1.0
  */
public   class   GraphicsTest   extends   MIDlet   {
        private   Display   display;
        private   MyGraphicsCanvas   myGraphicsCanvas;
        public   GraphicsTest()   {
                display   =   Display.getDisplay(this);
                myGraphicsCanvas   =   new   MyGraphicsCanvas();
        }

        /**
          *   destroyApp
          *
          *   @param   _boolean   boolean
          *   @throws   MIDletStateChangeException
          *   @todo   Implement   this   javax.microedition.midlet.MIDlet   method
          */
        protected   void   destroyApp(boolean   _boolean)   throws
                        MIDletStateChangeException   {
        }

        /**
          *   pauseApp
          *
          *   @todo   Implement   this   javax.microedition.midlet.MIDlet   method
          */
        protected   void   pauseApp()   {
        }

        /**
          *   startApp
          *
          *   @throws   MIDletStateChangeException
          *   @todo   Implement   this   javax.microedition.midlet.MIDlet   method
          */
        protected   void   startApp()   throws   MIDletStateChangeException   {
                display.setCurrent(myGraphicsCanvas);
        }
}

class   MyGraphicsCanvas   extends   Canvas   {
        Image   img;
        public   MyGraphicsCanvas(){
                try{
                        img   =   Image.createImage( "/globe.png ");


                }   catch(Exception   e){
                }
        }
        protected   void   paint(Graphics   g){
                g.setColor(255,255,255);
                g.fillRect(0,0,getWidth(),getHeight());
                g.setColor(0,0,0);

                g.drawLine(5,10,55,60);
                g.fillRect(65,10,50,50);
                g.drawRect(125,10,50,50);
                g.drawRoundRect(185,10,50,50,10,10);
                g.fillRoundRect(5,70,50,50,10,10);
                g.drawArc(65,70,50,50,0,130);
                g.fillArc(125,70,50,50,130,240);

                String   str   =   new   String( "This   is   String ");
                char[]   chars   =   str.toCharArray();
                g.drawString(str,5,130,g.TOP|g.LEFT);
                g.drawSubstring(str,0,10,5,140,g.TOP|g.LEFT);
                g.drawChar( 'a ',5,150,g.TOP|g.LEFT);
                g.drawChars(chars,0,11,5,160,g.TOP|g.LEFT);

                g.drawImage(img,100,130,g.TOP|g.LEFT);
                g.drawRegion(img,0,0,20,20,0,150,130,g.TOP|g.LEFT);
                g.copyArea(65,10,50,50,15,200,g.TOP|g.LEFT);
        }
}

错误信息如下:
D:\Borland\JBuilder2006\j2mewtk2.2\bin\emulator.exe   -Xdevice:DefaultColorPhone     -Xdescriptor: "F:\IavaTest\GraphicsTest\GraphicsTest.jad "
正在通过存储根   DefaultColorPhone   来运行  
java.lang.IllegalStateException
at   javax.microedition.lcdui.Graphics.copyArea(+14)
at   iavatest.MyGraphicsCanvas.paint(+300)
at   javax.microedition.lcdui.Canvas.callPaint(+80)
at   javax.microedition.lcdui.Display.repaint(+77)
at   javax.microedition.lcdui.Display.registerNewCurrent(+237)
at   javax.microedition.lcdui.Display.access$400(+6)
at   javax.microedition.lcdui.Display$DisplayAccessor.foregroundNotify(+46)
at   javax.microedition.lcdui.Display$DisplayManagerImpl.notifyWantsForeground(+152)
at   javax.microedition.lcdui.Display$DisplayManagerImpl.access$100(+6)
at   javax.microedition.lcdui.Display.setCurrentImpl(+115)
at   javax.microedition.lcdui.Display.setCurrent(+29)
at   iavatest.GraphicsTest.startApp(+11)
at   javax.microedition.midlet.MIDletProxy.startApp(+7)
at   com.sun.midp.midlet.Scheduler.schedule(+270)
at   com.sun.midp.main.Main.runLocalClass(+28)
at   com.sun.midp.main.Main.main(+116)


[解决办法]
copyArea
public void copyArea(int x_src,
int y_src,
int width,


int height,
int x_dest,
int y_dest,
int anchor)Copies the contents of a rectangular area (x_src, y_src, width, height) to a destination area, whose anchor point identified by anchor is located at (x_dest, y_dest). The effect must be that the destination area contains an exact copy of the contents of the source area immediately prior to the invocation of this method. This result must occur even if the source and destination areas overlap.
The points (x_src, y_src) and (x_dest, y_dest) are both specified relative to the coordinate system of the Graphics object. It is illegal for the source region to extend beyond the bounds of the graphic object. This requires that:


x_src + tx > = 0
y_src + ty > = 0
x_src + tx + width <= width of Graphics object 's destination
y_src + ty + height <= height of Graphics object 's destination

where tx and ty represent the X and Y coordinates of the translated origin of this graphics object, as returned by getTranslateX() and getTranslateY(), respectively.

However, it is legal for the destination area to extend beyond the bounds of the Graphics object. Pixels outside of the bounds of the Graphics object will not be drawn.

The copyArea method is allowed on all Graphics objects except those whose destination is the actual display device. This restriction is necessary because allowing a copyArea method on the display would adversely impact certain techniques for implementing double-buffering.

Like other graphics operations, the copyArea method uses the Source Over Destination rule for combining pixels. However, since it is defined only for mutable images, which can contain only fully opaque pixels, this is effectively the same as pixel replacement.


Parameters:
x_src - the x coordinate of upper left corner of source area
y_src - the y coordinate of upper left corner of source area
width - the width of the source area
height - the height of the source area
x_dest - the x coordinate of the destination anchor point
y_dest - the y coordinate of the destination anchor point
anchor - the anchor point for positioning the region within the destination image
Throws:
IllegalStateException - if the destination of this Graphics object is the display device
IllegalArgumentException - if the region to be copied exceeds the bounds of the source image
Since:
MIDP 2.0

[解决办法]
翻译下楼上的关键部分:
Throws:
IllegalStateException - if the destination of this Graphics object is the display device
IllegalArgumentException - if the region to be copied exceeds the bounds of the source image

抛出:
非法状态异常 --如果Graphics对象的目标是显示设备

也就是说,你不能对Canvas中的Graphics来调用copyArea
只能来拷贝从Image对象获取的Graphics

热点排行