Hi all
I am new to Java and having trouble with constructors with images and audio. I have tried to use the API library to work it out myself but cannot understand what they are talking about.

My problem is that I am trying to create two instances of the class Plane (redplane and blueplane) Each instance has a different image and audio file attached to it and I am unable to work out how to create a constructor with an image and audio file.

I have viewed many constructors with integers but none with image and audio.
Can anyone help me with the syntax needed in the Plane class and then to and how to call it in the Main class of my applet.

regards
Daryl

How are we supposed to know what the class Plane is? If it's a standard library class, at least link us to the documentation or something.

okay here is the code for the java class Plane and class Main but not sure how many mistakes are in it.

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.*;
import java.lang.*;


public class Plane 
{

    private int pos_x;
    private int pos_y;
    private int x_speed;
    private int y_speed;
    //private int radius;

    private int first_x;
    private int first_y;

    private int maxspeed;

    private int x_leftout = 10;
    private int x_rightout = 370;
    private int y_upout = 45;
    private int y_downout = 370;


    Image decal;
    AudioClip out;
    Player player; 
    //Color color;  
    Random rnd = new Random ();

    public Plane(int x, int y, int vx, int vy, int ms, Graphic decal, AudioClip out, Player player) 
    {
        //this.radius = radius;

        pos_x = x; //
        pos_y = y;//
        first_x = x;
        first_y = y;
        x_speed = vx;//
        y_speed = vy;//
        maxspeed = ms;//

    /* Use 'this' keyword to make explicit that a method
         is accessing its own variables. */

        this.decal = decal;
        this.out = out;
        this.player = player;
    }

        public void move()
        {
            pos_x += x_speed;
            pos_y += y_speed;
            isOut();
        }  

        public void planeWasHit()
        {
            pos_x = first_x;
            pos_y = first_y;

            x_speed = (rnd.nextInt()) %  maxspeed;
        }

        public boolean userHit (int mouse_x, int mouse_y)
        {
            double x = mouse_x - pos_x;
            double y = mouse_y - pos_y;

            double distance = Math.sqrt ((x*x) + (y*y));

            if (distance < 15)
            {
                player.addScore (10*Math.abs(x_speed) + 10);
                return true;
            }
            else return false;
        }

        private boolean isOut()
        {
            if (pos_x < x_leftout)
            {
                pos_x = first_x;
                pos_y = first_y;

                out.play();

                x_speed = (rnd.nextInt ()) % maxspeed;

                player.looseLife();

                return true;
            }
            else if (pos_x > x_rightout)
            {
                pos_x = first_x;
                pos_y = first_y;

                out.play();

                x_speed = (rnd.nextInt ())% maxspeed;

                player.looseLife();

                return true;
            }
            else if (pos_y > y_downout)
            {
                pos_x = first_x;
                pos_y = first_y;

                out.play();

                x_speed = (rnd.nextInt ())% maxspeed;

                player.looseLife();

                return true;
            }
            else return false;
        }

    public void PaintPlane (Graphics g)
    {

//  g.drawImage (redpl, this);

    //  g.drawImage ();
        //(int x, int y, int vx, int vy, int ms, Image decal, AudioClip out, Player player)
    blueplane = new Plane ( 10, 190, 150, 1, 1, 3, bluepl, jetlow, player);
    //g.getPlane (g);
//  blueplane = new Plane ( 10, 190, 150, 1, 1, 3, bluepl, jetlow, player);
    }
}

.......and here is the main

import java.awt.*;
import java.util.*;
import java.applet.*;
import java.net.*;


public class Main extends Applet implements Runnable
{
    private int speed;  

    boolean isStopped = true;

    private Player player;
    private Plane blueplane;
    private Plane redplane;


    Thread th;                      

    AudioClip propPlaneSound;
    AudioClip jetlow;
    AudioClip miss;

    Font f = new Font ("Serif", Font.BOLD, 20);

    Cursor c;

    Image backImage;
    Image redpl;
    Image bluepl;


    private Image dbImage;
    private Graphics dbg;

    public void init ()
    {

        c = new Cursor (Cursor.CROSSHAIR_CURSOR);
        this.setCursor (c);

        Color superblue = new Color (0, 0, 255);


        backImage = getImage (getCodeBase(), "greenfield.jpg");
        decal = getImage (getCodeBase(), "redpl.png");
        decal = getImage (getCodeBase(), "bluepl.png");

        setFont (f);

        if (getParameter ("speed") != null)
        {
            speed = Integer.parseInt(getParameter("speed"));
        }
        else speed = 15;

        propPlaneSound = getAudioClip (getCodeBase(), "propPlaneSound.au");
        jetlow = getAudioClip (getCodeBase(), "jetlow.au");
        redplane = getImage (getCodeBase(), "redpl.png");
        blueplane = getImage (getCodeBase(), "bluepl.png");

        player = new Player ();
        redplane = new Plane (  10,   190,  250, 1, -1, 4, decal, propPlaneSound, player);
        blueplane = new Plane ( 10, 190, 150, 1, 1, 3, decal, jetlow, player);
            //               (int x, int y, int vx, int vy, int ms, AudioClip out, Player player) 

    }


