1

it's my first post so I hope it'll not be too cringeworthy. So I am trying to create a hex-based strategy game, not quite there yet but anyways.

To achieve a hex-based game I would like to create a field made of hexes which the user should be able to click, and receive the coordinates of that pixel. At the moment I can produce either a field of hexes or a mouselistener/mouseadapter but not both. The last one executed replaces the other on the screen.

If the pane.add(New HexMap()); is switched with pane.add(new MouseListener()); the listener works but the line is not printed

I've looked around for quite some time but the posts that I've encountered had either dealt with changing the background color which the mouselistener can do, because background is independent of the mousesensorhttp://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html? The other examples I've come by have been too advanced for me, because they're using multiple panes, and I have not been able to comprehend themhttp://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html.

So what I'm looking for is a way to add a mouselistener over a single pane, displaying the hexes. Would this be possible? E.G adding the hexMap after the mouselistener would not overwrite the mouselistener but rather act as an addition

A single line has been created acting as a placeholder for the hexes.

The code:

    import java.awt.*;                      
    import java.awt.event.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.util.*;
    import java.util.List;
    import javax.swing.*;

    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;



    public class GraphicsSetup extends JPanel{      

    public static final int FRAME_WIDTH = 600;
    public static final int FRAME_HEIGHT= 400;      

    private static JFrame frame;


    public static void main(String[] args){
        GraphicsSetup draw = new GraphicsSetup();           
    }

    public GraphicsSetup(){     

        HexMap hexMap = new HexMap();       
        JPanel panel = new JPanel();            
        frame = new JFrame("HexExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);            
        Container pane = frame.getContentPane();            
        pane.setBackground(new Color(20, 100, 30));
        pane.add(new MouseListener());
        pane.add(new HexMap()); 
        frame.setVisible(true);         
    }    

    public class HexMap extends JComponent{

        public void paint(Graphics g){          
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.blue);           
            g2d.drawLine(0,0, FRAME_WIDTH, FRAME_HEIGHT);                   
        }       
    }
    class MouseListener extends JComponent{

        public MouseListener(){

            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent me) {
                    System.out.println("Mouse Event" + me);                     
                }
            });             
        }           
    }       
}   

Yours Sincerely

Andro
  • 13
  • 6
  • This post regarding [Playing with Shapes](http://tips4java.wordpress.com/2013/05/13/playing-with-shapes/), might can help you, I hope :-) And do remember, `paint()` is not used for custom drawings instead `paintComponent()` is used for a task at hand. Please have a look at [Solving common painting problems](http://docs.oracle.com/javase/tutorial/uiswing/painting/problems.html), look at the first problem and solution for the same :-) +1 for Yours Sincerely :-) – nIcE cOw Aug 12 '13 at 15:38
  • The order of pane.add(new MouseListener()); and pane.add(new HexMap()); makes all the difference sorry for not adding it, if mouselistener is first the line is not printed and vice versa – Andro Aug 12 '13 at 16:01
  • Please explain, as if you are explaining it to a child, at the start, how the window looks like, now what exactly will happen. What is a hex, where will one click with mouse, and what thingy should happen once its clicked ? The question is unclear still, I guess. – nIcE cOw Aug 12 '13 at 16:03
  • Upon executing you'll see a green field with a blue line. The line is a simplified model of a hexagon, The true hexagon will later replace the line. The program is also meant to display the specific coordinates of the point where you click with your mouse, this part is however not functional. If the order between the pane.add() is switched you can not see the blue line anymore but the mouselistener begin to work. Only the last activated is viewable – Andro Aug 12 '13 at 16:17
  • Still I doubt, I understood, completely :( Though, have a look at this [example](http://stackoverflow.com/a/18189896/1057230), if you wanted to do something like this !!! – nIcE cOw Aug 12 '13 at 16:23
  • I think I might have directed the focus to the wrong place, the hexagons and the coordinates are under full control. The problem that I'm struggling with is to display a drawn image while at the same time being able to get user input from the user's mouseclicks – Andro Aug 12 '13 at 16:35

1 Answers1

1

I'm not entirely sure what you're after, but try adding your components to your panel object. Such as:

panel.add(new MouseListener());
panel.add(new HexMap());

And then add this to the content pane of your frame:

pane.add(panel);

If you're wondering how to arrange your interface differently, read about layout managers here:

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Edit

Try the following:

Set the layout manager to use a BorderLayout:

JPanel panel = new JPanel(new BorderLayout()); 

Add your components to the panel and set their location:

panel.add(new MouseListener(), BorderLayout.NORTH);
panel.add(new HexMap(), BorderLayout.CENTER);

Add the panel to the frame content pane:

pane.add(panel);

This will work but the size of the MouseListener panel is quite small...you'll need to figure that out next...

PA001
  • 451
  • 3
  • 12
  • I tried adding HexMap and MouseListener to panel and then the panel to the container. But upon executing the frame turned grey, and the mouselistener doesn't work. Those parts should be just before frame.setVisible(true) right? – Andro Aug 12 '13 at 16:24
  • Correct, take a look at the amended answer. Thanks. – PA001 Aug 12 '13 at 16:41
  • True, it works, but it feels like the GUI should be able to have a working mouselistener, and painted stuff in the same place? – Andro Aug 12 '13 at 16:50
  • If you want your HexMap instance to listen for mouse events then you'll need to add the mouse listener to that, rather than creating a new panel/component. So that would mean creating an instance of HexMap, e.g, HexMap hexMap = new HexMap(); and then call hexMap.addMouseListener(...); – PA001 Aug 12 '13 at 17:15