Tuesday, March 15, 2016

Excel File Reader

This short program serves as an Excel reader. Using the Apache POI API, it fetches data from an .xls file and displays it on its panel.



  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
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader extends JFrame{
    private JTextField tftitle=null;
    private String filename=null;
    private JLabel ldata=null;
    
    public ExcelReader(){
        super("Excel Reader");
        setSize(400,240);
        
        setLayout(new BorderLayout());
        
        JPanel pleft=new JPanel();
        pleft.setSize(200,240);
        pleft.setLayout(new FlowLayout());
        tftitle=new JTextField();
        tftitle.setColumns(10);
        pleft.add(tftitle);
        JButton bfile=new JButton("File");
        bfile.addActionListener(new ButtonHandler());
        pleft.add(bfile);
        add("West",pleft);
        
        JPanel pright=new JPanel();
        pright.setSize(200,240);
        ldata=new JLabel();
        pright.add(ldata);
        add("Center",pright);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand()=="File"){
                FileDialog fd=new FileDialog(ExcelReader.this,"Open File",FileDialog.LOAD);
                fd.setDirectory("C:\\");
                fd.setVisible(true);
                filename=fd.getFile();
                
                try{                    
                    FileInputStream inputStream = new FileInputStream(new File(filename));
                    Workbook workbook = new HSSFWorkbook(inputStream);
                    Sheet sheet = workbook.getSheetAt(0);
                    
                    int numrow=sheet.getLastRowNum();
                    int numcol=sheet.getRow(1).getLastCellNum();
                    
                    String content="<html><div align=\"center\">"+tftitle.getText()+"</div><table border=\"1\">";
                    
                    content=content+"<tr>";
                    for(int i=0;i<numcol;i++){
                        content=content+"<td align=\"center\">"+sheet.getRow(0).getCell(i).getStringCellValue()+"</td>";
                    }
                    content=content+"</tr>";
                    
                    
                    for(int i=1;i<=numrow;i++){
                        content=content+"<tr>";
                        for(int j=0;j<numcol;j++){
                            Cell cell=sheet.getRow(i).getCell(j);
                            switch (cell.getCellType()) {
                            case Cell.CELL_TYPE_STRING:
                                content=content+"<td align=\"center\">"+sheet.getRow(i).getCell(j).getStringCellValue()+"</td>";
                                break;
                            case Cell.CELL_TYPE_BOOLEAN:
                                content=content+"<td align=\"center\">"+sheet.getRow(i).getCell(j).getBooleanCellValue()+"</td>";
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                content=content+"<td align=\"center\">"+sheet.getRow(i).getCell(j).getNumericCellValue()+"</td>";
                                break;
                            }
                        }
                        content=content+"</tr>";
                    }

                    content=content+"</table></html>";
                    
                    ldata.setText(content);
                    
                    inputStream.close();
                }catch(Exception ex){
                    
                }
            }
        }
    }
    
    public static void main(String[] args){
        new ExcelReader();
    }
}

Monday, March 14, 2016

Simple Keyword Counter


This program counts how many times a particular word appears inside a text area. JApplet class is used here to display the graphical components.

 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeywordCounter extends JApplet{
    private JTextField tfkeyword=null;
    private JLabel lresult=null;
    private JTextArea tacontent=null;
    private JButton bcount=null;
    
    public void init() {
        setSize(400,500);
        setLayout(new BorderLayout());
        
        JPanel ptop=new JPanel();
        ptop.setLayout(new FlowLayout());
        tfkeyword=new JTextField();
        tfkeyword.setColumns(10);
        ptop.add("North",tfkeyword);
        lresult=new JLabel();
        ptop.add("North",lresult);
        getContentPane().add("North",ptop);
        
        tacontent=new JTextArea();
        tacontent.setLineWrap(true);
        tacontent.setColumns(20);
        tacontent.setRows(15);
        getContentPane().add("Center",tacontent);
        
        bcount=new JButton("Click Me");
        bcount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String tempkeyword=tfkeyword.getText().toLowerCase();
                String tempcontent=tacontent.getText().toLowerCase();
                int counter=0;
                int indexsearch=0;
                while(true){
                    int i=tempcontent.indexOf(tempkeyword,indexsearch);
                    if(i==-1){
                        break;
                    }
                    else{
                        counter++;
                        tempcontent=tempcontent.substring(tempcontent.indexOf(tempkeyword)+tempkeyword.length());
                        System.out.println(indexsearch);
                    }
                }
                lresult.setText(counter+"");
            }
        });
        getContentPane().add("South",bcount);
    }

    public void destroy() {

    }
}

