package gametemplate; import java.awt.Color; import java.awt.*; import java.awt.*; import java.awt.image.*; import javax.imageio.ImageIO; import java.io.*; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; public class TryGame extends Game { private int i; private Image image; private char c=' '; private boolean keypressed=false; private int ballX, ballY; private int ball1X, ball1Y; private String output=""; public void stop() { System.out.println("stopping..."); } public void init(){ resize(400,400); i=0; ballX=100; ballY=100; setSleepTime(60); setCursor(readBufferedImage("cursor1.png"), new Point(16,16), "cursor1"); image=readBufferedImage("image.jpg"); } public Color getBackgroundColor() { return new Color(i%255,0,0); } public void mousePressed(MouseEvent e){ ballX=e.getX(); ballY=e.getY(); } public void mouseReleased(MouseEvent e){ ball1X=e.getX(); ball1Y=e.getY(); } public void mouseClicked(MouseEvent e){ output="Mouse clicked "+ e.getClickCount()+" times at the same place."; } public void mouseExited(MouseEvent e){ output="Mouse left the panel at: ("+e.getX()+","+e.getY()+")."; } public void mouseEntered(MouseEvent e){ output="Mouse entered the panel at: ("+e.getX()+","+e.getY()+")."; } public void keyTyped(KeyEvent e){ if(e.getKeyChar()!=KeyEvent.CHAR_UNDEFINED) c=e.getKeyChar(); } public void keyPressed(KeyEvent e){ keypressed=true; } public void keyReleased(KeyEvent e){ keypressed=false; } public void paint(Graphics g) { i+=2; Point p=gp.getMousePosition(); if(p!=null){ ballX=(int)(0.95*ballX+0.05*p.getX()); ballY=(int)(0.95*ballY+0.05*p.getY()); } g.drawImage(image, 80, 80, applet); g.setColor(Color.white); g.fillOval(ballX-10,ballY-10,20,20); g.setColor(Color.yellow); g.fillOval(ball1X-15,ball1Y-15,30,30); g.setFont(new Font("Serif",Font.BOLD,100)); g.drawString(c+"",300,300); g.setColor(Color.green); if(keypressed){ g.fillOval(50,250,100,20); } g.setColor(Color.cyan); g.setFont(new Font("Serif",Font.BOLD,15)); g.drawString(output,10,350); } }