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 15 of 15

Thread: GUI part in lines of action

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default GUI part in lines of action

    i have a problem with GUI i can't write the code for lines of action and the deadline of submission is tomorrow midnight (Cairo : gmt+2)
    i work on eclipse, can any body help me with that?

    --- Update ---

    i've written the code of the engine part and my whole problem is in the java part


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,150
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: GUI part in lines of action

    Do you have any specific questions about your assignment?
    Please post your code and any questions about problems you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: GUI part in lines of action

    the thing is that i don't understand GUI so i need help with it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,150
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: GUI part in lines of action

    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: GUI part in lines of action

    take a look at this. it's my gui code till now and it doesn't show the board when i run,, it shows an emty window. why is that ?
    Attached Files Attached Files
    • File Type: zip q.zip (1.1 KB, 0 views)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,150
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: GUI part in lines of action

    Please post any code you have questions about in the thread. No links.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: GUI part in lines of action

    this is the main method class
    package eg.edu.guc.loa.gui;
     
    import eg.edu.guc.loa.engine.*;
    import java.awt.Color;
    import java.awt.GridLayout;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Frame1 {
    	public static void main(String[] args) {
    		JFrame f = new JFrame("Lines Of Action");
    		f.setVisible(true);
    		f.setSize(700, 700);
     
    		JPanel panel = new JPanel();
    		panel.setSize(700, 700);
     
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		BoardGui b = new BoardGui();
    		f.add(panel);
    		panel.add(b);
    	}
     
    }
     
    and this is the board class
     
    package eg.edu.guc.loa.gui;
     
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Polygon;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Rectangle2D;
     
    @SuppressWarnings("serial")
    public class BoardGui extends JPanel {
    	JPanel[][] board;
     
    	public BoardGui() {
    		setLayout(new GridLayout(8, 8));
    		this.setVisible(true);
    		this.setSize(700, 700);
     
    		this.setBounds(0, 0, 700, 700);
    		board = new JPanel[8][8];
    		int count = 0;
    		for (int i = 0; i != 8; i++) {
    			for (int j = 0; j != 8; j++) {
    				if (count % 2 == 0) {
    					board[i][j].setBackground(Color.GREEN);
    				} else {
    					board[i][j].setBackground(Color.CYAN);
     
    				}
     
    			}
     
    		}
     
    	}
     
    	public static void main(String[] args) {
    		BoardGui b = new BoardGui();
    		b.show();
    	}
    }
    Last edited by Norm; May 9th, 2013 at 03:37 PM. Reason: added code tags

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,150
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: GUI part in lines of action

    Both classes have a main() method. Which class do you use with the java command to execute the program?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: GUI part in lines of action

    the first class

    --- Update ---

    i tried deleting the main method in the second class but that was useless

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,150
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: GUI part in lines of action

    Do you get errors when executing the code? Copy the full text and pasted it here.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    trexy (May 10th, 2013)

  12. #11
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: GUI part in lines of action

    No i don't get any errors it is executed and run perfectly but the problem is the frame opens is blank and has no panels (board) in it.
    and this is the full text pasted above and as i stated in it these are two separate classes.

  13. #12
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: GUI part in lines of action

    Firstly you need to initialize the individual JPanels. Secondly, you need to add them to something.
     
    board[i][j] = new JPanel();
    add(board[i][j];

  14. The Following User Says Thank You to theoriginalanomaly For This Useful Post:

    trexy (May 10th, 2013)

  15. #13
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: GUI part in lines of action

    take a look at this,, it shows no errors but it doesn't run what is the reason ?
    and it runs correctly showing the board without any checkers(buttons) when i remove any add statements.

    public BoardGui() {

    setBounds(0, 0, 600, 600);
    setResizable(false);
    board = new JPanel[8][8];
    setLayout(new GridLayout(8, 8));
    for (int i = 0; i < board.length; i++) {
    for (int j = 0; j < board[i].length; j++) {
    board[i][j] = new JPanel();
    getContentPane().add(board[i][j]);
    }
    }

    int count = 0;
    for (int i = 0; i != board.length; i++) {
    for (int j = 0; j != board.length; j++) {

    if (count % 2 == 0) {
    board[i][j].setBackground(Color.CYAN);
    } else {
    board[i][j].setBackground(Color.DARK_GRAY);
    }

    count++;
    }
    count++;
    }
    ButtonGui b = new ButtonGui();
    for (int i = 0; i <= board.length; i++) {
    for (int j = 0; j <= board.length; j++) {
    board[i][j] = new JPanel();

    }
    }
    for (int i = 1; i < 7; i++) {

    board[0][i].add(b);
    board[7][i].add(b);
    }
    for (int i = 1; i < 7; i++) {
    board[i][0].add(b);
    board[i][7].add(b);
    }

    }

  16. #14
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: GUI part in lines of action

    You should combine your for loops, no need to go through the same procedure 3 times. Are you just adding buttons for the click listener? You should be getting compile time errors for trying to call getContentPane() JPanel doesn't have that method, and setResizable(). Those are both methods for the JFrame. Also, there is no reason to add a JPanel to the frame and then add the boardgui to the panel. Just add the boardgui (it is a JPanel) to the frame. And the method you are using will not create a checker board, just columns of the same color. What Ide are you using? It makes no sense that you are not getting errors. And you are adding the same button to all of the jpanels.

    --- Update ---

    JFrame is the easel, JPanel is the canvas, and Graphics are the paintbrush. You should use the graphics to paint, it will give you more control.

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,150
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: GUI part in lines of action

    i don't get any errors
    The code would give errors because this statement was missing:
    board[i][j] = new JPanel();
    It would cause a NullPointerException
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help With Three Part Program
    By new2java123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 04:27 PM
  2. [2 Part Qestion] Saving Game Data & Hiding GUI from taskbar
    By Jello36 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2012, 09:38 PM
  3. Help..Simple GUI needs help,can not sort out these red lines..
    By zhen1606 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2012, 08:06 AM
  4. Replies: 1
    Last Post: April 26th, 2011, 08:47 AM
  5. [SOLVED] Help Again Last Part of my Problem :(
    By metalx66 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 09:48 PM