import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.applet.Applet; public class Liikumine3 extends Applet implements ActionListener, MouseListener{ double x=150; double y=80; double d=15; double v=2.5; static final long serialVersionUID=1; BufferedImage puhver=new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB); Graphics2D puhverg=(Graphics2D)puhver.getGraphics(); Button nupp1=new Button("Kiiremaks"); Button nupp2=new Button("Aeglasemaks"); Timer t=new Timer(100, this); public Liikumine3(){ add(nupp1); add(nupp2); nupp1.addActionListener(this); nupp2.addActionListener(this); addMouseListener(this); t.start(); } public void paint(Graphics g){ super.paint(g); puhverg.setColor(Color.yellow); puhverg.fillRect(0, 0, 300, 300); puhverg.setColor(Color.black); puhverg.drawRect((int)x, (int)y, (int)d, (int)d); g.drawImage(puhver, 0, 0, this); } public void update(Graphics g){paint(g);} public void liigu(){ x=x+v; if(x+d>getWidth()){v=-Math.abs(v);} if(x<1){v=Math.abs(v);} repaint(); } public void actionPerformed(ActionEvent e){ if(e.getSource()==t){ liigu(); } if(e.getSource()==nupp1){ v=v*1.3; } if(e.getSource()==nupp2){ v=v*0.8; } } //luba hiirega määrata ruudu kohta public void mousePressed(MouseEvent e){ x=e.getX(); y=e.getY(); repaint(); } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public static void main(String[] arg){ Frame f=new Frame("Liikumine"); f.add(new Liikumine3()); f.setSize(300, 300); f.setVisible(true); } }