Thursday, March 31, 2016

Image Background Remover

As the name implies, this program removes the background from any image or photo. Unwanted colors are first selected by drawing rectangle on the main panel using mouse drag event. The removal process is then performed through the use of the getRGB() and setRGB() methods.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;


public class BackgroundRemover extends JFrame{
    private JButton bopen=null;
    private JPanel pmain=null;
    private String fname=null;
    private JButton bstart=null;
    private BufferedImage bufimage=null;
    private BufferedImage grayimage=null;
    private Color pixcolor[][]=null;
    private Point pstart=null;
    private Point pend=null;
    
    public BackgroundRemover(){
        super("Background Remover");
        setSize(800,600);
        setLayout(new BorderLayout());
        
        bopen=new JButton("Open");
        bopen.addActionListener(new ButtonHandler());
        pmain=new WallImage();
        pmain.addMouseListener(new BackgroundRemover.MouseHandler());
        pmain.addMouseMotionListener(new BackgroundRemover.MouseHandler());
        bstart=new JButton("Remove");
        bstart.addActionListener(new ButtonHandler());
        
        add("North",bopen);
        add("Center",pmain);
        add("South",bstart);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class WallImage extends JPanel{
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            
            Graphics2D g2=(Graphics2D)g;
            
            if(bufimage!=null){
                g2.drawImage(bufimage,0,0,pmain);
            }
            
            if(pstart!=null&&pend!=null){
                g2.setColor(Color.YELLOW);
                g2.drawRect(pstart.x,pstart.y,pend.x-pstart.x,pend.y-pstart.y);
            }
        }
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand()=="Open"){
                FileDialog fd=new FileDialog(BackgroundRemover.this,"Open Image File",FileDialog.LOAD);
                fd.setDirectory("C:\\");
                fd.setVisible(true);
                fname=fd.getDirectory()+fd.getFile();
                try{
                    bufimage=ImageIO.read(new File(fname));
                }catch(Exception ex){
                    
                }
                pstart=null;
                pend=null;
                pmain.repaint();
            }
            if(e.getActionCommand()=="Remove"){
                int widthRect=pend.x-pstart.x;
                int heightRect=pend.y-pstart.y;
                pixcolor=new Color[widthRect][heightRect];
                
                //Graphics2D g2=(Graphics2D)bufimage.getGraphics();
            
                for(int i=0;i<widthRect;i++){
                    for(int j=0;j<heightRect;j++){
                        pixcolor[i][j]=new Color(bufimage.getRGB(pstart.x+i,pstart.y+j));
                    }
                }
                
                int widthImg=bufimage.getWidth();
                int heightImg=bufimage.getHeight();
                
                Graphics2D g2=(Graphics2D)bufimage.getGraphics();
            
                for(int i=0;i<heightImg;i++){
                    for(int j=0;j<widthImg;j++){
                        Color c=new Color(bufimage.getRGB(j,i));
                        for(int a=0;a<widthRect;a++){
                            for(int b=0;b<heightRect;b++){
                                if(c.equals(pixcolor[a][b])){
                                    bufimage.setRGB(j,i,new Color(255,255,255).getRGB());
                                }
                            }
                        }
                    }
                }
                
                pixcolor=null;
                repaint();
            }
        }
    }
    
    class MouseHandler implements MouseMotionListener, MouseListener{
        public void mouseMoved(MouseEvent e){
            
        }
        public void mouseDragged(MouseEvent e){
            pend = e.getPoint();
            repaint();
        }
        public void mouseEntered(MouseEvent e){
            
        }
        public void mouseExited(MouseEvent e){
            
        }
        public void mouseReleased(MouseEvent e){           
            repaint();
        }
        public void mousePressed(MouseEvent e){
            pstart=e.getPoint();
        }
        public void mouseClicked(MouseEvent e){

        }
    }    
    
    public static void main(String[] args){
        new BackgroundRemover();
    }
}

Tuesday, March 29, 2016

Cannonball Parabolic Motion

This program uses several laws of physics to simulate cannonball motion. The motion formula utilizes the Math class (sin and cos methods). The calculation is not highly accurate. It only takes into account the velocity of the cannonball (max 100 m/s), the launch angle (max 45 degrees), and gravitational force. However it should be enough to create a simple animation.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SinCos extends JFrame{
    private JTextField tf=null;
    private JTextField tf2=null;
    private JButton b=null;
    private JPanel pmain=null;
    private boolean shot=false;
    private double posx=0;
    private double posy=0;
    private CannonBall cb=null;
    
    public SinCos(){
        super("CannonBall");
        setSize(1100,400);
        
        tf=new JTextField();
        tf.setColumns(10);
        tf2=new JTextField();
        tf2.setColumns(10);
        b=new JButton("Shoot!");
        b.addActionListener(new ButtonHandler());
        JPanel ptop=new JPanel();
        ptop.setLayout(new FlowLayout());
        ptop.add(tf);
        ptop.add(tf2);
        ptop.add(b);
        
        pmain=new PanelDraw();
        pmain.setPreferredSize(new Dimension(1100,300));
        
        setLayout(new BorderLayout());
        add("North",ptop);
        add("Center",pmain);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class PanelDraw extends JPanel{
        public void paintComponent(Graphics g){
            //g.clearRect(0,0,1100,350);
            super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            
            g2.setColor(Color.BLACK);
            
            if(shot==true){
                g2.fillOval((int)posx,pmain.getHeight()-(int)posy,10,10);
            }
        }
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==b){
                cb=new CannonBall();
                Thread t=new Thread(cb);
                t.start();
            }
        }
    }
    
    class CannonBall implements Runnable{
        double t=0;
        double vx=0;
        double vy=0;
                
        public CannonBall(){
            shot=true;
            double deg=Double.valueOf(tf.getText()).doubleValue();
            if(deg>45){
                return;
            }
            double rad=deg*Math.PI/180;
            
            double vel=Double.valueOf(tf2.getText()).doubleValue();
            if(vel>100){
                return;
            }
            
            //dist=2*Math.pow(vel,2)*Math.sin(rad)*Math.cos(rad)/9.8;
            
            t=2*vel*Math.sin(rad)/9.8;
            
            vx=vel*Math.cos(rad);
            vy=vel*Math.sin(rad);
        }
        
        public void run(){
            for(int i=0;i<=Math.ceil(t);i++){
                double x=vx*i;
                double y=(vy*i)-(0.5*9.8*Math.pow(i,2));
                posx=x;
                posy=y;
                try{
                    Thread.currentThread().sleep(1000);
                }catch(Exception ex){
                    
                }
                pmain.repaint();
            }
            shot=false;
        }
    }
    
    public static void main(String[] args){
        new SinCos();
    }
}

Sunday, March 27, 2016

Entire-Screen Capture

This program captures the entire screen and saves it as an image file. The process is done through the use of the Robot class. The setState(JFrame.ICONIFIED) method minimizes the frame before the process begins and the setState(JFrame.NORMAL) method restores the frame when the process ends.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;

public class ScreenCapture2 extends JFrame{
    private JButton bcapture=null;
    
    public ScreenCapture2(){
        super("SCreen Capture");
        setSize(200,100);
        
        bcapture=new JButton("Capture");
        bcapture.addActionListener(new ButtonHandler());
        bcapture.setMnemonic(KeyEvent.VK_G);
        
        setLayout(new FlowLayout());
        add(bcapture);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand()=="Capture"){
                capturing();
            }
        }
    }
    
    void capturing(){
                try{
                    this.setState(JFrame.ICONIFIED);
                    Robot robot=new Robot();
                    String fileName="screenshot.jpg";
                    Rectangle rect=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
                    BufferedImage bufImage=robot.createScreenCapture(rect);
                    ImageIO.write(bufImage, "jpg", new File(fileName));                                
                    this.setState(JFrame.NORMAL);
                }catch(Exception ex){
                    
                }                    
    }
 
    public static void main(String[] args) {
        ScreenCapture2 ap=new ScreenCapture2();
    }
}

Saturday, March 26, 2016

Simple Port Scanner

This program scan the local machine to identify available ports. Scanning range is 0-1024 and can be easily changed by giving the startPort and/or stopPort a new value.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.swing.*;

public class PortScanner extends JFrame{
    static JTextArea ta=null;
    JScrollPane sp=null;
    
    public PortScanner(){
        super("Port Scanner");
        setSize(400,400);
        setLayout(new BorderLayout());
        
        JButton b=new JButton("Start");
        b.addActionListener(new ButtonHandler());
        add("North",b);
        
        ta=new JTextArea(20,20);
        sp=new JScrollPane(ta);
        add("Center",sp);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    public static void main(String[] args){
        new PortScanner();
    }
}

class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        int startPort=0;
        int stopPort=1024; //49151 - 65536
        
        CheckPort[] cp=new CheckPort[(stopPort+1)-startPort];
        
        for(int i=startPort;i<=stopPort;i++){
            cp[i]=new CheckPort(i);
            cp[i].start();
        }
    }
}

class CheckPort extends Thread{        
    int curPort=0;
    
    public CheckPort(int x){
        curPort=x;
    }
    
    public void run(){
        try{
            Socket sok=new Socket("127.0.0.1",curPort);
            PortScanner.ta.append("Port: "+curPort+" (in use)\n");
            sleep(1000);
            sok.close();
        }catch(Exception e){
            PortScanner.ta.append("Port: "+curPort+" (not in use)\n");
        }
    }
}

