View Single Post
  #2 (permalink)  
Old 03-18-2008, 04:48 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Here's how you deal with deprecation warnings:
Code:
C:\jexp>javac firstappletrx.java Note: firstappletrx.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\jexp>javac -Xlint:deprecation firstappletrx.java firstappletrx.java:141: warning: [deprecation] mouseDown(java.awt.Event,int,int) in java.a wt.Component has been deprecated public boolean mouseDown (Event e, int x, int y) ^ 1 warning
Code:
// <applet code="FirstAppletRx" width="400" height="400"></applet> import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class FirstAppletRx extends Applet implements Runnable { //position of the ball int x_pos = 12; int y_pos = 12; // int first_x = 1; // int first_y = 1; //ball size int radius = 5; //direction int x_speed = 3; int y_speed = 2; //applet size int appletsize_top = 10; int appletsize_bottom = 370; int appletsize_left = 10; int appletsize_right = 370; //random movement Random rnd = new Random (); //Drawing image off screen private Image dbImage; private Graphics dbg; int maxspeed = 5; Thread thread; boolean running = false; public void init() { addMouseListener(ma); } // start - method is called after init // and every time the applet is maximized public void start () { // define a new thread running = true; thread = new Thread (this); // start this thread thread.start (); System.out.println("start"); } // A well–behaved Applet stops // its animation when minimized. public void stop() { running = false; if(thread != null) thread.interrupt(); thread = null; System.out.println("stop"); } //run- start the applet game // Implementation of the Runnable interface. public void run () { while (running) { // lower ThreadPriority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // change direction if ball hits any side if (x_pos < appletsize_left || x_pos > appletsize_right) { x_speed *= -1; } if (y_pos < appletsize_top || y_pos > appletsize_bottom) { y_speed *= -1; } x_pos += x_speed; y_pos += y_speed; //repaint applet repaint(); try { // Stop thread for the speed Thread.sleep (50); } catch (InterruptedException ex) { running = false; break; } // set ThreadPriority to maximum value Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } //create an image off screen and loads it into the applet public void update (Graphics g) { // initialize buffer if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } // clear screen in background dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); // draw elements in background dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); } /** * paint - method allows you to paint into your applet. * This method is called e.g. if you move your browser * window or if you call repaint() */ public void paint (Graphics g) { // set color g.setColor (Color.red); // paint a filled colored circle g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); } private MouseListener ma = new MouseAdapter() { // Method to handle mousePressed events public void mousePressed(MouseEvent e) { // Preserve the direction of x_speed [-:left +:right]. //int sign = (int)Math.signum(x_speed); // j2se 1.5+ // or we can try: int sign = (x_speed > 0) ? 1 : -1; x_speed = 1 + rnd.nextInt(maxspeed); // [1 - maxspeed] // Restore the sign. x_speed *= sign; } }; }
Reply With Quote