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 ); } }