Results 1 to 2 of 2
  1. #1
    Lightcraft is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Little problem I'm running with my applet

    Hello everyone. I am relatively new to Java and trying to get more experience. I've been recently handed an assignment that requires drawing 3 balls that bounce off each other and off the walls. It is also required that I created a button to change the direction of the red ball in a 45 degree clockwise angle. I have been able to accomplish this for the most part but it seems my coding for bouncing off each other messes up as I click the button to change the direction of the red ball. I am trying to think of why this is happning (if the actual incrementing I am doing for the balls is different then what the animation is showing me) and currently I am stumped. Any help to why or hints this is happening would be helpful.

    I also know some of the methods I use are deprecated but the prof insisted on just using those methods for now so I had no choice. Also the balls only bounce off in a mirror reflection like fashion so there was no need to use vectors.

    Java Code:
    import java.lang.Math;
    import java.awt.*;
    import java.applet.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    import java.applet.AudioClip;
    
    class Animation extends Thread{
      private JApplet app;
      private Color color;
      private int which;
      private int posit1x = 0;
      private int posit2x = 20;
      private int posit3x = 130;
      private int posit1y = 0;
      private int posit2y = 50;
      private int posit3y = 150;
      private int pause;
      private AudioClip a1,a2;
      private int in1x = 1;
      private int in1y = 1;
      private int in2x = 1;
      private int in2y = 1;
      private int in3x = 1;
      private int in3y = 1;
      private double c1,c2,c3;
      Animation(JApplet a, Color c, int which, int p) {
        color = c;
        app = a;
        pause = p;
        this.which = which;
      }
      public void run() {
        while (true) {
          posit1x += in1x;
          posit2x += in2x;
          posit3x += in3x;
          posit1y += in1y;
          posit2y += in2y;
          posit3y += in3y;
          try {
            Thread.sleep(pause);
          } catch (InterruptedException e) {}
          //Calculate distance between each ball to see if they need to bounce off each other
          c1 = (Math.pow((posit1x-posit2x),2) + Math.pow((posit1y-posit2y),2));
          c2 = (Math.pow((posit1x-posit3x),2) + Math.pow((posit1y-posit3y),2));
          c3 = (Math.pow((posit2x-posit3x),2) + Math.pow((posit2y-posit3y),2));
          a1 = app.getAudioClip(app.getDocumentBase(), "Audio1.au");
          a2 = app.getAudioClip(app.getDocumentBase(), "Audio2.au");
          //Coding to allow the balls to bounce off the walls 
          if (posit1x > app.size().width - 20|| posit1x < 0) {
            in1x = -in1x;
            a1.play();
          }if(posit1y > app.size().height - 45|| posit1y < 0){
            in1y = -in1y;
            a1.play();
          }if (posit2x > app.size().width -20|| posit2x < 0) {
            in2x = -in2x;
            a1.play();
          }if(posit2y > app.size().height - 45|| posit2y < 0){
            in2y = -in2y;
            a1.play();
          }if (posit3x > app.size().width - 20|| posit3x < 0) {
            in3x = -in3x;
            a1.play();
          }if(posit3y > app.size().height -45|| posit3y < 0){
            in3y = -in3y;
            a1.play();
            //Coding to allow balls to bounce off each other
          }if(Math.sqrt(c1) < 20){
            in1x = -in1x;
            in2x = -in2x;
            in1y = -in1y;
            in2y = -in2y;
            a2.play();
          }if(Math.sqrt(c2) < 20){
            in1x = -in1x;
            in3x = -in3x;
            in1y = -in1y;
            in3y = -in3y;
            a2.play();
          }if(Math.sqrt(c3) < 20){
            in2x = -in2x;
            in3x = -in3x;
            in2y = -in2y;
            in3y = -in3y;
            a2.play();
          }
         app.repaint();
          
        }
      }
      public void draw(Graphics g) {
        g.setColor(color);
        if (which == 1)
          g.fillOval(posit1x, posit1y, 20, 20); 
        if (which == 2)
          g.fillOval(posit2x, posit2y, 20, 20);
        if (which == 3)
          g.fillOval(posit3x , posit3y, 20, 20);
      }
      //X increment set method
      public void setx(int a){
        in1x = a;
      }
      //Y increment set method
      public void sety(int b){
        in1y = b;
      }
      //X get method
      public int getx(){
        return in1x;
      }
      //Y get method
      public int gety(){
        return in1y;
      }
      
    }
    class Animate extends JPanel {
      private JApplet a;
      private Animation t, t1, t2; 
      private Image img;          
      private Graphics gcopy;   
      private Double c1, c2; 
      private Color color;
      
      public void update(Graphics g) {
        paint(g);
      }
      public void paint(Graphics g) {
        //use method in A3 class to create references to each animation object
        t = A3.getAnimation("t");
        t1 = A3.getAnimation("t1");
        t2 = A3.getAnimation("t2");
        img = createImage(getWidth(), getHeight());
        gcopy = img.getGraphics();
        gcopy.setColor(Color.YELLOW);
        gcopy.fillRect(0, 0, getWidth(), getHeight());
        //Create double buffer to smooth out animation
        //and load each ball object into JPanel
        t.draw(gcopy);         
        t1.draw(gcopy);  
        t2.draw(gcopy);  
        g.drawImage(img, 0, 0, this);  
      }
    }
    public class A3 extends JApplet implements ActionListener{
      private Animate s;
      private Container c;
      private JButton b1;
      private JPanel j2;
      private static Animation t,t1,t2;
      public void init(){
        j2 = new JPanel();
        c = this.getContentPane();
        //Create a new Animate class to put into applet container
        s = new Animate();
        b1 = new JButton("TURN RED BALL 45 DEGREES");
        b1.addActionListener(this);
        j2.add(b1);
        c.setLayout(new BorderLayout());
        c.add(s, BorderLayout.CENTER);
        c.add(b1, BorderLayout.SOUTH);
      }
      //method to allow referencing to each ball object created in this class to JPanel for drawing
      public static Animation getAnimation(String b){
        if(b.equals("t")){
          return t;
        }if(b.equals("t1")){
          return t1;
        }if(b.equals("t2")){
          return t2;
        }
        return null;
      }
      public void start() {
        if (t == null) {
          t = new Animation(this, Color.RED,1,15);
          t.start();
        }if (t1 == null) {
          t1 = new Animation(this, Color.BLUE,2,15);
          t1.start();
        }if (t2 == null) {
          t2 = new Animation(this, Color.GREEN,3,15);
          t2.start();
        } 
      }
      public void stop() {
        if (t != null) {
          t.stop();
          t = null;
        } if (t1 != null) {
          t1.stop();
          t1 = null;
        }if (t2 != null) {
          t2.stop();
          t2 = null;
        }
      }
      
      public void actionPerformed(ActionEvent e){
        if(e.getSource()==b1){
          //Coding to program red ball behaviour each time the button is clicked based on direction the red ball is moving
          if(t.getx() == 1 && t.gety() == 1){
            t.setx(0);
          }else if(t.getx()== 0 && t.gety() == 1){
            t.setx(-1);
          }else if(t.getx()== -1 && t.gety() == 1){
            t.sety(0);
          }else if(t.getx() == -1 && t.gety() == 0){
            t.sety(-1);
          }else if(t.getx() == -1 && t.gety() == -1){
            t.setx(0);
          }else if(t.getx() == 0 && t.gety() == -1){
            t.setx(1);
          }else if(t.getx() == 1 && t.gety() == -1){
            t.sety(0);
          }else if(t.getx() == 1 && t.gety() == 0){
            t.sety(1);
          }
        }
      }
    }
    Last edited by Lightcraft; 04-08-2010 at 10:01 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Well, I don't know the answer to your question, but I'm going to suggest you learn some Java basics first. For example:

    a) use meaningfull variable names. What the heck are "a, t, t1, t2, c1, c2"? They mean nothing to me so I didn't even bother to try and follow the logic i your code.

    b) Your painting code is wrong. You should never override update() and paint(), That is an old trick for when you a using AWT. For custom painting on a Swing component you should be overriding the paintComponent() method. Read the section from the Swing tutorial on "Custom Painting" for more information.

Similar Threads

  1. running an applet
    By ksatty in forum Java Applets
    Replies: 4
    Last Post: 01-08-2010, 10:12 AM
  2. Java Applet Not Running
    By kahaj in forum Java Applets
    Replies: 4
    Last Post: 10-08-2009, 12:11 AM
  3. Replies: 0
    Last Post: 08-26-2009, 01:03 PM
  4. First Applet not running on browsers
    By Centinela66 in forum Java Applets
    Replies: 11
    Last Post: 10-09-2008, 01:58 PM
  5. Getting url of page applet is running on
    By damounh in forum Java Applets
    Replies: 1
    Last Post: 05-09-2008, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •