Welcome to Dream.In.Code
Become a Java Expert!

Join 150,028 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,583 people online right now. Registration is fast and FREE... Join Now!




ok.... I am back

2 Pages V  1 2 >  
Reply to this topicStart new topic

ok.... I am back, synchronization

RockBander
3 Jun, 2008 - 09:43 PM
Post #1

New D.I.C Head
*

Joined: 12 May, 2008
Posts: 36


My Contributions
ok i didn't want to ask more questions on my project, i know that one of the members, Pbl, along with other help members w/ very very helpful insights... but i'm gonna ask my question anyways since my grade depends on it.

so here it goes...

CODE
package hero;


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JApplet;
import java.awt.event.KeyEvent;



public class Main extends Applet implements KeyListener, Runnable{
    private boolean keyCtrl,keyLeft,keyDown,keyRight;
    
    
    //end of pre-c 1


    
    AudioClip officethemeus;

    public void init() {
    
    keyCtrl=false;
    keyLeft=false;keyDown=false;keyRight=false;
    
    setSize(700,500);
    setBackground(Color.darkGray);
    addKeyListener(this);
    setFocusable(true);

    }

    public void start()
    {
        Thread th = new Thread (this);
        th.start ();
    }
    public void stop() {  }
    public void destroy() { }
    public void run ()
    {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        
        while (true)
        {
            
            
                GreenYpos+=8;
                
                
                
            
            
        
        
        repaint();
            try
            {
            Thread.sleep (20);
            }
        catch (InterruptedException ex){}
        //Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }
    public void paint( Graphics g )
    {
        //scoring
        g.setColor(Color.yellow);
        g.drawString ("Y0U R0CK!!!", 10, 40);

        
        g.setColor(Color.black);
        Polygon fret=new Polygon();
                    fret.addPoint(200,0);fret.addPoint(500,0);fret.addPoint(650, 500);fret.addPoint(50, 500);
                    g.drawPolygon(fret);
                    g.fillPolygon(fret);
                    
                                g.setColor(Color.gray);
                                g.fillOval(106,410,100,50);
        if (keyRight == false)
        {g.setColor( Color.green);}
        else
         {g.setColor(Color.pink);}
            g.fillOval( 100, 400, 100, 50 );
        //x-int,y-int, width, height
                                g.setColor(Color.gray);
                                g.fillOval(236,410,100,50);
        if (keyDown == false)
        {g.setColor( Color.red );}
        else
         {g.setColor(Color.pink);}
        g.fillOval( 230, 400, 100, 50);
                                g.setColor(Color.gray);
                                g.fillOval(366,410,100,50);
        
            if (keyLeft == false)      
                 {g.setColor(Color.yellow);}
                 else
                 {g.setColor(Color.pink);}
                g.fillOval( 360, 400, 100, 50);
                                g.setColor(Color.gray);
                                g.fillOval(496,410,100,50);
            if (keyCtrl == false)      
                  {g.setColor(Color.cyan);}
                   else
                  {g.setColor(Color.pink);}
        g.fillOval( 490, 400, 100, 50);
        //Moving notes!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        
        g.setColor  (Color.green);
        g.fillOval (106,GreenYpos, 90, 40);//xpos=X-position-radius, Ypos=Y-[psotopm-radius,
        
        
        
        
        
        
        
    }
    public void keyPressed(KeyEvent e)
    {
        int ctrl=KeyEvent.CTRL_DOWN_MASK;
        
        if (e.getModifiersEx() == ctrl)
        {
            if (keyCtrl == false)
            {
            keyCtrl = true;
            }
            else
            {
              keyCtrl = false;
            }
            repaint();
        
        
    }
    public void keyReleased(KeyEvent e)
    {
         int ctrl = KeyEvent.CTRL_DOWN_MASK;
        
            if (e.getKeyCode()==17) //17 = CTRL
            {
            keyCtrl = false;repaint();
            }
          
         repaint();
            }
      
            
    }
    public void keyTyped(KeyEvent e)
    {}

}


is my main class, with a scoring class


CODE

package hero;

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

public class GreenNotes
{
private int pos_x;
private int pos_y;                 
    private int y_speed;            
    private int radius;                
    private int first_x;private int first_y;            
    private int maxspeed;            


    
    
    private final int y_downout = 500;

    
    Color color;


    AudioClip out;

    GHPlayer player;

    public GreenNotes (int radius, int x, int y,  int vy,  Color color, AudioClip out, GHPlayer player)
    {
        
        this.radius = radius;//20

        pos_x = x;
        pos_y = y
        first_y = y;

        
        y_speed = vy;//8

        

        this.color = color;

        this.out = out;

        this.player = player;

    }


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


    public void NoteWasHit ()
    {pos_y = first_y;}

    
    public boolean userHit (int maus_y)
    {
        
        double y = maus_y - pos_y;

        double distance = y;

        if (distance < 40)
        {
            player.addScore (100);
            return true;
        }
        else
        {
            player.addScore (-50);
            return false;    
        }
            
    }

    

    public void DrawBall (Graphics g)
    {
        g.setColor  (color);
        g.fillOval (106,pos_y, 90, 40);//xpos=X-position-radius, Ypos=Y-[psotopm-radius,
    }

}

but I have no idea how to make them "sync" to match each other when I press the buttons...


thanks a bunch!!!!!!



User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Ok.... I Am Back
3 Jun, 2008 - 09:44 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,153



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Ok...? Maybe I am missing the point, but what's your question?
User is online!Profile CardPM
+Quote Post

RockBander
RE: Ok.... I Am Back
3 Jun, 2008 - 10:46 PM
Post #3

New D.I.C Head
*

Joined: 12 May, 2008
Posts: 36


My Contributions
QUOTE(no2pencil @ 3 Jun, 2008 - 10:44 PM) *

Ok...? Maybe I am missing the point, but what's your question?

hmmmm i swear i typed my quesion....
anyways my question is

how to I "sync" the moving notes so that the scoring system works properly?


thanks
User is offlineProfile CardPM
+Quote Post

mensahero
RE: Ok.... I Am Back
4 Jun, 2008 - 07:39 AM
Post #4

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(RockBander @ 3 Jun, 2008 - 11:46 PM) *

QUOTE(no2pencil @ 3 Jun, 2008 - 10:44 PM) *

Ok...? Maybe I am missing the point, but what's your question?

hmmmm i swear i typed my quesion....
anyways my question is

how to I "sync" the moving notes so that the scoring system works properly?


thanks


found your missing question.. its inside your code bracket..

QUOTE

but I have no idea how to make them "sync" to match each other when I press the buttons...

thanks a bunch!!!!!!


Your problem is not quite clear. Could you give a more detailed information of what you are trying to achieve.

1. Are you referring to class synchronization?
2. Is that a game? like bust-a-move or dance revo or something..
(i.e. when the note moved in the exact position as of the button and the user pressed the button, points will be added to the players score)


User is offlineProfile CardPM
+Quote Post

RockBander
RE: Ok.... I Am Back
4 Jun, 2008 - 02:07 PM
Post #5

New D.I.C Head
*

Joined: 12 May, 2008
Posts: 36


My Contributions
QUOTE

Your problem is not quite clear. Could you give a more detailed information of what you are trying to achieve.

1. Are you referring to class synchronization?
2. Is that a game? like bust-a-move or dance revo or something..
(i.e. when the note moved in the exact position as of the button and the user pressed the button, points will be added to the players score)


I'm trying to do "2. Is that a game? like bust-a-move or dance revo or something..
(i.e. when the note moved in the exact position as of the button and the user pressed the button, points will be added to the players score)"

but stuck...
User is offlineProfile CardPM
+Quote Post

pbl
RE: Ok.... I Am Back
4 Jun, 2008 - 02:48 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,586



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(RockBander @ 4 Jun, 2008 - 03:07 PM) *

QUOTE

Your problem is not quite clear. Could you give a more detailed information of what you are trying to achieve.

1. Are you referring to class synchronization?
2. Is that a game? like bust-a-move or dance revo or something..
(i.e. when the note moved in the exact position as of the button and the user pressed the button, points will be added to the players score)


I'm trying to do "2. Is that a game? like bust-a-move or dance revo or something..
(i.e. when the note moved in the exact position as of the button and the user pressed the button, points will be added to the players score)"

but stuck...


RockBander
still not really understand your problem.... but with the nice comments at the beginning of your post...
I'll cut&paste your code and try to understand what you mean
Be back in half an hour

This post has been edited by pbl: 4 Jun, 2008 - 02:49 PM
User is online!Profile CardPM
+Quote Post

pbl
RE: Ok.... I Am Back
4 Jun, 2008 - 02:59 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,586



Thanked: 233 times
Dream Kudos: 75
My Contributions
RockBander you sure you are not bullshitting us a little bit ?
Your code does not even compile....
public void KeyReleased() is not implemented
And you will to post your class GHPlayer

User is online!Profile CardPM
+Quote Post

RockBander
RE: Ok.... I Am Back
4 Jun, 2008 - 04:27 PM
Post #8

New D.I.C Head
*

Joined: 12 May, 2008
Posts: 36


My Contributions
QUOTE(pbl @ 4 Jun, 2008 - 03:59 PM) *

RockBander you sure you are not bullshitting us a little bit ?
Your code does not even compile....
public void KeyReleased() is not implemented
And you will to post your class GHPlayer

o.... ok i'll do that
User is offlineProfile CardPM
+Quote Post

RockBander
RE: Ok.... I Am Back
4 Jun, 2008 - 05:59 PM
Post #9

New D.I.C Head
*

Joined: 12 May, 2008
Posts: 36


My Contributions
QUOTE(pbl @ 4 Jun, 2008 - 03:59 PM) *

RockBander you sure you are not bullshitting us a little bit ?
Your code does not even compile....
public void KeyReleased() is not implemented
And you will to post your class GHPlayer


hmm ok
so the main method
CODE

package hero;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.*;
import java.applet.*;
import java.net.*;

public class Main extends Applet implements Runnable
{
    private boolean keyCtrl;
    private int speed;                
    boolean isStoped = true;        
    private GHPlayer player;            
    private GreenNotes green;            
    Thread th;                            
    AudioClip hitnoise;        
    AudioClip outnoise;    
    
  
    private Image dbImage;
    private Graphics dbg;

    
    public void init ()
    {
        keyCtrl=false;
        setBackground (Color.black);

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

        hitnoise = getAudioClip (getCodeBase() , "beat.au");
        hitnoise.play();
        hitnoise.stop();
        outnoise = getAudioClip (getCodeBase() , "miss.au");
        outnoise.play();
        outnoise.stop();
        
        player = new GHPlayer ();
        
        green = new GreenNotes (20, 10, 0, 8, Color.green, outnoise, player);
    }


    public void start ()
    {
        th = new Thread (this);
        th.start ();
    }

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

    public void keyPressed(KeyEvent e)
    {
        int ctrl=KeyEvent.CTRL_DOWN_MASK;
        if (e.getModifiersEx() == ctrl)
        {
            if (keyCtrl == false)
            {
            keyCtrl = true;
            }
            else
            {
              keyCtrl = false;
            }
            repaint();
        }

        
            if (green.userHit (y))
            {
            hitnoise.play();

                green.NoteWasHit ();
            }
            else
            {
                outnoise.play();
            }
        return keyCtrl;
    }
    public void keyReleased(KeyEvent e)
    {
         int ctrl = KeyEvent.CTRL_DOWN_MASK;
        
        
            if (e.getKeyCode()==17) //17 = CTRL
            {
            keyCtrl = false;repaint();
            }
    }
    public void run ()
    {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

                green.move();

            repaint();

            try
            {
                Thread.sleep (speed);
            }
            catch (InterruptedException ex)
            {
                
            }

            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        
    }

    public void paint (Graphics g)
    {
        if (player.getScore() >= 1110)
        {
            g.setColor (Color.yellow);

            g.drawString ("Score: " + player.getScore(), 10, 40);
            green.DrawBall(g);

            
        }
        else if (player.getScore() > 10000)
        {
            g.setColor (Color.yellow);

            g.drawString ("Game over!", 130, 100);
            g.drawString ("You scored " + player.getScore() + " Points!", 90, 140);}
    }

    public void update (Graphics g)
    {
    
    }
}

and the gnotes vfile
CODE
package hero;

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

public class GreenNotes
{

    private int pos_y;                 
    private int y_speed;            
    private int radius;                
    private int first_x;private int first_y;            
    private int speed;        //8    

    
    
    private final int y_downout = 500;//boundary
    Color color;
    AudioClip out;
    GHPlayer player;
    public GreenNotes (int radius, int x, int y,  int vy,  Color color, AudioClip out, GHPlayer player)
    {
        this.radius = radius;//20
        pos_y = y;
        first_y = y;
        y_speed = vy;//8
        this.color = color;
        this.out = out;
        this.player = player;
    }


    public void move ()
    {pos_y += y_speed;}
    public void NoteWasHit ()
    {pos_y = first_y;}    
    public boolean userHit (int maus_y)
    {
        double y = maus_y - pos_y;
        double distance = y;
        if (distance < 40)
        {
            player.addScore (100);
            return true;
        }
        else
        {
            player.addScore (-50);
            return false;    
        }
    }
    public void DrawBall (Graphics g)
    {
        g.setColor  (color);
        g.fillOval (106,pos_y, 90, 40);//xpos=X-position-radius, Ypos=Y-[psotopm-radius,
    }
}



and the player one
CODE
package hero;

public class GHPlayer {
private int score;

    public GHPlayer()
    {
        score=0;
    }
    public int getScore()
    {
        return score;
    }
    public void addScore(int plus)
    {
        score+=plus;
    }
    
}



I'm sure there will be errors, and that's why i need help from people who know what they're doing....

thanks everybody for all the help

User is offlineProfile CardPM
+Quote Post

pbl
RE: Ok.... I Am Back
4 Jun, 2008 - 09:06 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,586



Thanked: 233 times
Dream Kudos: 75
My Contributions
OK a few comments...

CODE

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


thread.stop()... is deprecated don't use it...It created a lot of problems...
use a boolean variable like threadHasToStop and in your thread code check periodically for it

CODE

public void run() {
   ...
   if(threadHasToStop) return;
   ...
   if(threadHasToStop) return;
   ...
}


in your keyPressed() method "y" is not defined

heyPressed() is a void method it cannot return keyCtrl


User is online!Profile CardPM
+Quote Post

mensahero
RE: Ok.... I Am Back
4 Jun, 2008 - 09:36 PM
Post #11

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE


I'm trying to do "2. Is that a game? like bust-a-move or dance revo or something..
(i.e. when the note moved in the exact position as of the button and the user pressed the button, points will be added to the players score)"

but stuck...



Well if thats the case.. You could add a method in your button keypressed that will check the rectangular position of your notes and compare it with the rectangular position of your button..

if the note enters the button rectangular position and the user click it then a point is passed in the score. blink.gif something like that would work.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Ok.... I Am Back
4 Jun, 2008 - 09:41 PM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,586



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mensahero @ 4 Jun, 2008 - 10:36 PM) *

Well if thats the case.. You could add a method in your button keypressed that will check the rectangular position of your notes and compare it with the rectangular position of your button..

if the note enters the button rectangular position and the user click it then a point is passed in the score. blink.gif something like that would work.

Hey Mensahero offer a beer to Cindy for me... I'll pay the tab next time I'll be in Manilla

User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:32PM