Advertisement
rainman002

Tool for word cube puzzle

Jan 4th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.97 KB | None | 0 0
  1. package gui;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Point;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseMotionListener;
  9. import java.util.HashMap;
  10.  
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13.  
  14. public class WordCubes
  15. {
  16.     static Point select;
  17.    
  18.     public static void main(String[] args)
  19.     {
  20.         final String[] words = "BUNK CAVE CRUX DOZE FIST GLAD HARM PLUM TIRE TUCK WOLF ZANY".split(" ");
  21.         final HashMap<Character,Point> tokens = new HashMap<Character,Point>();
  22.         for(String s : words)
  23.             for(Character l : s.toCharArray())
  24.                 tokens.put(l, new Point((int)(100+600*Math.random()),(int)(100+400*Math.random())));
  25.         final JFrame f = new JFrame("word cubes");
  26.         final JPanel p = new JPanel() {
  27.             public void paintComponent(Graphics g) {
  28.                 g.setColor(new Color(0));
  29.                 g.fillRect(0,0,getWidth(),getHeight());
  30.                 g.setColor(new Color(0x000033));
  31.                 if(select != null)
  32.                     g.fillOval(select.x-20, select.y-20, 40, 40);
  33.                 g.setColor(new Color(0xffffff));
  34.                 g.setFont(new Font("monospace",Font.BOLD,20));
  35.                 for(Character l : tokens.keySet())
  36.                     g.drawString(l.toString(), tokens.get(l).x-5, tokens.get(l).y+5);
  37.                 g.setColor(new Color(0x006600));
  38.                 for(String s : words)
  39.                     for(int i=0 ; i<s.length() ; i++)
  40.                         for(int j=i+1 ; j<s.length() ; j++)
  41.                             g.drawLine(tokens.get(s.charAt(i)).x,tokens.get(s.charAt(i)).y,tokens.get(s.charAt(j)).x,tokens.get(s.charAt(j)).y);
  42.             }
  43.         };
  44.         p.addMouseMotionListener(new MouseMotionListener() {
  45.             public void mouseMoved(MouseEvent e) {
  46.                 select = null;
  47.                 for(Point p : tokens.values())
  48.                     if(p.distanceSq(e.getPoint()) < 200)
  49.                         select = p;
  50.                 p.repaint();
  51.             }
  52.             public void mouseDragged(MouseEvent e) {
  53.                 if(select != null)
  54.                     select.setLocation(e.getPoint());
  55.                 p.repaint();
  56.             }
  57.         });
  58.         f.setContentPane(p);
  59.         f.setVisible(true);
  60.         f.setSize(800,600);
  61.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement