マウスでクリックすると画像がチェンジしていく方法 |
- プログラムソース
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
* <applet code="Clickchange" width=100 height=100>
* <param name="file1" value="1.jpg">
* <param name="file2" value="2.jpg">
* <param name="file3" value="3.jpg">
* <param name="file4" value="4.jpg">
* <param name="file5" value="5.jpg">
* </applet>
*/
public class Clickchange extends Applet implements MouseListener
{
int x=0;
Image image[]=new Image[5]; //Imageオブジェクト配列
Graphics g;
public void init()
{
addMouseListener(this);
image[0]=getImage(getDocumentBase(),getParameter("file1"));
image[1]=getImage(getDocumentBase(),getParameter("file2"));
image[2]=getImage(getDocumentBase(),getParameter("file3"));
image[3]=getImage(getDocumentBase(),getParameter("file4"));
image[4]=getImage(getDocumentBase(),getParameter("file5"));
setBackground(Color.red); //背景色の設定
}
public void paint(Graphics g)
{
g.drawImage(image[x],0,0,this);
}
public void mouseClicked(MouseEvent me)
{
x++;
if(x==5)
x=0;
repaint();
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
public void mousePressed(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
}
- アプレット表示
アプレット領域(画像部分と赤色部分)をクリックすたびに計→算→物→理→学→計→・・・と文字が書かれた画像が表示されていく。
アプレットの表示ができない場合は、Sun Microsystems 社のJavaソフトウェアが必要です。ダウンロードは http://java.com/ja/download/index.jsp でできます。
|
|
|