当前位置: 技术问答>java相关
java初级问题
来源: 互联网 发布时间:2015-01-07
本文导语: 源代码如下: import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class TranslateTest extends Applet { Image img; private Point pressed=new Point(),lastTranslate=new Point(); public void init() { Image img=getImage(getCodeBase(),"pu...
源代码如下:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class TranslateTest extends Applet
{
Image img;
private Point pressed=new Point(),lastTranslate=new Point();
public void init()
{
Image img=getImage(getCodeBase(),"pure.gif");
try
{
MediaTracker mt=new MediaTracker(this);
mt.addImage(img,0);
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
Point ioc=e.getPoint();
pressed.x=ioc.x-lastTranslate.x;
pressed.y=ioc.y-lastTranslate.y;
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
Point ioc=e.getPoint();
Point translate=new Point(ioc.x-pressed.x,ioc.y-pressed.y);
Graphics g=getGraphics();
try
{
g.clearRect(0,0,getSize().width,getSize().height);
g.translate(translate.x,translate.y);
showStatus("Translate Graphics:"+translate);
g.drawImage(img,0,0,TranslateTest.this);
}
finally
{
g.dispose();
}
lastTranslate=translate;
}
});
}
public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}
}
错误提示如下:
D:jdk1.2test>javac TranslateTest.java
TranslateTest.java:43: Attempt to use a non-final variable img from a different
method. From enclosing blocks, only final local variables are available.
g.drawImage(img,0,0,TranslateTest.this);
^
1 error
那位高手能解释一下这个错误发生的原因,谢谢
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class TranslateTest extends Applet
{
Image img;
private Point pressed=new Point(),lastTranslate=new Point();
public void init()
{
Image img=getImage(getCodeBase(),"pure.gif");
try
{
MediaTracker mt=new MediaTracker(this);
mt.addImage(img,0);
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
Point ioc=e.getPoint();
pressed.x=ioc.x-lastTranslate.x;
pressed.y=ioc.y-lastTranslate.y;
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
Point ioc=e.getPoint();
Point translate=new Point(ioc.x-pressed.x,ioc.y-pressed.y);
Graphics g=getGraphics();
try
{
g.clearRect(0,0,getSize().width,getSize().height);
g.translate(translate.x,translate.y);
showStatus("Translate Graphics:"+translate);
g.drawImage(img,0,0,TranslateTest.this);
}
finally
{
g.dispose();
}
lastTranslate=translate;
}
});
}
public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}
}
错误提示如下:
D:jdk1.2test>javac TranslateTest.java
TranslateTest.java:43: Attempt to use a non-final variable img from a different
method. From enclosing blocks, only final local variables are available.
g.drawImage(img,0,0,TranslateTest.this);
^
1 error
那位高手能解释一下这个错误发生的原因,谢谢
|
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class TranslateTest extends Applet
{
static Image img;
private Point pressed=new Point(),lastTranslate=new Point();
public void init()
{
Image img=getImage(getCodeBase(),"pure.gif");
try
{
MediaTracker mt=new MediaTracker(this);
mt.addImage(img,0);
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
Point ioc=e.getPoint();
pressed.x=ioc.x-lastTranslate.x;
pressed.y=ioc.y-lastTranslate.y;
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
Point ioc=e.getPoint();
Point translate=new Point(ioc.x-pressed.x,ioc.y-pressed.y);
Graphics g=getGraphics();
try
{
g.clearRect(0,0,getSize().width,getSize().height);
g.translate(translate.x,translate.y);
showStatus("Translate Graphics:"+translate);
g.drawImage(TranslateTest.this.img,0,0,TranslateTest.this);
}
finally
{
g.dispose();
}
lastTranslate=translate;
}
});
}
public void paint(Graphics g)
{
if(img==null)
img = getImage(getCodeBase(),"pure.gif");
g.drawImage(img,0,0,this);
}
}
import java.awt.*;
import java.awt.event.*;
public class TranslateTest extends Applet
{
static Image img;
private Point pressed=new Point(),lastTranslate=new Point();
public void init()
{
Image img=getImage(getCodeBase(),"pure.gif");
try
{
MediaTracker mt=new MediaTracker(this);
mt.addImage(img,0);
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
Point ioc=e.getPoint();
pressed.x=ioc.x-lastTranslate.x;
pressed.y=ioc.y-lastTranslate.y;
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
Point ioc=e.getPoint();
Point translate=new Point(ioc.x-pressed.x,ioc.y-pressed.y);
Graphics g=getGraphics();
try
{
g.clearRect(0,0,getSize().width,getSize().height);
g.translate(translate.x,translate.y);
showStatus("Translate Graphics:"+translate);
g.drawImage(TranslateTest.this.img,0,0,TranslateTest.this);
}
finally
{
g.dispose();
}
lastTranslate=translate;
}
});
}
public void paint(Graphics g)
{
if(img==null)
img = getImage(getCodeBase(),"pure.gif");
g.drawImage(img,0,0,this);
}
}
|
编译通过
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class TranslateTest extends Applet
{
Image img;
private Point pressed=new Point(),lastTranslate=new Point();
public void init()
{
img=getImage(getCodeBase(),"pure.gif");
try
{
MediaTracker mt=new MediaTracker(this);
mt.addImage(img,0);
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
Point ioc=e.getPoint();
pressed.x=ioc.x-lastTranslate.x;
pressed.y=ioc.y-lastTranslate.y;
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
Point ioc=e.getPoint();
Point translate=new Point(ioc.x-pressed.x,ioc.y-pressed.y);
Graphics g=getGraphics();
try
{
g.clearRect(0,0,getSize().width,getSize().height);
g.translate(translate.x,translate.y);
showStatus("Translate Graphics:"+translate);
g.drawImage(img,0,0,TranslateTest.this);
}
finally
{
g.dispose();
}
lastTranslate=translate;
}
});
}
public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class TranslateTest extends Applet
{
Image img;
private Point pressed=new Point(),lastTranslate=new Point();
public void init()
{
img=getImage(getCodeBase(),"pure.gif");
try
{
MediaTracker mt=new MediaTracker(this);
mt.addImage(img,0);
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
Point ioc=e.getPoint();
pressed.x=ioc.x-lastTranslate.x;
pressed.y=ioc.y-lastTranslate.y;
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
Point ioc=e.getPoint();
Point translate=new Point(ioc.x-pressed.x,ioc.y-pressed.y);
Graphics g=getGraphics();
try
{
g.clearRect(0,0,getSize().width,getSize().height);
g.translate(translate.x,translate.y);
showStatus("Translate Graphics:"+translate);
g.drawImage(img,0,0,TranslateTest.this);
}
finally
{
g.dispose();
}
lastTranslate=translate;
}
});
}
public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}
}
|
呵呵,你多写了几个字符:
Image img=getImage(getCodeBase(),"pure.gif");
我想应该是 img=getImage(getCodeBase(),"pure.gif");
吧。
错误的意思是img如果在内嵌类里使用,必须定义为final。也就是说应该类里,而不是函数init里。
Image img=getImage(getCodeBase(),"pure.gif");
我想应该是 img=getImage(getCodeBase(),"pure.gif");
吧。
错误的意思是img如果在内嵌类里使用,必须定义为final。也就是说应该类里,而不是函数init里。