Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how do I make a random falshing color.
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ecookman




PostPosted: Tue Jul 14, 2009 3:36 pm   Post subject: how do I make a random falshing color.

I am new to java, and am making a asteroids game.
I thought it would be cool if I could have my spaceship flash different colors, is this possible, and if it is, how would this be possible.
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Tue Jul 14, 2009 4:06 pm   Post subject: Re: how do I make a random falshing color.

All depends on how your rendering your Spaceship.
Tony




PostPosted: Tue Jul 14, 2009 4:46 pm   Post subject: Re: how do I make a random falshing color.

ecookman @ Tue Jul 14, 2009 3:36 pm wrote:
I thought it would be cool if I could have my spaceship flash different colors...

I don't think that would be cool.

Zren @ Tue Jul 14, 2009 4:06 pm wrote:
All depends on how your rendering your Spaceship.

It all depends on how you're rendering your spaceship.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
saltpro15




PostPosted: Tue Jul 14, 2009 7:57 pm   Post subject: RE:how do I make a random falshing color.

You tell him Tony! Learn yourself some grammer Zren! Laughing
Zren




PostPosted: Tue Jul 14, 2009 8:25 pm   Post subject: Re: RE:how do I make a random falshing color.

Oh hush you two. My grammar strangely suffers during the period between June and September.

Oh and by the way salty: grammer.

Bloody Grammar Nazis (expression alright).
ecookman




PostPosted: Tue Jul 14, 2009 8:50 pm   Post subject: RE:how do I make a random falshing color.

@tony
lol good thing you won't be making the game Razz


and I am making my spaceship using vector art...
if that helps any.
I will dig up the code for it and post it later.
ecookman




PostPosted: Tue Jul 14, 2009 9:00 pm   Post subject: Re: how do I make a random falshing color.

Java:

//Class asteroids
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.Timer;
import java.util.*;

public class asteroids extends Applet implements KeyListener, ActionListener  {
    boolean UK,LK,RK;
       
    Timer timer;
    Image offscreen;
    Graphics offg;
    SpaceCraft ship;
    asteroid roid;
    ArrayList <asteroid> Alist;

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
   
    public void init() {
        this.setSize(900, 600);
        this.addKeyListener(this);
       
        ship=new SpaceCraft();
        timer=new Timer(20, this);
        offscreen=createImage(this.getWidth(), this.getHeight());
        offg= offscreen.getGraphics();
        roid = new asteroid();
        Alist= new ArrayList();
        for (int i = 0; i<5; i++){
            Alist.add(new asteroid());   
       
       }
    }
    public void paint (Graphics g) {
        offg.setColor(Color.black);
        offg.fillRect(0,0,900,600);
        offg.setColor(Color.green);
        if (ship.active){
        ship.paint(offg);
        }

        offg.setColor(Color.yellow);
        for (int i = 0; i<5; i++){
            Alist.add(new asteroid());
        }
   
        roid.paint(offg);
        g.drawImage (offscreen, 0,0,this);
        repaint();
    }
    public void update (Graphics g){
        paint(g);
    }
   
    public void actionPerformed(ActionEvent e){
        respawnS();
        ship.updateposition();
        keyChecked();
        roid.updateposition();
        checkCollision();
       
       
    }
   
    public void keyPressed (KeyEvent a){
       if (ship.xspeed > 25){
           ship.xspeed =25;
       }
       if (ship.yspeed > 25){
           ship.yspeed =25;
       }
        if (a.getKeyCode() ==KeyEvent.VK_RIGHT){
         
            RK=true;
        }
        if (a.getKeyCode() ==KeyEvent.VK_LEFT){
            LK=true;
        }
        if (a.getKeyCode() ==KeyEvent.VK_UP){
           
           UK=true;
        }

       repaint();
    }
    public void keyReleased (KeyEvent a){
        if (a.getKeyCode() ==KeyEvent.VK_RIGHT){
            RK=false;
        }
        if (a.getKeyCode() ==KeyEvent.VK_LEFT){
            LK=false;
       
        }
         if (a.getKeyCode() ==KeyEvent.VK_UP){
            UK=false;
         }
    }
    public void keyChecked (){
    if (RK==true) {
        ship.TRight();
    }
    if (LK==true) {
        ship.TLeft();
    }

    if (UK==true){
    ship.accellerate();
    }
    }
    public void keyTyped (KeyEvent a){}