Sunday, March 13, 2016

Table Maker With HTML

This program utilizes HTML code to create a table. Table rows can be added dynamically by clicking a button.


 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TableMaker extends JFrame{
    private JPanel pcontrol=null;
    private JTextField tftitlerow=null;
    private ArrayList<JTextField> alcontentrow=null;
    private JTextField tfcontentrow=null;
    private JButton brow=null;
    private JButton bshow=null;
    private String content=null;
    private JPanel pdraw=null;
    private GridBagConstraints gbc=null;
    
    public TableMaker(){
        super("Table Maker");
        setSize(600,300);
        setLayout(new BorderLayout());
        
        content=new String();
        content="<html><table border=\"1\">";
        
        alcontentrow=new ArrayList<JTextField>();
        
        pcontrol=new JPanel();
        pcontrol.setPreferredSize(new Dimension(200,300));
        pcontrol.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets=new Insets(5,0,0,0);
        tftitlerow=new JTextField();
        tftitlerow.setColumns(12);
        pcontrol.add(tftitlerow,gbc);
        
        brow=new JButton("Add Row");
        brow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                gbc.gridy++;
                alcontentrow.add(new JTextField());
                alcontentrow.get(alcontentrow.size()-1).setColumns(12);
                pcontrol.add(alcontentrow.get(alcontentrow.size()-1),gbc);
                pcontrol.revalidate();
            }
        });
        gbc.gridy++;
        pcontrol.add(brow,gbc);
        
        gbc.gridy++;
        bshow=new JButton("Show");
        bshow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                content=content+"<tr><td align=\"center\"><b>"+tftitlerow.getText()+"</b></td></tr>";
                for(int i=0;i<alcontentrow.size();i++){
                    content=content+"<tr><td>"+alcontentrow.get(i).getText()+"</td></tr>";
                }
                content=content+"</table></html>";
                pdraw.removeAll();
                JLabel ldraw=new JLabel();
                pdraw.add(ldraw);
                ldraw.setText(content);
            }
        });
        pcontrol.add(bshow,gbc);
        add("West",pcontrol);
        
        pdraw=new JPanel();
        pdraw.setPreferredSize(new Dimension(400,300));
        pdraw.setBackground(Color.WHITE);
        add("Center",pdraw);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    public static void main(String[] args){
        TableMaker app=new TableMaker();
    }
}

Saturday, March 12, 2016

3D Box With Different Faces

This program shows the use of the TextureLoader class to apply a different texture to each face of a 3D cube. Six two-dimensional images are used for this purpose. By clicking the "Spin" button, the dice will be rotated around x,y, and x axis. The Transform3D is modified randomly with the help of Random class.


  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
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.*;
import javax.vecmath.Vector3f;
import java.util.*;

public class Dice3D extends JFrame{
    private Box box=null;
    private BranchGroup bg=null;
    private SimpleUniverse su=null;
    private TransformGroup tg=null;
    private Transform3D t3d=null;
    private JButton bspin=null;
    
