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

package ftw;
import
java.awt.*;
import
java.awt.event.*;
import
java.io.*;
import
javax.imageio.*;
import
javax.swing.*;

//public class ImageTest {
public class ftw2 {
    public static void main(String[] args) {
        ImageFrame frame = new ImageFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}








class QueryExit extends JPanel {
//To be implemented later... I'm going to create a custom dialog that displays one of
//the exit catch-phrases that Wolfenstein 3D uses. Here they are. I'm extending JPanel
//because I don't like JDialog it is cranky.
/*
{"Dost thou wish to\nleave with such hasty\nabandon?"},
{"Chickening out...\nalready?"},
{"Press N for more carnage.\nPress Y to be a weenie."},
{"So, you think you can\nquit this easily, huh?"},
{"Press N to save the world.\nPress Y to abandon it in\nits hour of need."},
{"Press N if you are brave.\nPress Y to cower in shame."},
{"Heroes, press N.\nWimps, press Y."},
{"You are at an intersection.\nA sign says, 'Press Y to quit.'\n>"},
{"For guns and glory, press N.\nFor work and worry, press Y."}
*/
}
/**
A frame with an image panel
*/
class ImageFrame extends JFrame /*implements MouseListener*/ {
public ImageFrame() {
//setUndecorated(true);
//set panel size
// get the GraphicsDevice
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = environment.getDefaultScreenDevice();
//device.
//setFullScreenWindow(this);
setSize(800,600);
// add panel to frame
ImagePanel panel = new ImagePanel();
add(panel);
/* I realized that the following code was made obsolete by the setFullScreenWindow method
//Get size of screen
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
//setSize(screenWidth, screenHeight);
*/
}
public void setUndecorated(boolean b) {
super.setUndecorated(b);
}
//this was going to be used to enable the user to resize the window and to move it
//around as java handles all that for the programmer when the window is decorated.
//Unfortunately, I ran into some scope issues, so it isn't used for now.
//To implement it, I would code it so that when the user hit a button, such as esc,
//the window would change to either decorated or undecorated.
//The game would pause when the window is decorated.
//So if the user wanted to decorate the window so he could move it around,
//setUndecorated(true) would be called. And if he wanted to start playing again,
//setUndecorated(false) would be called.
}
//end class ImageFrame
 
/**
A panel that displays an image, draws a grid for every increment of 50 in the x and y axes,
and implements tooltips for the mouse so that the user can see where he is in the panel
*/
class ImagePanel extends JPanel implements MouseMotionListener {
   
private Image image;
private Point tipPoint = null;
private Label tipLabel = new TipLabel();
private SelectionListener selectionListener;
public Polygon hex = new Polygon();
Hexagon hex_temp2 = new Hexagon(1, 1, 1,1);

public ImagePanel() {
    hex.addPoint(31,0);
    hex.addPoint(0,19);
    hex.addPoint(0,55);
    hex.addPoint(31,73);
    hex.addPoint(63,55);
    hex.addPoint(63,19);
    //Hexagon hex = new Hexagon(1,1,10,10);
   
    // get the image
    try {
        image = ImageIO.read(new File("C:/Users/Jon/Pictures/konq_back.jpg"));
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    this.add(tipLabel);
    tipLabel.setVisible(false);
    //mouseClicked is not called if the mouse moves between the mouse button being pressed
    //and released. Hence the the press and release separately are handled separately.
   
    this.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            mouseClick(e, true);
    }
    public void mouseReleased(MouseEvent e) {
        mouseClick(e, false);
    }
    });
    // Track mouse motion for tooltips
    this.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseMoved(MouseEvent e) {
            ImagePanel.this.mouseMoved(e);
        }
    });


    }
    //end constructor
    /** Register a selectionListener.
    *
    * For simplicity, only one listener at a time is supported.
    *
    * See end of file for the SelectionListener interface
    */
    public void setSelectionListener(SelectionListener s) {
        selectionListener = s;
    }
    private Point clickedPoint = null;
    /** Respond to a mouse click by selecting the point... This isn't a good way of
    * articulating what I'm thinking; but after running the program a few times
    * you'll see why it works.
    *
    * ATTENTION!!!! PARTS OF THIS METHOD MIGHT BE CAUSING THE FLICKER PROBLEMS
    * THAT ARE HAPPENING!!!
    * */
    public void mouseClick(MouseEvent e, boolean down) {
        Point pt = e.getPoint();
        if (down) {
        // The mouse button has gone down: record where we are
            clickedPoint = pt;
            if (hex_temp2.contains(pt)) {
                System.out.println("Clicked in region of hexagon");
            } else {
                System.out.println("did not click in region");
            }
        }
        else {
        // The mouse button has gone up: perform the action if we are still over
        // the same town
        if (selectionListener != null && pt != null && pt == clickedPoint)
            selectionListener.select(pt);
            clickedPoint = null;
        }
    }
    public Image getImage() {
        return image;
    }
    // Track mouse movement in order to show tooltips
    public void mouseMoved(MouseEvent e) {
    Point z = e.getPoint();
    tipPoint = z;
    tipLabel.setText(z.toString());
    tipLabel.setSize(tipLabel.getPreferredSize());
    tipLabel.setLocation(z.x+8, z.y+8);
    tipLabel.setVisible(true);
    //I originally thought that I would need the original lines of code, but they
    //cause lots of problems. SO DO NOT THINK THAT IMPLEMENTING THEM WILL
    //IMPROVE ANYTHING
    /*
    //repaint();
    if (tipPoint != null) {
    //Tool tip showing: remove if not at that point anymore
    tipPoint = null;
    tipLabel.setVisible(false);
    return;
    }*/
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
   
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                   RenderingHints.VALUE_ANTIALIAS_ON);
       
        if (image == null) return;
        // draw the image in the upper-left corner
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
       
        g.drawImage(image, 0,0, null);
   
        for(int y = 0; y < screenHeight; y+=50) {
            g.drawLine(0, y, screenWidth, y);
        }
           
   
        for(int x = 0; x < screenWidth; x+= 50) {
            g.drawLine(x, 0, x, screenHeight);
        }
       
        int hex_x[] = { 31 ,0  ,0  ,31 ,63, 63 };
        int hex_y[] = { 0,19,55,73,55,19 };
        g.setColor(Color.green);
       
           
            //g.translate((800/2)-30,(600/2)-40);
            //hex_temp2.move(800/2, 600/2);
            g.fillPolygon(hex_temp2);
            hex_temp2.move(800/2+30,600/2+30);
            repaint();
           
            Hexagon nhex = new Hexagon(1,1,1,1);
            //g.clipRect(0, 0, 100, 100);

       
       
       
       
       
       
       
       
        //g.drawRect(575, 500, 150, 85);
        //displayToolTipText();
        }
        //end paintComponents
        //Deprecated, but kept for ideas.
        /*
        public void displayToolTipText() {
        Point currentcoords = new Point(600,600 /*e.getPoint());
        //Toolkit kit = Toolkit.getDefaultToolkit();
        (ToolTipManager.sharedInstance()).registerComponent(this);
        JToolTip tooltip = this.createToolTip();
        tooltip.setToolTipText(currentcoords.toString());
        }*/
        //implement the MouseMotionListener interface method mouseDragged
   
        public void mouseDragged(MouseEvent e) {
        //do nothing
        }
    }

    //Interface for classes that can listen for clicks on point
    interface SelectionListener {
        void select(Point pt);
    }
    /**
    Like a Label, but with a preferred size barely big
    * enough for the text.
    */
    class TipLabel extends Label {
        Font tipFont = new Font("Segoe UI", Font.PLAIN, 10);
    public TipLabel() {
        setBackground(new Color(255, 255, 255));
        setFont(tipFont);
    }
    /** Calculate the mimimum size */
    public Dimension getMinimumSize() {
    FontMetrics fm = getFontMetrics(tipFont);
    String label = getText();
    if (label == null) label = ""; // Add 4 pixels to the width for safety
    return new Dimension(fm.stringWidth(label)+20, fm.getHeight());
    }
    /** Calculate the preferred size. */
    public Dimension getPreferredSize() {
    return getMinimumSize();
    }
}
 


 

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

 

Comments: 0