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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import java.util.*; public class TreeDiagramMaker extends JFrame{ private JPanel pleft=null; private JPanel pright=null; private JButton bnode=null; private JButton bline=null; private JButton bsave=null; private int counter=0; private int curindex=0; private ArrayList<Rectangle> nodes=null; private int mousex=0,mousey=0; private boolean pressed=false; private ArrayList<Integer> parent=null; private ArrayList<Integer> children=null; //private ArrayList<Point> lines=null; //private ArrayList<Point> lines2=null; private int counterp=0; int rect1=-1,rect2=-1; public TreeDiagramMaker(){ super("Tree Diagram Maker"); setSize(900,700); setLayout(new BorderLayout()); pleft=new JPanel(); bnode=new JButton(); BufferedImage bufimage=null; try{ bufimage=ImageIO.read(new File("node.png")); }catch(Exception e){ } bnode.setIcon(new ImageIcon(bufimage)); bnode.addActionListener(new ButtonHandler()); pleft.add(bnode); pright=new DrawDiagram(); pright.setPreferredSize(new Dimension(600,600)); pright.addMouseListener(new MouseHandler()); pright.addMouseMotionListener(new MouseHandler()); add(BorderLayout.WEST,pleft); add(BorderLayout.EAST,pright); nodes=new ArrayList<Rectangle>(); parent=new ArrayList<Integer>(); children=new ArrayList<Integer>(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class DrawDiagram extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2=(Graphics2D)g; if(!nodes.isEmpty()){ for(int i=0;i<nodes.size();i++){ g2.draw(nodes.get(i)); } } if(pressed){ g2.draw(nodes.get(curindex)); } if(!parent.isEmpty()&&!children.isEmpty()){ for(int i=0;i<parent.size();i++){ int centerx1=(int)(nodes.get(parent.get(i)).getMinX())+(int)(nodes.get(parent.get(i)).getMaxX()-nodes.get(parent.get(i)).getMinX())/2; int centery1=(int)(nodes.get(parent.get(i)).getMaxY()); int centerx2=(int)(nodes.get(children.get(i)).getMinX())+(int)(nodes.get(children.get(i)).getMaxX()-nodes.get(children.get(i)).getMinX())/2; int centery2=(int)(nodes.get(children.get(i)).getMinY()); g2.drawLine(centerx1,centery1,centerx2,centery2); } } } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==bnode){ nodes.add(counter,new Rectangle(0,0,100,80)); curindex=counter; counter++; repaint(); } } } class MouseHandler implements MouseMotionListener, MouseListener{ public void mouseMoved(MouseEvent e){ } public void mouseDragged(MouseEvent e){ if(pressed){ mousex=e.getX(); mousey=e.getY(); nodes.get(curindex).setLocation(mousex,mousey); repaint(); } } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mouseReleased(MouseEvent e){ pressed=false; } public void mousePressed(MouseEvent e){ for(int i=0;i<nodes.size();i++){ if(nodes.get(i).contains(e.getX(),e.getY())){ mousex=e.getX(); mousey=e.getY(); curindex=i; pressed=true; break; } } } public void mouseClicked(MouseEvent e){ if(e.getClickCount()==2){ rect1=-1; rect2=-1; for(int i=0;i<nodes.size();i++){ if(nodes.get(i).contains(e.getX(),e.getY())){ nodes.remove(i); counter--; curindex=0; for(int j=0;j<parent.size();j++){ if(parent.get(j)==i){ parent.remove(j); children.remove(j); counterp--; } } for(int j=0;j<children.size();j++){ if(children.get(j)==i){ children.remove(j); parent.remove(j); counterp--; } } repaint(); break; } } } if(e.getClickCount()==1){ for(int i=0;i<nodes.size();i++){ if(nodes.get(i).contains(e.getX(),e.getY())){ if(rect1==-1){ rect1=i; break; } if(rect1!=-1){ rect2=i; if(rect2!=rect1){ parent.add(counterp,rect1); children.add(counterp,rect2); counterp++; rect1=-1; rect2=-1; repaint(); break; } if(rect2==rect1){ rect1=-1; rect2=-1; break; } } } } } } } public static void main(String[] args){ TreeDiagramMaker ap=new TreeDiagramMaker(); } } |
Monday, February 29, 2016
Tree Diagram Maker
The main purpose of this short program is to demonstrate mouse events: mouse clicks, mouse presses, mouse drags, and mouse releases. Utilizing mouseDragged method, for instance, the nodes can be dragged around a panel. Double clicking a node will erase it. To connect two nodes, click both of them one by one.
Saturday, February 27, 2016
Simple Typing Game
This program shows the use of DocumentListener to listen for changes in a textfield. Three different words will appear on the screen and user needs to type them all one by one. When a correct word is typed, the corresponding word will dissappear and a variable named "score" will be added by 1. The variable "displayed" is used to count the number of words that have been displayed on the screen.
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 | import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import java.util.*; public class TypingTutor extends JFrame{ JButton b=null; JPanel p=null; JTextField tf=null; JLabel l=null; JLabel l2=null; int score=0; int displayed=0; ArrayList<String> wordlist; WordRun word1=null; WordRun word2=null; WordRun word3=null; public TypingTutor(){ super("Typing Tutor"); setSize(400,400); setLayout(new BorderLayout()); b=new JButton("Start"); b.addActionListener(new ButtonHandler()); add(b,BorderLayout.NORTH); p=new DrawPanel(); p.setPreferredSize(new Dimension(300,300)); add(p,BorderLayout.CENTER); JPanel p2=new JPanel(); p2.setLayout(new FlowLayout()); tf=new JTextField(10); tf.getDocument().addDocumentListener(new ListenText()); p2.add(tf); l=new JLabel("0"); p2.add(l); l2=new JLabel("0"); p2.add(l2); add(p2,BorderLayout.SOUTH); wordlist=new ArrayList<String>(); wordlist.add(0,"abandon"); wordlist.add(1,"abate"); wordlist.add(2,"abbreviate"); wordlist.add(3,"abdicate"); wordlist.add(4,"aberration"); wordlist.add(5,"abhorrence"); wordlist.add(6,"ability"); wordlist.add(7,"ablaze"); wordlist.add(8,"abnormal"); wordlist.add(9,"aboard"); word1=new WordRun(); word2=new WordRun(); word3=new WordRun(); word1.st=null; word2.st=null; word3.st=null; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class DrawPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2=(Graphics2D)g; Font f=new Font("Monospaced",Font.BOLD,14); FontMetrics fm=this.getFontMetrics(f); g2.setColor(Color.BLACK); g2.setFont(f); if(word1.st!=null){ g2.drawString(word1.st,word1.posx,word1.posy); } if(word2.st!=null){ g2.drawString(word2.st,word2.posx,word2.posy); } if(word3.st!=null){ g2.drawString(word3.st,word3.posx,word3.posy); } } } class WordRun implements Runnable{ String st=null; int posx; int posy; public void run(){ try{ Random rand=null; while(true){ if((st==null)||(posy==p.getHeight())){ rand=new Random(); Thread.currentThread().sleep(rand.nextInt(1000)); st=wordlist.get(rand.nextInt(10)); posy=0; do{ posx=rand.nextInt(p.getWidth()); }while(posx>(p.getWidth()-100)); tf.setText(""); displayed++; l2.setText(displayed+""); } else{ Thread.currentThread().sleep(20); posy++; } repaint(); } }catch(Exception e){ } } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="Start"){ Thread t1=new Thread(word1); Thread t2=new Thread(word2); Thread t3=new Thread(word3); t1.start(); t2.start(); t3.start(); } } } class ListenText implements DocumentListener{ public void changedUpdate(DocumentEvent e) { } public void removeUpdate(DocumentEvent e) { } public void insertUpdate(DocumentEvent e) { if(tf.getText().equals(word1.st)){ word1.st=null; word1.posy=0; score++; l.setText(score+""); } if(tf.getText().equals(word2.st)){ word2.st=null; word2.posy=0; score++; l.setText(score+""); } if(tf.getText().equals(word3.st)){ word3.st=null; word3.posy=0; score++; l.setText(score+""); } } } public static void main(String[] args){ TypingTutor ap=new TypingTutor(); } } |
Thursday, February 25, 2016
Word Search Puzzle Maker
This program creates word search puzzle. The result can be saved as an image file. The number of characters displayed is 14x14=196. 10 textfields are used to input words that will be searched.
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import java.util.*; import java.io.*; import javax.imageio.ImageIO; public class WordSearchMaker extends JFrame{ JPanel p1=null; JPanel p2=null; JTextField tf1=null; JTextField tf2=null; JTextField tf3=null; JTextField tf4=null; JTextField tf5=null; JTextField tf6=null; JTextField tf7=null; JTextField tf8=null; JTextField tf9=null; JTextField tf10=null; JButton bcreate=null; JButton bsave=null; public WordSearchMaker(){ super("Word Search Maker"); setSize(1000,800); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx=0; gbc.gridy=0; gbc.insets=new Insets(5,10,0,0); p1=new JPanel(); p1.setLayout(new GridBagLayout()); gbc.gridy++; tf1=new JTextField(20); p1.add(tf1,gbc); gbc.gridy++; tf2=new JTextField(20); p1.add(tf2,gbc); gbc.gridy++; tf3=new JTextField(20); p1.add(tf3,gbc); gbc.gridy++; tf4=new JTextField(20); p1.add(tf4,gbc); gbc.gridy++; tf5=new JTextField(20); p1.add(tf5,gbc); gbc.gridy++; tf6=new JTextField(20); p1.add(tf6,gbc); gbc.gridy++; tf7=new JTextField(20); p1.add(tf7,gbc); gbc.gridy++; tf8=new JTextField(20); p1.add(tf8,gbc); gbc.gridy++; tf9=new JTextField(20); p1.add(tf9,gbc); gbc.gridy++; tf10=new JTextField(20); p1.add(tf10,gbc); gbc.gridy++; bcreate=new JButton("Create"); bcreate.addActionListener(new ButtonHandler()); p1.add(bcreate,gbc); gbc.gridy++; bsave=new JButton("Save"); bsave.addActionListener(new ButtonHandler()); p1.add(bsave,gbc); add("West",p1); p2=new DrawPanel(); p2.setPreferredSize(new Dimension(700,700)); p2.setBackground(Color.BLACK); add("East",p2); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class DrawPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2=(Graphics2D)g; g2.setBackground(Color.LIGHT_GRAY); g2.clearRect(0,0,this.getWidth(),this.getHeight()); Font f=new Font("Monospaced",Font.BOLD,14); FontMetrics fm=p2.getFontMetrics(f); g2.setColor(Color.BLACK); g2.setFont(f); String words[]=new String[10]; words[0]=tf1.getText(); words[1]=tf2.getText(); words[2]=tf3.getText(); words[3]=tf4.getText(); words[4]=tf5.getText(); words[5]=tf6.getText(); words[6]=tf7.getText(); words[7]=tf8.getText(); words[8]=tf9.getText(); words[9]=tf10.getText(); for(int i=0;i<words.length;i++){ if(words[i].length()>14){ return; } } String alphabet="abcdefghijklmnopqrstuvwxyz"; Character box[][]=new Character[14][14]; for(int i=0;i<14;i++){ for(int j=0;j<14;j++){ box[i][j]=null; } } Random rand=new Random(); int counter=0; do{ String st=words[counter]; int stlength=st.length(); int dir=rand.nextInt(4); boolean mark=false; //horizontal if(dir==0){ int x=rand.nextInt(14-stlength); int y=rand.nextInt(14); for(int i=0;i<stlength;i++){ if(box[x+i][y]==null||box[x+i][y]==st.charAt(i)){ mark=true; } else{ mark=false; counter--; break; } } if(mark==true){ for(int i=0;i<stlength;i++){ box[x+i][y]=st.charAt(i); } } } //vertical if(dir==1){ int x=rand.nextInt(14); int y=rand.nextInt(14-stlength); for(int i=0;i<stlength;i++){ if(box[x][y+i]==null||box[x][y+i]==st.charAt(i)){ mark=true; } else{ mark=false; counter--; break; } } if(mark==true){ for(int i=0;i<stlength;i++){ box[x][y+i]=st.charAt(i); } } } //diagonal if(dir==2){ int x=rand.nextInt(14-stlength); int y=rand.nextInt(14-stlength); for(int i=0;i<stlength;i++){ if(box[x+i][y+i]==null||box[x+i][y+i]==st.charAt(i)){ mark=true; } else{ mark=false; counter--; break; } } if(mark==true){ for(int i=0;i<stlength;i++){ box[x+i][y+i]=st.charAt(i); } } } if(dir==3){ int x=rand.nextInt(14-stlength); int y=rand.nextInt(14-stlength)+stlength; for(int i=0;i<stlength;i++){ if(box[x+i][y-i]==null||box[x+i][y-i]==st.charAt(i)){ mark=true; } else{ mark=false; counter--; break; } } if(mark==true){ for(int i=0;i<stlength;i++){ box[x+i][y-i]=st.charAt(i); } } } counter++; }while(counter<10); for(int i=0;i<14;i++){ for(int j=0;j<14;j++){ if(box[i][j]==null){ box[i][j]=alphabet.charAt(rand.nextInt(26)); } } } for(int i=0;i<14;i++){ for(int j=0;j<14;j++){ g2.drawString(box[i][j]+"",50+(30*i),50+(30*j)); } } counter=0; for(int i=0;i<2;i++){ for(int j=0;j<5;j++){ g2.drawString(words[counter],50+(100*i),500+(30*j)); counter++; } } } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="Create"){ repaint(); } if(e.getActionCommand()=="Save"){ BufferedImage bufimage = new BufferedImage(p2.getWidth(),p2.getHeight(),BufferedImage.TYPE_INT_ARGB); Graphics2D g=(Graphics2D)bufimage.getGraphics(); p2.paint(g); try { ImageIO.write(bufimage,"png", new File("wordsearchpuzzle.png")); } catch (IOException ex) { ex.printStackTrace(); } } } } public static void main(String[] args){ WordSearchMaker ap=new WordSearchMaker(); } } |
Subscribe to:
Posts (Atom)