Posted by main class Wed 7th Mar 2007 22:58 - Syntax is None/text - 52 views
Download | New Post | Modify | Hide line numbers
Description:
This is an adaptation of your source code - the grid and coordinates really
help

  1. package ftw;
  2. import
  3. java.awt.*;
  4. import
  5. java.awt.event.*;
  6. import
  7. java.io.*;
  8. import
  9. javax.imageio.*;
  10. import
  11. javax.swing.*;
  12.  
  13. //public class ImageTest {
  14. public class ftw2 {
  15.     public static void main(String[] args) {
  16.         ImageFrame frame = new ImageFrame();
  17.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         frame.setVisible(true);
  19.     }
  20. }
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. class QueryExit extends JPanel {
  30. //To be implemented later... I'm going to create a custom dialog that displays one of
  31. //the exit catch-phrases that Wolfenstein 3D uses. Here they are. I'm extending JPanel
  32. //because I don't like JDialog it is cranky.
  33. /*
  34. {"Dost thou wish to\nleave with such hasty\nabandon?"},
  35. {"Chickening out...\nalready?"},
  36. {"Press N for more carnage.\nPress Y to be a weenie."},
  37. {"So, you think you can\nquit this easily, huh?"},
  38. {"Press N to save the world.\nPress Y to abandon it in\nits hour of need."},
  39. {"Press N if you are brave.\nPress Y to cower in shame."},
  40. {"Heroes, press N.\nWimps, press Y."},
  41. {"You are at an intersection.\nA sign says, 'Press Y to quit.'\n>"},
  42. {"For guns and glory, press N.\nFor work and worry, press Y."}
  43. */
  44. }
  45. /**
  46. A frame with an image panel
  47. */
  48. class ImageFrame extends JFrame /*implements MouseListener*/ {
  49. public ImageFrame() {
  50. //setUndecorated(true);
  51. //set panel size
  52. // get the GraphicsDevice
  53. GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
  54. GraphicsDevice device = environment.getDefaultScreenDevice();
  55. //device.
  56. //setFullScreenWindow(this);
  57. setSize(800,600);
  58. // add panel to frame
  59. ImagePanel panel = new ImagePanel();
  60. add(panel);
  61. /* I realized that the following code was made obsolete by the setFullScreenWindow method
  62. //Get size of screen
  63. Toolkit kit = Toolkit.getDefaultToolkit();
  64. Dimension screenSize = kit.getScreenSize();
  65. int screenWidth = screenSize.width;
  66. int screenHeight = screenSize.height;
  67. //setSize(screenWidth, screenHeight);
  68. */
  69. }
  70. public void setUndecorated(boolean b) {
  71. super.setUndecorated(b);
  72. }
  73. //this was going to be used to enable the user to resize the window and to move it
  74. //around as java handles all that for the programmer when the window is decorated.
  75. //Unfortunately, I ran into some scope issues, so it isn't used for now.
  76. //To implement it, I would code it so that when the user hit a button, such as esc,
  77. //the window would change to either decorated or undecorated.
  78. //The game would pause when the window is decorated.
  79. //So if the user wanted to decorate the window so he could move it around,
  80. //setUndecorated(true) would be called. And if he wanted to start playing again,
  81. //setUndecorated(false) would be called.
  82. }
  83. //end class ImageFrame
  84.  
  85. /**
  86. A panel that displays an image, draws a grid for every increment of 50 in the x and y axes,
  87. and implements tooltips for the mouse so that the user can see where he is in the panel
  88. */
  89. class ImagePanel extends JPanel implements MouseMotionListener {
  90.    
  91. private Image image;
  92. private Point tipPoint = null;
  93. private Label tipLabel = new TipLabel();
  94. private SelectionListener selectionListener;
  95. public Polygon hex = new Polygon();
  96. Hexagon hex_temp2 = new Hexagon(1, 1, 1,1);
  97.  
  98. public ImagePanel() {
  99.     hex.addPoint(31,0);
  100.     hex.addPoint(0,19);
  101.     hex.addPoint(0,55);
  102.     hex.addPoint(31,73);
  103.     hex.addPoint(63,55);
  104.     hex.addPoint(63,19);
  105.     //Hexagon hex = new Hexagon(1,1,10,10);
  106.    
  107.     // get the image
  108.     try {
  109.         image = ImageIO.read(new File("C:/Users/Jon/Pictures/konq_back.jpg"));
  110.     }
  111.     catch (IOException e) {
  112.         e.printStackTrace();
  113.     }
  114.     this.add(tipLabel);
  115.     tipLabel.setVisible(false);
  116.     //mouseClicked is not called if the mouse moves between the mouse button being pressed
  117.     //and released. Hence the the press and release separately are handled separately.
  118.    
  119.     this.addMouseListener(new MouseAdapter() {
  120.         public void mousePressed(MouseEvent e) {
  121.             mouseClick(e, true);
  122.     }
  123.     public void mouseReleased(MouseEvent e) {
  124.         mouseClick(e, false);
  125.     }
  126.     });
  127.     // Track mouse motion for tooltips
  128.     this.addMouseMotionListener(new MouseMotionAdapter() {
  129.         public void mouseMoved(MouseEvent e) {
  130.             ImagePanel.this.mouseMoved(e);
  131.         }
  132.     });
  133.  
  134.  
  135.     }
  136.     //end constructor
  137.     /** Register a selectionListener.
  138.     *
  139.     * For simplicity, only one listener at a time is supported.
  140.     *
  141.     * See end of file for the SelectionListener interface
  142.     */
  143.     public void setSelectionListener(SelectionListener s) {
  144.         selectionListener = s;
  145.     }
  146.     private Point clickedPoint = null;
  147.     /** Respond to a mouse click by selecting the point... This isn't a good way of
  148.     * articulating what I'm thinking; but after running the program a few times
  149.     * you'll see why it works.
  150.     *
  151.     * ATTENTION!!!! PARTS OF THIS METHOD MIGHT BE CAUSING THE FLICKER PROBLEMS
  152.     * THAT ARE HAPPENING!!!
  153.     * */
  154.     public void mouseClick(MouseEvent e, boolean down) {
  155.         Point pt = e.getPoint();
  156.         if (down) {
  157.         // The mouse button has gone down: record where we are
  158.             clickedPoint = pt;
  159.             if (hex_temp2.contains(pt)) {
  160.                 System.out.println("Clicked in region of hexagon");
  161.             } else {
  162.                 System.out.println("did not click in region");
  163.             }
  164.         }
  165.         else {
  166.         // The mouse button has gone up: perform the action if we are still over
  167.         // the same town
  168.         if (selectionListener != null && pt != null && pt == clickedPoint)
  169.             selectionListener.select(pt);
  170.             clickedPoint = null;
  171.         }
  172.     }
  173.     public Image getImage() {
  174.         return image;
  175.     }
  176.     // Track mouse movement in order to show tooltips
  177.     public void mouseMoved(MouseEvent e) {
  178.     Point z = e.getPoint();
  179.     tipPoint = z;
  180.     tipLabel.setText(z.toString());
  181.     tipLabel.setSize(tipLabel.getPreferredSize());
  182.     tipLabel.setLocation(z.x+8, z.y+8);
  183.     tipLabel.setVisible(true);
  184.     //I originally thought that I would need the original lines of code, but they
  185.     //cause lots of problems. SO DO NOT THINK THAT IMPLEMENTING THEM WILL
  186.     //IMPROVE ANYTHING
  187.     /*
  188.     //repaint();
  189.     if (tipPoint != null) {
  190.     //Tool tip showing: remove if not at that point anymore
  191.     tipPoint = null;
  192.     tipLabel.setVisible(false);
  193.     return;
  194.     }*/
  195.     }
  196.     public void paintComponent(Graphics g) {
  197.         super.paintComponent(g);
  198.    
  199.         Graphics2D g2 = (Graphics2D) g;
  200.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  201.                    RenderingHints.VALUE_ANTIALIAS_ON);
  202.        
  203.         if (image == null) return;
  204.         // draw the image in the upper-left corner
  205.         Toolkit kit = Toolkit.getDefaultToolkit();
  206.         Dimension screenSize = kit.getScreenSize();
  207.         int screenWidth = screenSize.width;
  208.         int screenHeight = screenSize.height;
  209.        
  210.         g.drawImage(image, 0,0, null);
  211.    
  212.         for(int y = 0; y < screenHeight; y+=50) {
  213.             g.drawLine(0, y, screenWidth, y);
  214.         }
  215.            
  216.    
  217.         for(int x = 0; x < screenWidth; x+= 50) {
  218.             g.drawLine(x, 0, x, screenHeight);
  219.         }
  220.        
  221.         int hex_x[] = { 31 ,0  ,0  ,31 ,63, 63 };
  222.         int hex_y[] = { 0,19,55,73,55,19 };
  223.         g.setColor(Color.green);
  224.        
  225.            
  226.             //g.translate((800/2)-30,(600/2)-40);
  227.             //hex_temp2.move(800/2, 600/2);
  228.             g.fillPolygon(hex_temp2);
  229.             hex_temp2.move(800/2+30,600/2+30);
  230.             repaint();
  231.            
  232.             Hexagon nhex = new Hexagon(1,1,1,1);
  233.             //g.clipRect(0, 0, 100, 100);
  234.  
  235.        
  236.        
  237.        
  238.        
  239.        
  240.        
  241.        
  242.        
  243.         //g.drawRect(575, 500, 150, 85);
  244.         //displayToolTipText();
  245.         }
  246.         //end paintComponents
  247.         //Deprecated, but kept for ideas.
  248.         /*
  249.         public void displayToolTipText() {
  250.         Point currentcoords = new Point(600,600 /*e.getPoint());
  251.         //Toolkit kit = Toolkit.getDefaultToolkit();
  252.         (ToolTipManager.sharedInstance()).registerComponent(this);
  253.         JToolTip tooltip = this.createToolTip();
  254.         tooltip.setToolTipText(currentcoords.toString());
  255.         }*/
  256.         //implement the MouseMotionListener interface method mouseDragged
  257.    
  258.         public void mouseDragged(MouseEvent e) {
  259.         //do nothing
  260.         }
  261.     }
  262.  
  263.     //Interface for classes that can listen for clicks on point
  264.     interface SelectionListener {
  265.         void select(Point pt);
  266.     }
  267.     /**
  268.     Like a Label, but with a preferred size barely big
  269.     * enough for the text.
  270.     */
  271.     class TipLabel extends Label {
  272.         Font tipFont = new Font("Segoe UI", Font.PLAIN, 10);
  273.     public TipLabel() {
  274.         setBackground(new Color(255, 255, 255));
  275.         setFont(tipFont);
  276.     }
  277.     /** Calculate the mimimum size */
  278.     public Dimension getMinimumSize() {
  279.     FontMetrics fm = getFontMetrics(tipFont);
  280.     String label = getText();
  281.     if (label == null) label = ""; // Add 4 pixels to the width for safety
  282.     return new Dimension(fm.stringWidth(label)+20, fm.getHeight());
  283.     }
  284.     /** Calculate the preferred size. */
  285.     public Dimension getPreferredSize() {
  286.     return getMinimumSize();
  287.     }
  288. }
  289.  
  290.  
  291.  
  292.  

PermaLink to this entry https://pastebin.co.uk/11541
Posted by main class Wed 7th Mar 2007 22:58 - Syntax is None/text - 52 views
Download | New Post | Modify | Hide line numbers

 

Comments: 0