Posts which you will also like.

Wednesday, January 26, 2011

A JAVA Program:Bouncing ball with color change on collision with walls

In this program a ball is created which bounces back and forth and also changes its color on collision with the walls of the container.

CODE:

import java.awt.Graphics;
import java.util.Random;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
class bounceball extends JPanel
{
    private static final int win_w=640;
    private static final int win_h=480;
    private float rad=100;// radius of the ball
    private float x=rad+50;
    private float y=rad+20;//center coordinates of the ball
    private float sx=4;
    private float sy=3;       //change in the coordinates(x and y)
    private static final int update_rate=30;//refresh of screen per second
    // Now constructor
    private Random r;
    private Color clr=Color.BLUE;
    public void set(Color c)
    {
        clr=c;
    }
    public Color get()
    {
        return clr;
    }
    public bounceball()
    {
        r=new Random();
        setPreferredSize(new Dimension(win_w,win_h));//size of the panel is set to
        //the size of the window
        Thread ballthread=new Thread(){
            public void run(){
                                while(true)
                                {
                                    x+=sx;
                                    y+=sy;
                                    //here we handle collision with the window walls
                                    //checking the horizontal collision
                                    if(x-rad<0)
                                    {  
                                        sx=-sx;//reverse the direction
                                        x=rad;
                                        set(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
                                      
                                    }
                                    else if(x+rad>win_w)
                                    {
                                        sx=-sx;
                                        x=win_w-rad;
                                        set(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
                                    }
                                    //checking the vertical collision
                                    if(y-rad<0)
                                    {
                                        sy=-sy;
                                        y=rad;
                                        set(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
                                    }
                                    else if(y+rad>win_h)
                                    {  
                                        sy=-sy;
                                        y=win_h-rad;
                                        set(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
                                    }
                                    repaint();
                                    try
                                    {
                                        Thread.sleep(1000/update_rate);
                                    }
                                    catch(InterruptedException e)
                                    {
                                        JOptionPane.showMessageDialog(null,"Please don't interrupt me while sleeping !","Disturbed",JOptionPane.ERROR_MESSAGE);
                                    }
                                }
                             }
                            };
        ballthread.start();//start the thread's execution      
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(0,0,win_w,win_h);
        g.setColor(get());
        g.fillOval((int)(x-rad),(int)(y-rad),(int)(2*rad),(int)(2*rad));
        g.setColor(Color.WHITE);
        String s=String.format("Position:[%3.0f,%3.0f]",x,y);
        g.drawString(s,20,40);
    }
    public static void main(String [] args)
    {
        JFrame jr=new JFrame("Bouncing ball.");
        bounceball ball=new bounceball();
        jr.add(ball);
        jr.setVisible(true);
        jr.setSize(650,500);
        jr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jr.pack();
    }
}

OUTPUT:


More on this at techcresendo.com

Read More ->>

Thursday, January 13, 2011

A java program to print a shape using wired mesh

This program uses swing and Graphics class.All coding is straight forward and can be easily understood.You may need to create main method to use this.

import javax.swing.JPanel;
import java.awt.Graphics;
public class Drawpanel4 extends JPanel
{
    public void paintComponent(Graphics g)
    {
        int width=getWidth();
        int height=getHeight();
        int stepx=width/15;
        int stepy=height/15;
        super.paintComponent(g);
        int x=0,y=0,i=0,j=0,k=0,l=0;
        while(i<15)
        {   
            g.drawLine(0,y,x,height);
            y+=stepy;
            x+=stepx;
            i++;
       
        }
        x=0;y=height;   
        while(j<15)
        {
            g.drawLine(0,y,x,0);
            y-=stepy;
            x+=stepx;
            j++;
        }
        x=width;y=0;
        while(k<15)
        {
            g.drawLine(width,y,x,height);
            y+=stepy;
            x-=stepx;
            k++;
        }
        y=height;x=width;
        while(l<15)
        {
            g.drawLine(width,y,x,0);
            x-=stepx;
            y-=stepy;
            l++;
        }
    }
}



OUTPUT:


Read More ->>

Wednesday, January 12, 2011

Program in java to show file description

This program shows the full description of a file or directory such as name ,when modified , length etc.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class FileDemonstration extends JFrame
{
    private JTextArea outarea;
    private JScrollPane scroll;
    public FileDemonstration()
    {
        super("Testing Class file..");
        outarea=new JTextArea();
        outarea.setFont(new Font("Serif",Font.BOLD,14));
        scroll=new JScrollPane(outarea);
        add(scroll,BorderLayout.CENTER);
        setSize(400,400);
        setVisible(true);
        analyzefiles();
    }
    private File getFileinf()
    {
        JFileChooser fc=new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int result=fc.showOpenDialog(this);
        if(result==JFileChooser.CANCEL_OPTION)
            System.exit(1);
           
        File fname=fc.getSelectedFile();
        if((fname==null) || (fname.getName().equals("")))
        {
            JOptionPane.showMessageDialog(this,"Invalid Name!","Invalid Name!",JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
        return fname;
    }
    public void analyzefiles()
    {
        File name=getFileinf();
        if(name.exists())
        {
            outarea.setText(String.format("%s %s\n %s\n %s\n %s\n %s \n %s\n%s\n %s %s\n %s %s",
            name.getName(),"exists.",(name.isFile()?" is file.":"is not a file."),
            (name.isDirectory()?"is directory":"is not a directory"),(name.isAbsolute() ? " is absolute.":"is not absolute"),
            "Last Modified:",name.lastModified(),"Length",name.length(),"Path:",name.getPath(),"Absolute Path",name.getAbsolutePath(),
            "Parent",name.getParent()));
        }
        if(name.isDirectory())
        {
            String[] dir=name.list();
            outarea.append("\n\nDirectory content\n\n");
            for(String dirname:dir)
                outarea.append(dirname+"\n");
        }
        else
            {
                JOptionPane.showMessageDialog(this,name+"does not exits.","Error",JOptionPane.ERROR_MESSAGE);
            }
    }
}
class jfile
{
    public static void main(String args[])
    {
        FileDemonstration f=new FileDemonstration();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

output:



Read More ->>

Saturday, January 8, 2011

Demonoid Invitation codes

Entry to demonoid:
Demonoid is one of the top most ranked torrent tracker website.One can only register to this site either through invitation code or through open seasons.
Open season means the first week of the month when demonoid is open for all. Within this period we don't need the invitation code to register.Although invitation codes are free but some people tries to sell it because of its demand.
Now demonoid.com is changing its  address to demonoid.Me.



Some invitation codes are:

qc8h14wymfg3gr70sb1dai0rnjq82qncr271tz0
tcb6we3sz1mjvrhhs00m192m5amk6ei6btivlut0coy7
3giy5v5usworywionwrijgv4bpovmfn6bil
whyqv9d1lrqqhat9gayhwcw105b7v18ul5xc9p1jm3
av4w4rzpse3t2uchormaxu316cs55h5zf40o34opkcp
sujwprq4slhqc8178vsl00jtdyxtz3nccqfqvxop6
lclvuoigqclk988fngw4zf4hs4bmb8iv0agpuof
70kasx53pw0ot0hmz4o69r90w01lkqh32iru37eqh4djn
h25zfcjn76a1lls739ous58aun0hovf870nl
kezd9l0ht0hdim9g64n3ao48efi2c7tm
eiibawgpetm65r1abo38lsi0bxajui3f6q
v0187ko1g6ocstv823pqfqt704ijqdis0am1qovfiyu
efycj6bbzaqax1qq98cgwybxpanghyu9augnmiv9tkgh
helldown3Xa3mb0rbyr9y4p0vj37flqqzm6zn24j4jvkl
helldown3Xh21g8y9f7y8mubfdwaawrow49n
helldown3Xaoti0lndjcmmqul1zmur9l0bzvy7xvi7jnj
helldown3Xpvj1aojabb8hz9tl57i3pvsx2y0n8gdvwtk
helldown3X3tra2neoiyb63kxayw391z9bdla9ocsfve0
helldown2X4vi138bd8m0d7jyk7pt2691dyoqtq7vz6
helldown2X9xpayin63q69so3k9s8fgb79ihga
helldown2Xpf2gwu279mljniworo35kwsueca0v4o5p8y
helldown2Xsfpf2wy88jq6i4otq911klcgdbi7upcu690
helldown2Xn3xmy23hruspns0bjovnibxbkgge6zhaxfo

There is no guarantee that all of these codes may work because some of them my have been used already.

Read More ->>
DMCA.com Protected by Copyscape Online Plagiarism Tool