    public Dice3D(){
        super("Dice 3D");
        setSize(300,400);
        setLayout(new BorderLayout());
        
        GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas=new Canvas3D(config);
        canvas.setSize(300,300);
        add("Center",canvas);
        
        su=new SimpleUniverse(canvas);
        su.getViewingPlatform().setNominalViewingTransform();
        
        bg=new BranchGroup();
        Appearance ap=new Appearance();
        ap.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
 ap.setCapability(Appearance.ALLOW_TEXGEN_WRITE);
        box=new Box(0.5f,0.5f,0.5f,Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS,ap);
        box.setCapability(Box.ENABLE_APPEARANCE_MODIFY);
        box.setCapability(Box.GEOMETRY_NOT_SHARED);
        
        TextureLoader loader = new TextureLoader("c://Users/MARIO/one.png","RGB",new Container());
        Texture texture = loader.getTexture();
        ap.setTexture(texture);
        box.getShape(Box.FRONT).setAppearance(ap);
        
        Appearance ap2=new Appearance();
        TextureLoader loader2 = new TextureLoader("c://Users/MARIO/two.png","RGB",new Container());
        Texture texture2 = loader2.getTexture();
        ap2.setTexture(texture2);
        box.getShape(Box.BACK).setAppearance(ap2);
        
        Appearance ap3=new Appearance();
        TextureLoader loader3 = new TextureLoader("c://Users/MARIO/three.png","RGB",new Container());
        Texture texture3 = loader3.getTexture();
        ap3.setTexture(texture3);
        box.getShape(Box.LEFT).setAppearance(ap3);
        
        Appearance ap4=new Appearance();
        TextureLoader loader4 = new TextureLoader("c://Users/MARIO/four.png","RGB",new Container());
        Texture texture4 = loader4.getTexture();
        ap4.setTexture(texture4);
        box.getShape(Box.RIGHT).setAppearance(ap4);
        
        Appearance ap5=new Appearance();
        TextureLoader loader5 = new TextureLoader("c://Users/MARIO/five.png","RGB",new Container());
        Texture texture5 = loader5.getTexture();
        ap5.setTexture(texture5);
        box.getShape(Box.TOP).setAppearance(ap5);
        
        Appearance ap6=new Appearance();
        TextureLoader loader6 = new TextureLoader("c://Users/MARIO/six.png","RGB",new Container());
        Texture texture6 = loader6.getTexture();
        ap6.setTexture(texture6);
        box.getShape(Box.BOTTOM).setAppearance(ap6);
        
        tg = new TransformGroup();
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        t3d = new Transform3D();
        Vector3f v3f = new Vector3f(0.0f,0.0f,0.0f);
        t3d.setTranslation(v3f);
        
        tg.setTransform(t3d);
        tg.addChild(box);
        bg.addChild(tg);
        su.addBranchGraph(bg);
        
        bspin=new JButton("Spin");
        bspin.addActionListener(new ButtonHandler());
        add("South",bspin);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            Random rand=new Random();
            int temp=rand.nextInt(5)+1;
            Transform3D rot = new Transform3D();
            rot.rotX(Math.PI/temp);
            t3d.mul(rot);
            rot.rotY(Math.PI/temp);
            t3d.mul(rot);
            rot.rotZ(Math.PI/temp);        
            t3d.mul(rot);
            tg.setTransform(t3d);
        }
    }
    
    public static void main(String[] args){
        new Dice3D();
    }
}

Friday, March 11, 2016

Text To Image 3D


This simple program shows how to apply a texture Image to a rotated 3D box. The user can put a title into the canvas. A list that allows the user to select one of the available fonts on his/her local system is also provided. The output can be saved to disk as a PNG file.

  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
160
161
162
163
164
165
166
167
168
169
170
171
172
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Font3D;
import javax.media.j3d.FontExtrusion;
import javax.media.j3d.GraphicsContext3D;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.Raster;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Text3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

public class TextToImage3D extends JFrame{
    private JPanel panel=null;
    private JTextField tfTitle=null;
    private JList lFont=null;
    private JScrollPane splFont=null;
    private JButton bLoad=null;
    private JButton bShow=null;
    private JButton bSave=null;
    private BranchGroup bg=null;
    private SimpleUniverse su=null;    
    private Canvas3D canvas=null;
    private Box box=null;
    
    public TextToImage3D(){
        super("Text To Image 3D");
        setSize(700,550);
        setLayout(new BorderLayout());
        
        panel=new JPanel();
        panel.setPreferredSize(new Dimension(200,550));
        panel.setLayout(new FlowLayout());
        tfTitle=new JTextField();
        tfTitle.setColumns(16);
        panel.add(tfTitle);
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        String []fontFamilies=ge.getAvailableFontFamilyNames();
        lFont=new JList(fontFamilies);
        splFont=new JScrollPane(lFont);
        panel.add(splFont);
        bLoad=new JButton("Load");
        bLoad.addActionListener(new ButtonHandler());
        panel.add(bLoad);
        bShow=new JButton("Show");
        bShow.addActionListener(new ButtonHandler());
        panel.add(bShow);
        bSave=new JButton("Save");
        bSave.addActionListener(new ButtonHandler());
        panel.add(bSave);
        add("West",panel);
        
        GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
        canvas=new Canvas3D(config);
        canvas.setSize(500,550);
        add("Center",canvas);
        
        su=new SimpleUniverse(canvas);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand()=="Load"){
                su.getViewingPlatform().setNominalViewingTransform();
                
                bg=new BranchGroup();
                
                FileDialog fd=new FileDialog(TextToImage3D.this,"Open Image File",FileDialog.LOAD);
                fd.setDirectory("C:\\");
                fd.setVisible(true);
                String filename=fd.getDirectory()+fd.getFile();
                TextureLoader loader = new TextureLoader(filename,"RGB", new Container());
                Texture texture = loader.getTexture();
            
                Appearance ap = new Appearance();
                ap.setTexture(texture);

                box=new Box(0.5f,0.5f,0.5f,Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS,ap);
                
                TransformGroup tg = new TransformGroup();
  Transform3D t3d = new Transform3D();
                Vector3f v3f = new Vector3f(0.0f,-0.2f,0.0f);
  t3d.setTranslation(v3f);
                Transform3D rot = new Transform3D();
                rot.rotX(Math.PI / 5);
                rot.rotY(Math.PI / 5);
  t3d.mul(rot);
                
                tg.setTransform(t3d);
                tg.addChild(box);
                bg.addChild(tg);
                su.addBranchGraph(bg);
            }
            if(e.getActionCommand()=="Show"){
                su.getViewingPlatform().setNominalViewingTransform();
                
                bg=new BranchGroup();
                
                String temp=tfTitle.getText();
                String font=(String)lFont.getSelectedValue();
                
                Font3D f3d=new Font3D(new Font(font,Font.PLAIN,1),new FontExtrusion());
                Text3D tx3d=new Text3D(f3d,temp,new Point3f(0.0f,1.0f,-1.9f));
                tx3d.setAlignment(Text3D.ALIGN_CENTER);
                
                Color3f white = new Color3f(1.0f,1.0f,1.0f);
  Color3f blue = new Color3f(0.2f,0.2f,1f);
  Appearance a = new Appearance();
                a.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
  a.setCapability(Appearance.ALLOW_TEXGEN_WRITE);
  Material m = new Material(blue,blue,blue,white,60.0f);
  m.setLightingEnable(true);
  a.setMaterial(m);
                
                Shape3D sh=new Shape3D();
                sh.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
                sh.setGeometry(tx3d);
                sh.setAppearance(a);
                
                bg.addChild(sh);
                su.addBranchGraph(bg);
            }
            if(e.getActionCommand()=="Save"){
                GraphicsContext3D  ctx=canvas.getGraphicsContext3D();
                int w=canvas.getWidth();
                int h=canvas.getHeight();
        
                BufferedImage bi=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
                ImageComponent2D im=new ImageComponent2D(ImageComponent.FORMAT_RGB,bi);

                Raster ras = new Raster(new Point3f(-1.0f,-1.0f,-1.0f ),Raster.RASTER_COLOR,0,0,w,h,im,null );
        
                ctx.flush(true);
                ctx.readRaster(ras);
        
                BufferedImage bufImage = new BufferedImage(w,h, BufferedImage.TYPE_INT_ARGB);
                bufImage=ras.getImage().getImage();
        
                try {
                    ImageIO.write(bufImage,"png",new File("3dresult.png") );
                } catch (IOException ioe) { 
                    ioe.printStackTrace(); 
                }
            }
        }
    }
    
    public static void main(String[] args){
        TextToImage3D ap=new TextToImage3D();
    }
}