    public void start ()
    {

        th = new Thread (this);
        th.start ();


    }


    public void stop ()
    {
        th.stop();
    }

    public boolean mouseDown (Event e, int x, int y)
    {
        if (!isStopped)
        {
            if (redplane.userHit (x, y))
            {
                redplane.planeWasHit ();
            }
            if (blueplane.userHit (x, y))
            {
                blueplane.planeWasHit ();
            }
        else
            {
                //
            }
        }
        else if (isStopped && e.clickCount == 2)
        {
            isStopped = false;
            init ();
        }
        return true;
    }



    public void run ()
    {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        while (true)
        {
            if (player.getLives() >= 0 && ! isStopped)
            {
                redplane.move();
                blueplane.move();
            }
            repaint();

            try
            {
                Thread.sleep (speed);
            }
            catch (InterruptedException ex)
            {
                //do nothing
            }
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }                   


    }


    public void paint (Graphics g)
    {

        if (player.getLives() >= 0)
        {
            g.setColor (Color.yellow);

            g.drawString ("Score: "+ player.getScore(), 10, 40);
            g.drawString ("Lives: "+ player.getLives(), 300, 40);

            redplane.PaintPlane (g);
            blueplane.PaintPlane (g);

            if (isStopped)
            {
                g.setColor (Color.yellow);
                g.drawString ("Doubleclick on Applet to start Game!", 40, 200);
            }
        }
        else if (player.getLives () < 0)
        {
            g.setColor (Color.yellow);
            g.drawString ("Game Over!", 130, 100);
            g.drawString ("You scored " + player.getScore() + " Points!", 90, 140);

            g.drawString ("Doubleclick to play again!", 20, 220);
            isStopped = true;
        }
        g.drawImage (backImage, 0, 0, this);
    }



    public void update (Graphics g)
    {
        if(dbImage == null)
            {
                dbImage = createImage (this.getSize().width, this.getSize().height);
                dbg = dbImage.getGraphics();
            }

        g.drawImage (backImage, 0, 0, this);
        g.drawImage (redpl, 0, 0, this);
        g.drawImage (bluepl,50 ,0 , this); 
    //  g.drawImage (bluepl, pos_x, pos_y, this);

    }
}

cheers
daffer

Edited 3 Years Ago by Dani: Formatting fixed

Well, I don't know what's wrong: you have created a constructor for Plane that takes an AudioClip object and a Graphics object and you have passed your objects correctly. Looks ok to me.

What's wrong?

(Can you edit your post and put code tags? Reading the code will be better...)

Hi
Thanks for the reply, yeah not sure about putting in tags cause this code is from the Sun tutorials, and the orig tags are in German...I am just trying to adapt it to learn what it does.
When designing the Constructor for the Plane object do you write 'Image' or 'Graphic'?
If I could find a working code that has a constructor using Images and audio I might be able to put the pieces together. Thanks again anway!

Yeah it is in brackets..see the first line!

Back to the problem,
When designing the Constructor for the Plane object do you write 'Image' or 'Graphic'?
If I could find a working code that has a constructor using Images and audio I might be able to put the pieces together.

You already have a constructor using Image and AudioClip, so what problems are you having with that?

And you add code tags like this: [code] ... your code here ... [/code].

oh I thought it meant in brackets..
The problem is that when I try to compile the project the errors in the Main class are "cannot find symbol constructor Plane (int,int,int,int,int,int,java.awt.Image, java.applet.AudioClip, Player) for the following code

redplane = new Plane (  10,   190,  250, 1, -1, 4, decal, propPlaneSound, player);
blueplane = new Plane ( 10, 190, 150, 1, 1, 3, decal, jetlow, player);

The problem must be somewhere else.
Does anyone know of a sample code that uses a constructor with an Image and Audio that I could look at?
regards
daffer

The error is simple, it cannot find the cinstructor you are using:
"cannot find symbol constructor Plane (int,int,int,int,int,int,java.awt.Image, java.applet.AudioClip, Player)"

Meaning that the constructor you are using doesn't exist. I haven't looked at your code, but the arguments you enter at the constructor must much the ones that are declared at the class definition.
It is like having a constructor like this:

public ConA(String a, int b) {

}

And try to call it like this:

ConA cA = new ConA(1, 2, 3, "aaaa");

That constructor doesn't exist because the constructor takes as argumnents a String and an int.

So what arguments does the Plane constructor takes and use those.

This article has been dead for over six months. Start a new discussion instead.