Sunday, March 20, 2016

6-Digit Password Memorizer

This program converts a 6-digit numeric passcode into an English word (a=1, b=2, etc) so that it would be easier to memorize. Regular expression is used twice in this program, to check whether the input string contains digits (0-9) only and to extract the words from the content of the text file (words.txt).



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;

public class PasswordMemorizer extends JFrame{
    private JTextField tfpass=null;
    private JPanel panel=null;
    private JLabel lsuggest=null;
    private JButton bgenerate=null;
    
    public PasswordMemorizer(){
        super("Password Memorizer");
        setSize(300,120);
        
        tfpass=new JTextField();
        tfpass.setColumns(15);
        panel=new JPanel();
        lsuggest=new JLabel();
        bgenerate=new JButton("Generate");
        bgenerate.addActionListener(new ButtonHandler());
        
        setLayout(new BorderLayout());
        add("North",tfpass);
        panel.add(lsuggest);
        add("Center",panel);
        add("South",bgenerate);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String pwd=tfpass.getText();
            if(pwd.length()!=6){
                return;
            }
            Pattern pattern=Pattern.compile("\\d{6}");
            Matcher matcher=pattern.matcher(pwd);
            boolean found=false;
            while(matcher.find()){
                found=true;
            }
            if(!found){
                return;
            }
            
            try{
                Scanner sc=new Scanner(new FileInputStream("words.txt"));
                StringBuffer sbuf=new StringBuffer();
                sbuf.append(sc.nextLine());
                String regex=" ";
                Pattern p=Pattern.compile(regex);
                String[] items=p.split(sbuf);
                for(int i=0;i<items.length;i++){
                    String item="";
                    for(int j=0;j<items[i].length();j++){
                        String num=items[i].charAt(j)+"";
                        switch(num){
                            case "a":
                                num="1";
                                break;
                            case "b":
                                num="2";    
                                break;
                            case "c":
                                num="3";    
                                break;
                            case "d":
                                num="4";    
                                break;
                            case "e":
                                num="5";    
                                break;
                            case "f":
                                num="6";    
                                break;
                            case "g":
                                num="7";    
                                break;
                            case "h":
                                num="8";    
                                break;
                            case "i":
                                num="9";
                                break;
                            case "j":
                                num="10";    
                                break;
                            case "k":
                                num="11";
                                break;
                            case "l":
                                num="12";
                                break;
                            case "m":
                                num="13";    
                                break;
                            case "n":
                                num="14";    
                                break;
                            case "o":
                                num="15";    
                                break;
                            case "p":
                                num="16";
                                break;
                            case "q":
                                num="17";    
                                break;
                            case "r":
                                num="18";
                                break;
                            case "s":
                                num="19";
                                break;
                            case "t":
                                num="20";
                                break;
                            case "u":
                                num="21";
                                break;
                            case "v":
                                num="22";
                                break;
                            case "w":
                                num="23";    
                                break;
                            case "x":
                                num="24";
                                break;
                            case "y":
                                num="25";
                                break;
                            case "z":
                                num="26";
                                break;
                        }
                        item=item+num;
                    }
                    if(pwd.equals(item)){
                        lsuggest.setLocation(panel.getWidth()/2,panel.getHeight()/2);
                        lsuggest.setText(items[i]);
                    }
                    item="";
                }
            }catch(Exception ex){
                
            }
        }
    }
    
    public static void main(String[] args){
        new PasswordMemorizer();
    }
}

Friday, March 18, 2016

Playing Audio File (WAV)

This simple audio player utilizes JMF. Sound files (.wav) are first loaded from a predetermined folder. File names are then added to a list. The ListSelectionListener interface allows the user to select one of the sound files to be played.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.media.Manager;
import javax.media.Player;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class AudioPlayer extends JFrame{
    private Player audioPlayer=null;
    private JList lfiles=null;
    private File dir=null;
    
    public AudioPlayer(){
        setSize(300,200);
        
        lfiles=new JList();
        dir=new File("C://Users/MARIO/Documents/sound");
        String files[]=dir.list();
        lfiles.setListData(files);
        lfiles.addListSelectionListener(new ListHandler());
            
        setLayout(new BorderLayout());
        add("Center",lfiles);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    class ListHandler implements ListSelectionListener{
        public void valueChanged(ListSelectionEvent ev) {
            String filename=dir+"\\"+(String)lfiles.getSelectedValue();
            System.out.println(filename);
            File f=new File(filename);
            try{
                audioPlayer=Manager.createRealizedPlayer(f.toURL());
                audioPlayer.start();
            }catch(Exception e){
                                
            }
        }
    }
    
    public static void main(String[] args){
        new AudioPlayer();
    }
}