Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need some help

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Need some help

    First I want to say hello to the community. I am starting to learn java, and I want to do a bit of a more complex application. I want to make something like heroes 3, simplified version, based on a number of credits, I enroll units, and then on a battlefield, I make two armies fight.
    The units have attributes and so on, the battlefield I've implemented it like a chessboard, 8x8.
    So far, I'm working on the GUI, I've done the battlefield, but I am stuck at moving the units. Each unit is portrayed by a icon.

    createPage3 - this is the place of the battlefield, where the chessboard is implemented, as for the rest of the pages, I can deal with all that later, but now I am trying, for example, to have a unit there, and to move it, by drag and drop. From what I've read, I know I have to use something like mousePressed(MouseEvent e), mouseDragged(MouseEvent me) , mouseReleased(MouseEvent e) but I don't know where to implement all this, or how to do it.
    Also, should I move everything from createPage3 to another class, and get all from there into the page3?
    Very much appreciated any help.
    package src;
    import java.awt.*;
     
     
     
    import javax.swing.*;
     
    class TabbedPaneExample extends 	JFrame
    {
     
    	private		JTabbedPane tabbedPane;
    	private		JPanel		panel1;
    	private		JPanel		panel2;
    	private		JPanel		panel3;
    	ImageIcon i = new ImageIcon("http://www.javaprogrammingforums.com/images/davion.gif");
    	ImageIcon i2 = new ImageIcon("http://www.javaprogrammingforums.com/images/human_armour.gif");
    	ImageIcon i3 = new ImageIcon("http://www.javaprogrammingforums.com/images/neworc2mg3.gif");
    	ImageIcon i4 = new ImageIcon("http://www.javaprogrammingforums.com/images/22scythereloadedfullhelmbp7.gif");
     
    	public TabbedPaneExample()
    	{
    		// NOTE: to reduce the amount of code in this example, it uses
    		// panels with a NULL layout.  This is NOT suitable for
    		// production code since it may not display correctly for
    		// a look-and-feel.
     
    		setTitle( "GAME" );
    		setSize( 700, 700 );
    		setBackground( Color.gray );
    		setResizable(false);
    		JPanel topPanel = new JPanel();
    		topPanel.setLayout( new BorderLayout() );
    		getContentPane().add( topPanel );
    		//JOptionPane.showMessageDialog(topPanel,"Welcome to Battle SIM", "Message", getDefaultCloseOperation());
     
    		// Create the tab pages
    		createPage1();
    		createPage2();
    		createPage3();
     
    		// Create a tabbed pane
    		tabbedPane = new JTabbedPane();
    		tabbedPane.addTab( "Hire units",i, panel1 );
    		tabbedPane.addTab( "Army Overview",i, panel2 );
    		tabbedPane.addTab( "Page 3", i, panel3 );
    		topPanel.add( tabbedPane, BorderLayout.CENTER );
     
     
    	}
     
    	public void createPage1()
    	{
    		panel1 = new JPanel();
    		panel1.setLayout( new FlowLayout() );
     
    		panel1.add( new JButton( "Humans", i2 ) );
    		panel1.add( new JButton( "Undead", i3 ));
    		panel1.add( new JButton( "Orcs", i4 ));
     
     
    	}
     
     
    	public void createPage2()
    	{
    		panel2 = new JPanel();
    		panel2.setLayout(new FlowLayout());
    		panel2.add( new JButton( "Hire Units", i2 ) );
     
    	}
     
     
    	public void createPage3()
    	{
    		panel3 = new JPanel();
     
    		//Label l1 = new Label("FIGHT!!");
            panel3.setLayout(new GridLayout(8,8));
            for (int i=0; i<64; i++)
            {
                Panel square = new Panel();
     
                if ((i+i/8)%2 == 0)
                    square.setBackground(Color.darkGray);
                else
                    square.setBackground(Color.white);
     
                panel3.add(square);
               // add(square);
            }
           // pack();
           // setVisible(true);
     
    	}
     
     
    	public static void main( String args[] )
    	{
     
    		TabbedPaneExample mainFrame	= new TabbedPaneExample();
     
    		mainFrame.setVisible( true );
    	}
    }

  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help

    I don't know much about these things, but you should be able to read about event handling here: Javanotes 5.1.2, Section 6.4 -- Mouse Events

    this book is great!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need some help

    First, read through the following tutorials to get a better handle on MouseListeners to get a handle of how they work
    How to Write a Mouse-Motion Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    The details of how you can implement the listeners is dependent upon the details of how you implement your board (custom painting, components, drag and drop). Lastly, avoid mixing AWT and Swing (the createPage3 method adds Panels (AWT), rather than JPanels (Swing)).