    public void start(){
    timer.start();
    }
    public void stop (){
    timer.stop();
    }

    public boolean collision(VectorSprite Thing1, VectorSprite Thing2){
        int x,y;
        for (int i = 0; i<Thing1.drawShape.npoints; i++){
            x=Thing1.drawShape.xpoints[i];
            y=Thing1.drawShape.ypoints[i];

            if (Thing2.drawShape.contains (x , y )){
                return true;
            }
        }
        for (int i = 0; i<Thing2.drawShape.npoints; i++){
            x=Thing2.drawShape.xpoints[i];
            y=Thing2.drawShape.ypoints[i];
            if (Thing1.drawShape.contains (x , y )){
                return true;
            }
        }
        return false;
       
    }
    public void checkCollision()  {

        if (collision(roid, ship)){
            ship.Hit();
        }
    }
    public void respawnS(){

        if(ship.active==false&& ship.counter>= 5){
            ship.reset();


           
       }
    }
}




this is my main class and here are my other ones (p.s if you are going to run the applet, you have to run it from this class...using netbeans press shift F6 when clicked on this class to launch it)



Java:


// class #2 VectorSprite

import java.awt.*;



public class VectorSprite {
    double xspeed;
    double yspeed;
    double xposition;
    double yposition;
    double angle; 
    Polygon shape, drawShape;
    double ROTATION, THRUST;
    boolean active;
    int counter;
   
    public void paint(Graphics g){
        g.drawPolygon (drawShape);
       
    }
    public void updateposition () {
        counter ++;
        xposition+= xspeed;
        yposition+= yspeed;
        wrap();
        int x;
        int y;
       
        for (int i =0; i< shape.npoints; i++){
           // shape.xpoints[i] +=xspeed;
            //shape.ypoints[i] +=yspeed;
           
            x= (int)Math.round(shape.xpoints[i]* Math.cos(angle)-shape.ypoints[i] * Math.sin(angle));
            y= (int)Math.round(shape.xpoints[i]* Math.sin(angle)+shape.ypoints[i] * Math.cos(angle));
       
 
        drawShape.xpoints[i]= x;
        drawShape.ypoints[i]= y;

        }
        drawShape.invalidate();
        drawShape.translate((int)xposition, (int)yposition);
    }

    public void wrap(){
        if (xposition>905){
            xposition=-5;
        }
            if (yposition>605){
                yposition=-5;
            }
         if (xposition<-5){
            xposition=900;
        }
            if (yposition<-5){
                yposition=605;
            }
    }
}






/////////class #3 SpaceCraft////////////
import java.awt.*;

public class SpaceCraft extends VectorSprite {


