Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gui;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.util.HashMap;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class WordCubes
- {
- static Point select;
- public static void main(String[] args)
- {
- final String[] words = "BUNK CAVE CRUX DOZE FIST GLAD HARM PLUM TIRE TUCK WOLF ZANY".split(" ");
- final HashMap<Character,Point> tokens = new HashMap<Character,Point>();
- for(String s : words)
- for(Character l : s.toCharArray())
- tokens.put(l, new Point((int)(100+600*Math.random()),(int)(100+400*Math.random())));
- final JFrame f = new JFrame("word cubes");
- final JPanel p = new JPanel() {
- public void paintComponent(Graphics g) {
- g.setColor(new Color(0));
- g.fillRect(0,0,getWidth(),getHeight());
- g.setColor(new Color(0x000033));
- if(select != null)
- g.fillOval(select.x-20, select.y-20, 40, 40);
- g.setColor(new Color(0xffffff));
- g.setFont(new Font("monospace",Font.BOLD,20));
- for(Character l : tokens.keySet())
- g.drawString(l.toString(), tokens.get(l).x-5, tokens.get(l).y+5);
- g.setColor(new Color(0x006600));
- for(String s : words)
- for(int i=0 ; i<s.length() ; i++)
- for(int j=i+1 ; j<s.length() ; j++)
- 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);
- }
- };
- p.addMouseMotionListener(new MouseMotionListener() {
- public void mouseMoved(MouseEvent e) {
- select = null;
- for(Point p : tokens.values())
- if(p.distanceSq(e.getPoint()) < 200)
- select = p;
- p.repaint();
- }
- public void mouseDragged(MouseEvent e) {
- if(select != null)
- select.setLocation(e.getPoint());
- p.repaint();
- }
- });
- f.setContentPane(p);
- f.setVisible(true);
- f.setSize(800,600);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement