问答题

下面是一个Applet程序,其功能是将完整的图像显示于Applet的区块中,然后可以通过拖动鼠标让图像随着鼠标拖动的轨迹而移动。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。
源程序文件代码清单如下:
import java.applet.*;
import java.awt.*;
/*
<applet code="ex7_3.class" width=800 height=400 >
</applet>
*/
public class ex7_3 extends Applet
private Image iImg;
private int xPos,yPos;
public void init()

xPos = yPos = 0;
iImg = getImage("ex7_3.jpg");

public void paint(Graphics g)

g.drawImage(iImg,xPos,yPos);

public boolean mouseDrag(Event e,int x, int y)

xPos = x;
yPos = y;
paint();
return true;

ex7_3.html
<HTML>
<HEAD>
<TITLE>ex7_3</TITLE>
</HEAD>
<BODY>
<applet code="ex7_3.class" width=800 height=400 >
</applet>
</BODY>
</HTML>

【参考答案】

iImg=getImage(getDocumentBase(),"ex7_3.jpg")
g.drawImaS......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
以下程序中,当用户单击“移动”按钮以后,就可以使用方向键控制屏幕上句子的移动,单击“停止”按钮,则句子不再随着方向键移动。运行结果如下图所示 注意:请勿改动其他已有语句内容,仅在横线处填入适当语句。 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example2_8 extends Applet implements KeyListener public void keyTyped(KeyEvent e) public void keyReleased(KeyEvent e) Button button; Button stopButton; Label out; int x,y; public void _______ () button = new Button( 移动 ); button.addActionListener(new AddMoveListener(this)); stopButton = new Button( 停止移动 ); stopButton.addActionListener(new RemoveListener(this)); stopButton.setEnabled(false); out = new nabel( 按下按钮以后我可以随方向键移动 ); add(button); add(stopButton); add (out); public void start() super, start (); public void keyPressed(KeyEvent e) x=out.getBounds().x; y=out.getBounds().y; if(e.getKeyCode()==KeyEvent.VK_UP) y=y-2; if(y<=0) y=0; out. setLocation (x, y); else if(e.getKeyCode()==KeyEvent.VK_DOWN) y=y+2; if (y>=300) y=300; out. setLocation (x, y); else if(e.getKeyCode()==KeyEvent.VK_LEFT) x=x-2; if(x<=0) x=0; out. setLocation (x, y); else if(e.getKeyCode()==KeyEvent.VK_RiGHT) x=x+2; if(x>=300) x=300; out. setLocation (x, y); class AddMoveListener implements ActionListener Example2_8 lis; public AddMoveListener(Example2_8 lis) this.lis = lis; public void actionPerformed(ActionEvent e) button. _________(lis); stopButton, setEnabled (true); class RemoveListener implements ActionListener Example2_8 lis; public RemoveListener(Example2_8 lis) this.lis = lis; public void actionPerformed(ActionEvent e) button, removeKeyListener (lis); stopButton.setEnabled(false); Example2_8. html <HTML> <HEAD> <TITLE> Example2_8< TITLE> < HEAD> <BODY> <APPLET CODE= Example2_8.class WIDTH=400 HEIGHT=500> < APPLET> < BODY> < HTML>