题目
第1题
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class java2 extends Applet
{Button b[]=new Button[10];
int x,Y;
public void init()
{for(int i=0;i<=9;i++)
{b[i]=new Button(""+i);
b[i].addKeyListener(this);
addCb[i]);
}
}
public void
{Button button=(Button)e.getSource();
x=button.getBounds().x;
y—button.getBounds().y;
if(e,getKeyCode()= =KeyEvent.VK_UP)
{y=y-2;
if(y<=O)y=0;
button.setLocation(x,y);
}
else if(e.getKeyCode()= =KeyEvent.VK_
DOWN)
{y=y+2;
if(y>=300)y=300;
button,.setLocation(X,y);
}
else if(e.getKeyCode()= =KeyEvent.VK_
LEFT)
{x=x-2;
if(x<=0)x=0;
button.setLocation(x,y);
}
else if(e.getKeyCode()= =KeyEvent.VK_
RIGHT)
(x=X+2;
if(x>=300)x=300;
button.setLoeation(X,y);
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
第2题
第3题
第7题
下面是一个Apple(程序,程序的功能是在Applet显示区内画一个动态的、多维的绿色椭圆环。本题主要是通过数学方法sin(),cos()画出一个圆环。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。程序的执行结果:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class YuanHuan extends Applet
{
public void paint(Graphics g)
{
double w=getSize().width/2;
double h=getSize().height/2;
g.getColor(Color.green);
for(double th=0;th<10;th+=0.00003)
{
double r=Math.cos(16*th)+th;
double x=r*Math.cos(th)+w;
double y=r*Math.sin(th)+h;
g.drawOval((int)x-1,(int)y-1,3,3);
}
}
public static void main(String args[ ])
{
Frame. f=new Frame("Draw");
YuanHuan p=new YuanHuan();
p.inti();
p.start();
f.add(p);
f.setSize(400,300);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.out.exit(0);
}
});
f. show ();
}
}
ex3 3_3. htm1:
<html>
<head>
<title>A Simple Program</title>
</head>
<body>
<applet code=" YuanHuan.class" width=800 height=400>
</applet>
</body>
</html>
第8题
第9题
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class java3 extends Applet implements ActionListener
{int x=-1,y=-1,rubberNote=0,clearNote=0;
Color C=new Color(255,0,O);
int con=3;
Button b_red,b_blue,b_green,b_clear,b_quit;
public void init()
{
addMouseMotionListener(this);
b_red=new Button("画红色图形");
b_blue=new Button("画蓝色图形");
b_green=new Button("画绿色图形");
b_quit=new Button("橡皮");
b_clear=new Button("清除");
add(b_red);
add(b_green);
add(b_blue);
add(b_quit);
add(b_clear);
b_red.addActionListener(this);
b_green.addActionListener(this);
b_blue.addActionListener(this);
b_quit.addActionListener(this);
b_dear.addActionListener(this);
}
public void paint()
(if(x!=-l&&y!=-l&rubberNote= =
0R&clearNote= =0)
{g.setColor(c);
g.filloval(X,Y,con,con);
}
else if(rubberNote= =1&&clearNote= =O)
{g.clearRect(x,Y,10,10);
}
else if(clearNote= =1&&rubberNote= =O)
{g.clearRect(0,0,getSize().width,getSize().
height);
}
}
public void mouseDragged(MouseEvent e)
{x=(int)e.getX();y=(int)e.getY();repaint();
}
public void mouseMoved(MouseEvent e){)
public void update(Graphics g)
{paint(g);
}
public void actionPerformed(Event e)
{if(e.getSource()= =b-red)
{rubberNote=0;clearNote=0;c=new Color
(255,0,0);
}
else if(e.getSource()= =b_green)
{rubberNore=0;clearNote=0;C=new Color(0,
255,0);
}
else if(e.getSource()= =b_blue)
{rubberNote=0;clearNote=0;C=new Color(0,
0,255);
}
if(e.getSource()= =b—quit)
{rubberNote=1;clearNote=0;
}
if(e.getSource()= =b—clear)
{clearNote=1;rubberNote=0;repaint();
}
}
}
第10题
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!