    public SpaceCraft(){
        active=true;
        shape=new Polygon();
        shape.addPoint(25, 0);
        shape.addPoint(-18, 30);
        shape.addPoint(-2, 8);
        shape.addPoint(-2, -8);
        shape.addPoint(-18, -30);
//////////////////////////////////        shape.addPoint(15, 0);
///////////variation 1////////////        shape.addPoint(-20, 10);
//////////////////////////////////        shape.addPoint(-7, 6);
//////////////////////////////////        shape.addPoint(-7, -6);
//////////////////////////////////        shape.addPoint(-20, -10);
        ///        shape.addPoint (25,0);
//        shape.addPoint (-5,-15);
//        shape.addPoint (-2,-9);
//        shape.addPoint (-2,9);
//        shape.addPoint (-5,15);
        //shape.addPoint (-15,10);
        ROTATION= .2;
        THRUST = .45;

    drawShape=new Polygon();
       
        drawShape.addPoint(25, 0);
        drawShape.addPoint(-18, 30);
        drawShape.addPoint(-2, 8);
        drawShape.addPoint(-2, -8);
        drawShape.addPoint(-18, -30);
////////////////////////////////////        drawShape.addPoint(15, 0);
////////////////////////////////////        //top fin
////////////////////////////////////        drawShape.addPoint(-20, 10);
/////////////varation 1///////////////////////        drawShape.addPoint(-7, 6);
////////////////////////////////////        drawShape.addPoint(-7, -6);
////////////////////////////////////        //bottom fin
////////////////////////////////////        drawShape.addPoint(-20, -10);
////        drawShape.addPoint (25,0);
////        drawShape.addPoint (-5,-15);
////        drawShape.addPoint (-2,-9);
////        drawShape.addPoint (-2,9);
////        drawShape.addPoint (-5,15);
//        drawShape.addPoint(25,0);
//        drawShape.addPoint(-15,-10);
//        drawShape.addPoint(-10,25);
          xposition = 450;
          yposition = 300;
    }
    public void accellerate(){
    xspeed+= Math.cos(angle)*THRUST;
    yspeed+= Math.sin(angle)*THRUST;
}

    public void TLeft (){
    angle-= ROTATION;
}
    public void TRight(){
    angle+= ROTATION;
}

    public void reset(){
        if (active == false){
            xposition= 450;
            yposition= 300;
            xspeed=0;
            yspeed=0;
            angle= -Math.PI/2;
            active=true;
}



}

    public void Hit (){
        active=false;
        counter = 0;


    }
}





//////////class 4 asteroid (not asteroids this is for the floating roids'//////////
import java.awt.*;

public class asteroid extends VectorSprite  {
public asteroid(){
        active=true;
        double H,A;
        shape=new Polygon();
//           shape.addPoint(6,5);
//        shape.addPoint(5,35);
//        shape.addPoint(-25,10);
//        shape.addPoint(-17,-15);
//        shape.addPoint(20,-35);   
        ROTATION= .02;
       
        H=Math.random()*400+100;
        A=Math.random()*Math.PI*2;
       
        xposition = Math.cos(A)*H+450;
        yposition = Math.sin(A)*H+300;
        yposition = Math.random()+1*600;
        drawShape=new Polygon();
//        drawShape.addPoint(30,3);
//        drawShape.addPoint(5,35);
//        drawShape.addPoint(-25,10);
//        drawShape.addPoint(-17,-15);
//        drawShape.addPoint(20,-35);
     
     H=Math.random()*5;
     A=Math.random()*Math.PI*2;
     xspeed=  Math.cos(A)*H;
     yspeed= Math.sin(A)*H;
     // A stands for angle
     double curA;
     curA=0;
     double Aup;
      int sides = (int)(Math.random()*3+4);
        Aup=Math.PI*2/sides;
        double tempx;
        double tempy;
        for (int i = 0; i<sides;i++){
//           1) use rand num gen to find h
//           2)find x/y components using trig
//           3)add x/y point to both drawshape and shape
              H=Math.random()*30+50;
              tempx=Math.cos(curA)*H;
              tempy=Math.sin(curA)*H;
              drawShape.addPoint ((int)tempx,(int)tempy);
              shape.addPoint((int)tempx,(int)tempy);
              curA+=Aup;
       
       
        }   
     
     
     
}   
  public void updateposition() {
        angle+= ROTATION;
        super.updateposition();
     

}
}
   



This is all of the code I have. What happens so far, if you aren't going to run it... i have my ship and it freely can move around the screen, when it touches a asteroid it resets.
Zren




PostPosted: Tue Jul 14, 2009 10:54 pm   Post subject: Re: how do I make a random falshing color.

Since it's vector art:

Have the ship class contain the following.

code:
int colourCounter = 0
An array of colours containing the colours you want to cycle.



Then, where you do the logic part of the class:

You now have a few variations to do. Random colour (1,6,4,2,5...), cycle in order (1,2,3,1...), cycle back and forth (1,2,3,2...).

Random:
Set the colourCounter to a random number to the size of the Colours array.

