Results 1 to 7 of 7
  1. #1
    eliCanzee is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default Main method here to :S:S hlp pls

    Hi .. I need main method for this can someone help me .. Just main pls ???

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

    public class Shoot extends Applet implements Runnable
    {
    // Variablen
    double x_float = 400; // Startposition
    double y_float = 400; // Startposition
    double vx; // x - Geschwindigkeit
    double vy; // y - Geschwindigkeit
    double angle = 80; // Schusswinkel
    double power = 100; // Geschwindigkeit , power
    final double gravity = 0.3; // Schwerkraftskoeffizient

    double pi = 2 * Math.PI; // 2 * pi konstante

    int radius = 3; // Radius des Balles

    // Werte für die Kannone
    int i = 400 + (int)(20.0 * Math.cos(((double)angle * pi) / 360.0));
    int j = 400 - (int)(20.0 * Math.sin(((double)angle * pi) / 360.0));

    boolean addjusting = false; // Wichtig für Doppelpufferung oder nicht

    Random rnd = new Random();

    // Neue Windgeschwindigkeit (random)
    double wind = rnd.nextFloat() * 4.0 - 2.0;

    public void init()
    {
    setBackground (Color.black);
    }

    public void start ()
    {
    // Schaffen eines neuen Threads, in dem das Spiel läuft
    Thread th = new Thread (this);
    // Starten des Threads
    th.start ();
    }

    public void stop()
    {

    }

    public void destroy()
    {

    }

    // Variablen für die Doppelpufferung
    private Image dbImage;
    private Graphics dbg;

    /** Update - Methode, Realisierung der Doppelpufferung zur Reduzierung des Bildschirmflackerns */
    public void update (Graphics g)
    {
    if (addjusting)
    {
    // Initialisierung des DoubleBuffers
    if (dbImage == null)
    {
    dbImage = createImage (this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics ();
    }

    // Bildschirm im Hintergrund löschen
    dbg.setColor (getBackground ());
    dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

    // Auf gelöschten Hintergrund Vordergrund zeichnen
    dbg.setColor (getForeground());
    paint (dbg);

    // Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm anzeigen
    g.drawImage (dbImage, 0, 0, this);
    }
    else
    {
    paint(g);
    }
    }


    public boolean keyDown (Event e, int key)
    {
    // linke Cursortaste
    if (key == Event.LEFT)
    {
    angle++;
    i = 400 + (int)(20.0 * Math.cos(((double)angle * pi) / 360.0));
    j = 400 - (int)(20.0 * Math.sin(((double)angle * pi) / 360.0));
    addjusting = true;
    }
    else if (key == Event.DOWN)
    {
    power--;
    addjusting = true;
    }
    else if (key == Event.UP)
    {
    power++;
    addjusting = true;
    }
    // rechte Cursortaste
    else if (key == Event.RIGHT)
    {
    angle--;
    i = 400 + (int)(20.0 * Math.cos(((double)angle * pi) / 360.0));
    j = 400 - (int)(20.0 * Math.sin(((double)angle * pi) / 360.0));
    addjusting = true;
    }
    // SpaceBar hat Wert 32
    else if (key == 32)
    {
    // Zurücksetzen der Startposition
    x_float = i;
    y_float = j;

    // Berechnen der Startgeschwindigkeiten
    vx = (power / 10.0) * (float)Math.cos((pi * (double)angle) / 360.0);
    vy = -((power / 10.0) * (float)Math.sin((pi * (double)angle) / 360.0));

    addjusting = false;
    }

    return true;
    }


    public void run ()
    {
    // Erniedrigen der ThreadPriority um zeichnen zu erleichtern
    Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);

    // Solange true ist läuft der Thread weiter
    while (true)
    {
    // Ändern der x und y Position der Kugel
    x_float += vx;
    y_float += vy;

    // Ändern der vy (Schwerkraft wirkt auf die Kugel)
    vy += 0.3;

    // Berechnen des Windeinflusses
    double w = 1.9 * wind - vx;

    if (wind >=0)
    {
    vx+= (w * w) / 512.0;
    }
    else
    {
    vx -= (w * w) / 512.0;
    }

    // Neuzeichnen des Applets
    repaint();

    try
    {
    // Stoppen des Threads für in Klammern angegebene Millisekunden
    Thread.sleep (20);
    }
    catch (InterruptedException ex)
    {
    // do nothing
    }

    // Zurücksetzen der ThreadPriority auf Maximalwert
    Thread.currentThread().setPriority(Thread.MAX_PRIO RITY);
    }
    }


    public void paint (Graphics g)
    {
    g.setColor (Color.white);

    g.drawString ("power " + power, 100, 20);
    g.drawString ("angle " + angle, 100, 40);
    g.drawString ("wind " + wind, 100, 60);

    g.fillOval ((int)x_float - radius, (int)y_float - radius, 2 * radius, 2 * radius);
    g.drawLine(399, 400, i - 1, j);
    g.drawLine(401, 400, i + 1, j);
    g.drawLine(400, 399, i, j - 1);
    g.drawLine(400, 401, i, j + 1);
    g.fillOval (390, 390, 20, 20);

    }

    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Applet can't be invoked like normal java program by defining main method.

    Just go thru this link:
    Invoking Applets from HTML
    Ramya:cool:

  3. #3
    eliCanzee is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    And what can I do to make this class worrk because I need this for my project ??? how to run this at compiler ??? without method main ??

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Hi,
    Just check ur system is having appletviewer.

    After compiling call
    appletviewer Shoot.java in command line

    Code change

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.util.*;
    [B][COLOR="SeaGreen"]/*
    <applet code="Shoot" class ="Shoot.class" width=50 height=50>
     <param name=Color value="blue">
     </applet>
     */[/COLOR][/B]public class Shoot extends Applet implements Runnable
    {
    // Variablen
    double x_float = 400; // Startposition
    double y_float = 400; // Startposition
    double vx; // x - Geschwindigkeit
    double vy; // y - Geschwindigkeit
    double angle = 80; // Schusswinkel
    double power = 100; // Geschwindigkeit , power
    final double gravity = 0.3; // Schwerkraftskoeffizient
    
    double pi = 2 * Math.PI; // 2 * pi konstante
    
    int radius = 3; // Radius des Balles
    
    // Werte für die Kannone
    int i = 400 + (int)(20.0 * Math.cos(((double)angle * pi) / 360.0));
    int j = 400 - (int)(20.0 * Math.sin(((double)angle * pi) / 360.0));
    
    boolean addjusting = false; // Wichtig für Doppelpufferung oder nicht
    
    Random rnd = new Random();
    
    // Neue Windgeschwindigkeit (random)
    double wind = rnd.nextFloat() * 4.0 - 2.0;
    
    public void init()
    {
    setBackground (Color.black);
    }
    
    public void start ()
    {
    // Schaffen eines neuen Threads, in dem das Spiel läuft
    Thread th = new Thread (this);
    // Starten des Threads
    th.start ();
    }
    
    public void stop()
    {
    
    }
    
    public void destroy()
    {
    
    }
    
    // Variablen für die Doppelpufferung
    private Image dbImage;
    private Graphics dbg;
    
    /** Update - Methode, Realisierung der Doppelpufferung zur Reduzierung des Bildschirmflackerns */
    public void update (Graphics g)
    {
    if (addjusting)
    {
    // Initialisierung des DoubleBuffers
    if (dbImage == null)
    {
    dbImage = createImage (this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics ();
    }
    
    // Bildschirm im Hintergrund löschen
    dbg.setColor (getBackground ());
    dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
    // Auf gelöschten Hintergrund Vordergrund zeichnen
    dbg.setColor (getForeground());
    paint (dbg);
    
    // Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm anzeigen
    g.drawImage (dbImage, 0, 0, this);
    }
    else
    {
    paint(g);
    }
    }
    
    
    public boolean keyDown (Event e, int key)
    {
    // linke Cursortaste
    if (key == Event.LEFT)
    {
    angle++;
    i = 400 + (int)(20.0 * Math.cos(((double)angle * pi) / 360.0));
    j = 400 - (int)(20.0 * Math.sin(((double)angle * pi) / 360.0));
    addjusting = true;
    }
    else if (key == Event.DOWN)
    {
    power--;
    addjusting = true;
    }
    else if (key == Event.UP)
    {
    power++;
    addjusting = true;
    }
    // rechte Cursortaste
    else if (key == Event.RIGHT)
    {
    angle--;
    i = 400 + (int)(20.0 * Math.cos(((double)angle * pi) / 360.0));
    j = 400 - (int)(20.0 * Math.sin(((double)angle * pi) / 360.0));
    addjusting = true;
    }
    // SpaceBar hat Wert 32
    else if (key == 32)
    {
    // Zurücksetzen der Startposition
    x_float = i;
    y_float = j;
    
    // Berechnen der Startgeschwindigkeiten
    vx = (power / 10.0) * (float)Math.cos((pi * (double)angle) / 360.0);
    vy = -((power / 10.0) * (float)Math.sin((pi * (double)angle) / 360.0));
    
    addjusting = false;
    }
    
    return true;
    }
    
    
    public void run ()
    {
    // Erniedrigen der ThreadPriority um zeichnen zu erleichtern
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
    // Solange true ist läuft der Thread weiter
    while (true)
    {
    // Ändern der x und y Position der Kugel
    x_float += vx;
    y_float += vy;
    
    // Ändern der vy (Schwerkraft wirkt auf die Kugel)
    vy += 0.3;
    
    // Berechnen des Windeinflusses
    double w = 1.9 * wind - vx;
    
    if (wind >=0)
    {
    vx+= (w * w) / 512.0;
    }
    else
    {
    vx -= (w * w) / 512.0;
    }
    
    // Neuzeichnen des Applets
    repaint();
    
    try
    {
    // Stoppen des Threads für in Klammern angegebene Millisekunden
    Thread.sleep (20);
    }
    catch (InterruptedException ex)
    {
    // do nothing
    }
    
    // Zurücksetzen der ThreadPriority auf Maximalwert
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    }
    }
    
    
    public void paint (Graphics g)
    {
    g.setColor (Color.white);
    
    g.drawString ("power " + power, 100, 20);
    g.drawString ("angle " + angle, 100, 40);
    g.drawString ("wind " + wind, 100, 60);
    
    g.fillOval ((int)x_float - radius, (int)y_float - radius, 2 * radius, 2 * radius);
    g.drawLine(399, 400, i - 1, j);
    g.drawLine(401, 400, i + 1, j);
    g.drawLine(400, 399, i, j - 1);
    g.drawLine(400, 401, i, j + 1);
    g.fillOval (390, 390, 20, 20);
    
    }
    
    }
    Ramya:cool:

  5. #5
    eliCanzee is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    it works thnx man :D

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    In general, you can handle this situation by putting the entire page in a JPanel. You can then put the JPanel in an applet, if that is what you want to do. For testing with a main, put the JPanel in a JFrame that you create in the main.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by eliCanzee View Post
    it works thnx man :D
    And also, it's better to added your applet code in separate HTML page. Simply add the applet code within the body tags. And also your browser must Java enable.

Similar Threads

  1. Help me for main method
    By eliCanzee in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2009, 12:43 PM
  2. A query about main method
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:44 AM
  3. main method
    By eva in forum New To Java
    Replies: 5
    Last Post: 12-19-2007, 09:25 AM
  4. The main method in java...
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:21 AM

Posting Permissions

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