0

In my code below....i wish to add a yellow bar at the bottom if upon firing a bullet, the bullet reaches the moving bar at the top(which is a trail of alternative yellow and cyan boxes) and touches it at a yellow box.For that what I am planning is to first detect the color of the pixel where the bullet touches the moving bar and then add yellow bar below if the pixel color was yellow............Here is my code: //

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class abyss extends Applet implements Runnable,KeyListener{
int lim=446,l,src,i=-40,n,c,ct=450,cl=225,y,f,bl,bw,fr,mud=0;
Thread v=null;

public void init() {
setBackground(Color.black);
addKeyListener(this);
                          }
public void start() {
       v=new Thread(this);
       v.start();
                                   }
public void run() {
try {
         int trainDelay = 0;

    while (true) {
           if(ct<=200)
           {repaint();
            }

            if (y == 1) {
            if (f<=41) {
                bl = cl + 25;
                bw = 10;
                f = lim;
            } 
                   if (f > 41) {
                repaint(bl, f, bw, bw + 1);
           if (--f<=41) {
                   if(l%80==0)
                   {mud=1; 
                    ct=ct-20;
                    System.out.println("decreased");
                    lim=lim-20; 
                    repaint();}
                   else if(l%80!=0  && (ct+20<=420))
                   {ct=ct+20;
                    lim=lim+20; 
                    }
                    y = 0;
                    bw = 0;
                                      }
            }
        }

        if (trainDelay <= 0) {
            repaint();
            i = i + 40;
            c = 1;
            n = i / 40;
            trainDelay = 200;
        }
        Thread.sleep(5);
        trainDelay--;
   }
} catch (Exception e) {
}

}

public void paint(Graphics g) {
if(ct>=200){
if(mud==1)
{g.setColor(Color.orange);
g.fill3DRect(0,ct+20,500,450-ct,true);}
g.setColor(Color.darkGray);
g.fill3DRect(0,200,30,300,true);
g.fill3DRect(470,200,30,300,true);
g.fill3DRect(0,470,500,30,true);
g.setColor(Color.blue);
g.fill3DRect(cl,ct,50,20,true);
setBackground(Color.black);
for(int j=n-1;j>=0;j--)
{  l=j*40;
if((c%2)==0)
{g.setColor(Color.cyan);
g.fill3DRect(l,0,50,40,true);}
else
{g.setColor(Color.orange);
g.fill3DRect(l,0,50,40,true);}
c++;
}
} 

{g.setColor(Color.yellow);
g.fillOval(bl,f,bw,bw);
if(ct<=200)
{setBackground(Color.red);
g.setColor(Color.yellow);
g.drawString("You win!!",215,210);
}   
}                                         }

public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT && cl>=38){
cl=cl-10;}
if(e.getKeyCode()==KeyEvent.VK_RIGHT && cl<=412){
cl=cl+10;}
if(e.getKeyCode()==KeyEvent.VK_UP){
y=1;}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void stop() {
try{wait();}
catch(Exception e) {}
}
public void destroy() {}
}

Please tell me how to detect the color.........can getColor() be used??

Suraj Pandey
  • 31
  • 1
  • 7
  • Don't swallow exceptions. Also, your code is not thread-safe. – SLaks Apr 23 '13 at 16:46
  • 2
    Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Apr 23 '13 at 16:48
  • Thanks for suggestions but this is actually my first venture with applet animation...........so i want to invest more time in logic then on presentation.Somebody come to rescue. – Suraj Pandey Apr 23 '13 at 17:05
  • @SurajPandey, if you want people to help you, a good first step is not forcing them to work extra-hard just to understand the problem: Formatting and indentation helps *us* to help *you*. – Textmode Aug 29 '13 at 00:11

1 Answers1

0

It looks like you are off to a good start. I would recommend that you have your cyan and yellow boxes be objects with a color and location attributes.

Create a getColor method for your box objects, and create a getLocation method for your box objects and bullet object.

when bullet.getLocation = box.getLocation then box.getColor

Take a look at this Java/Processing App that sets the color of a circle when clicked on. It uses principles similar to what I just described.

https://sourceforge.net/projects/all-spark-cube/

https://github.com/spudstud/All-Spark-Cube/blob/master/Sandbox/Panel_2d/LedObject.pde

spuder
  • 17,437
  • 19
  • 87
  • 153
  • Thanks spuder........nyc to see a person with solution and not complaints.........but what you are suggesting requires me to change the whole code............i want to implement the whole code as it is .................please tell me how to use color detection methods for the code as it is......... – Suraj Pandey Apr 24 '13 at 08:44