340365E 荒生 肇
前回からの変更点は画像をロードする際initの内部で画像をロードするようにしたところです。Appletviewerではちゃんと動作することを確認しました。HTMLファイルを作成し、ブラウザから手元のファイルを開いてappletを実行すると正しく動作しないことがありましたが、ホームページにあげたところ正常動作が確認されたので特に対処はしていません。
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; class Moon{ public int xm,ym; public Moon(int x1,int x2){ xm=x1; ym=x2; } } class Canvas1209 extends Canvas implements KeyListener, MouseListener, MouseMotionListener{ Vector moonArray; int n=0; Image image; public Canvas1209(){ super(); moonArray=new Vector(); moonArray.addElement(new Moon(60,60)); setSize(400,450); addKeyListener(this); addMouseListener(this); addMouseMotionListener(this); } Image offScreenImage; Graphics offScreenGraphics; public void update(Graphics g){ if(offScreenImage==null){ offScreenImage=createImage(400,400); offScreenGraphics=offScreenImage.getGraphics(); } paint(offScreenGraphics); g.drawImage(offScreenImage,0,0,this); } public void paint(Graphics g){ g.setColor(Color.black); g.fillRect(0,0,400,175); Moon m=(Moon)moonArray.elementAt(n); g.setColor(Color.yellow); g.fillOval(m.xm,m.ym,50,50); g.setColor(Color.black); g.fillOval(m.xm,m.ym,40,40); if(m.ym>175){ g.drawImage(image,0,0,this); } g.setColor(Color.lightGray); g.fillRect(0,175,400,225); g.setColor(Color.white); g.fillOval(225,120,100,100); g.fillOval(215,210,120,120); g.setColor(Color.blue); g.fillOval(270,230,10,10); g.fillOval(270,250,10,10); g.fillOval(270,270,10,10); g.setColor(Color.black); g.fillRect(245,140,20,4); g.fillRect(285,140,20,4); g.fillOval(250,155,10,10); g.fillOval(290,155,10,10); g.fillRect(273,165,4,20); g.fillRect(260,200,30,4); } public void undoLine(){ n--; if(n<0){ n=0; } repaint(); } public void redoLine(){ n++; if(n>=moonArray.size()){ n--; } repaint(); } public void clearLine(){ moonArray.clear(); moonArray.addElement(new Moon(60,60)); n=0; repaint(); } public void keyPressed(KeyEvent e){ int key=e.getKeyChar(); System.out.println("keyPressed("+e+","+key+")"); if(key=='u'){ undoLine(); } if(key=='r'){ redoLine(); } if(key=='c'){ clearLine(); } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){ int xg=e.getX(); int yg=e.getY(); System.out.println("mouseClicked("+e+","+xg+","+yg+")"); for(int i=n+1;i<=moonArray.size()-1;){ moonArray.remove(i); } moonArray.addElement(new Moon(xg,yg)); n++; repaint(); } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseDragged(MouseEvent e){} public void mouseMoved(MouseEvent e){} } public class Kadai1209 extends Applet implements ActionListener{ Canvas1209 canvas1209; Button undoButton,redoButton,clearButton; public void init(){ Panel panel=new Panel(); panel.setLayout(new FlowLayout()); panel.add(undoButton=new Button("Undo(u)")); undoButton.addActionListener(this); panel.add(redoButton=new Button("Redo(r)")); redoButton.addActionListener(this); panel.add(clearButton=new Button("Clear(c)")); clearButton.addActionListener(this); setLayout(new BorderLayout()); add(panel,"South"); add(canvas1209=new Canvas1209(),"North"); undoButton.addKeyListener(canvas1209); redoButton.addKeyListener(canvas1209); clearButton.addKeyListener(canvas1209); setSize(getPreferredSize()); setVisible(true); canvas1209.image=getImage(getCodeBase(),"sun.png"); } public void actionPerformed(ActionEvent e){ System.out.println(e); Object source=e.getSource(); if(source.equals(undoButton)){ canvas1209.undoLine(); } else if(source.equals(redoButton)){ canvas1209.redoLine(); } else if(source.equals(clearButton)){ canvas1209.clearLine(); } } }