In order:
colourCounter++
if (colourCounter>Colours.size) colourCounter = 0

Back & Forth:
You need another variable indicating direction. It's basically in Order with an extra if statement.
This method is very nice to achieve a glow effect if the colours are in order by how light they are.

Then have it render with the colour set to Colours[colourCounter].

You get?
Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Wed Jul 15, 2009 12:01 am   Post subject: Re: how do I make a random falshing color.

Alternatively, if you want truly random colors, you could set the color using the Random class and the Color.getHSBColor(float h, float s, float b). Although creating an array of Colors yourself and cycling through them is probably more efficient for the program.

code:

import java.applet.*; //Applets are good.
import java.awt.*;  //Colours and stuff
import java.util.*;  //Random numbers are fun.

public class randColor extends Applet{
    Random r = new Random(); //a new random number!

    public void start(){
        setSize(70,70); //small window.
    }
    public void paint(Graphics g){
        g.setColor( //set the color
            Color.getHSBColor(r.nextFloat(),r.nextFloat(),r.nextFloat())
              //to a (sort of) random HSB color value
        );
        g.fillRect(10,10,50,50); //Draw a rectangle
        try{
                Thread.sleep(200); //wait
        }
        catch(InterruptedException e){          
        }
        repaint(); //start again
    }
}
ecookman




PostPosted: Wed Jul 15, 2009 12:09 pm   Post subject: Re: how do I make a random falshing color.

//space craft

public SpaceCraft(){
r= 255;
g= 0;
b= 100;
colour = new Color(r,g,b);
active=true;
....


//i added this on the bottom of the class
public void updateposition(){
r= (int)(Math.random()*255);
g= (int)(Math.random()*255);
b= (int)(Math.random()*255);
colour=new Color(r,g,b);
super.updateposition();


and then

// VectorSprite class
public void paint(Graphics g){
// i added this
g.setColor(colour);





I have no clue how to get it to transition from one color to another
TheGuardian001




PostPosted: Wed Jul 15, 2009 1:49 pm   Post subject: Re: how do I make a random falshing color.

Following your instructions on those additions, it works for me.
ecookman




PostPosted: Wed Jul 15, 2009 1:50 pm   Post subject: RE:how do I make a random falshing color.

i am not sure on how to implement it.
Zren




PostPosted: Wed Jul 15, 2009 5:44 pm   Post subject: Re: how do I make a random falshing color.

Another thing that would be best is if you have it rotating/randoming on a timer that way it isn't changing EVERY single cycle.

Here's what I'd have changed from the initial code to get something working. It's not exactly how your doing it, but it should help you find where you need to go.
Set of Colours
Random

VectorSprite Class
Color colour

getColour method
returns colours[colourCounter]

paint method
setColor( this.getColour() );

Spaceship Class
int colourCounter
Color colours[]


Init method
define colours and set counter to 0
this.nextColour()


nextColour method
increment next colour
check if counter is over array size, and if so, reset
set VectorSprite variable colour to colours[colourCounter]

set VectorSprite variable colour new random colour

Asteroids Class

ActionPerformed method
ship.nextColour()


If you want to add a timer around it, then add a new variable in either VectorSprite or Spaceship (Depending on if you only want the ship to go flash-like), that would represent the time between colour changes. Also add a variable to represent the time at the last colour change.
ecookman




PostPosted: Wed Jul 15, 2009 8:07 pm   Post subject: RE:how do I make a random falshing color.

what if I made a if statement around each of my " r g b" variables and have it say something like if the value of the variable is 255 then variable -1. but then how would i get it to counting back up w/o having infinite if statements.

(sorry this is my first time using java and this is kinda complicated for me... I have never worked with counting or timers.)
Zren




PostPosted: Wed Jul 15, 2009 9:07 pm   Post subject: Re: how do I make a random falshing color.

Java:
int i = 0;
while (true) {
i++;
if (i > 255) i = 0;
}


This would loop forever going 0, 1, 2, ..., 255, 0, 1, ...

ActionPerformed is basically inside a loop, which exits when the applet closes. You following?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: