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

Thread: frame not displaying correct contents

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

    Default frame not displaying correct contents

    I am working on a program that allows two people to play chess. I created a frame that allows a user to select a piece to promote a pawn to when the reach the 8th rank. The problem is, when I create this frame from my main method, it displays fine, but when I create it from my mouse listener, which is where its supposed to be created, it shows a 2x4 checkered pattern, and im not sure why. To be more clear, it actually displays whatever is behind it when its created, because I experimented with displaying it in different locations. here is a compilable example that displays the bug. just copy and paste the following into Example.java, compile and run. When you run it, the promotion frame should come up correctly the first time. click on a string within it to make it go away. after that, click any square, then click on a square in the top row and it will come up incorrectly and you will see the bug

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class Example extends JFrame {
     
        private SquarePanel[][] squares;
        private Colors colors;
     
        public Example () {
            super();
            colors = new Colors();
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container con = this.getContentPane();
            setSize(800,800);
            setLocation(0,0);
            con.setLayout(new GridLayout(8,8));
            SquarePanel pane = null;
            SquarePanel lastpanel = null;
            squares = new SquarePanel[8][8];
            int color = colors.WHITE;
     
            for(int i = 7; i >= 0; --i) {   
     
                if(color == colors.WHITE)
                    color = colors.BLACK;
                else
                    color = colors.WHITE;
     
                for(int j = 0; j <= 7; ++j) {
     
                    if(color == colors.WHITE)
                        color = colors.BLACK;
                    else
                        color = colors.WHITE;
     
                    lastpanel = new SquarePanel(color,j,i);
                    pane = lastpanel;              
                    con.add(pane);
                    squares[j][i] = pane;
                }
            }
     
            for(int i = 0; i < 8; ++i) {
                for(int j = 0; j < 8; ++j) {
                    squares[j][i].setResident("pawn");
                    //System.out.println(squares[j][i].getResident());
                    //squares[j][i].repaint();
                }
            }
     
            MouseListener listener = new MoveListener(this, squares);
            for(int i = 0; i < 8; ++i)
                for(int j = 0; j < 8; ++j)
                    squares[i][j].addMouseListener(listener);
            setVisible(true);
            PromotionFrame pf = new PromotionFrame(this, 1);
            String p = null;
     
            while(p == null) {
                p = pf.getPiece();
            }
     
            pf.dispose();
        }
     
        public class SquarePanel extends JPanel {
            private ImageIcon content;
            private int x;
            private int y;
            private int color;
            private String resident;
            private Colors colors;
     
            public SquarePanel(int color, int x, int y) {
                content = null;
                resident = null;
                colors = new Colors();
                this.color = color;
                this.x = x;
                this.y = y;
                Border blackline = BorderFactory.createLineBorder(Color.black);
                setBorder(blackline);
                this.setLayout(new GridBagLayout());
            }
     
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                int width = getWidth();
                int height = getHeight();
     
                if(color == colors.WHITE)
                    g.setColor(Color.white);
                else
                    g.setColor(Color.green);
     
                g.fillRect(0, 0, width, height);
                g.setColor(Color.black);
                //System.out.println("here");
            }
     
            public String getResident() {
                return resident;
            }
     
            public void setResident(String p) {
                resident = p;
     
                if(p == null)
                    removeAll();
                else
                    add(new JLabel(p));
            }
     
            public int getXCoordinate() {
                return x;
            }
     
            public int getYCoordinate() {
                return y;
            }
        }
     
        private class Colors {
            public int BLACK = 0;
            public int WHITE = 1;
        }
     
        public class MoveListener implements MouseListener {
            private int first_click;
            private SquarePanel first_source;
            private JFrame frame;
            private int turn;
            private SquarePanel[][] squares;
     
            public MoveListener(JFrame f, SquarePanel[][] s) {
                first_click = 0;
                this.frame = f;
                turn = colors.WHITE;
                squares = s;
            }
     
     
            /* Empty method definition. */
            public void mousePressed(MouseEvent e) {
                SquarePanel p = (SquarePanel)e.getSource();
                ImageIcon content = null;
     
                if(first_click == 0) {
                    first_source = p;
                    ++first_click;
                    p.setBorder(BorderFactory.createLineBorder(Color.cyan,4));
                }
                else {
                    first_click = 0;
                    first_source.setBorder(BorderFactory.createLineBorder(Color.black));
                    //System.out.println(turn == colors.WHITE ? "White" : "Black");
     
                    if(p.getResident().equals("pawn") && (p.getYCoordinate() == 7 || p.getYCoordinate() == 0)) {
                        PromotionFrame pframe = new PromotionFrame(frame, colors.WHITE);
                        String piece;
     
                        do {
                            piece = pframe.getPiece();
                        }
                        while(piece == null);
     
                        System.out.println(piece);
                        pframe.dispose();
                        p.setResident(piece);
                    }
     
                }
     
            }
     
            /* Empty method definition. */
            public void mouseReleased(MouseEvent e) {
            }
     
            /* Empty method definition. */
            public void mouseEntered(MouseEvent e) {
            }
     
            /* Empty method definition. */
            public void mouseExited(MouseEvent e) {
            }
     
            public void mouseClicked(MouseEvent e) {
     
            }
     
        }
     
        public class PromotionFrame extends JFrame implements MouseListener {
            private JFrame master;
            private String piece;
     
            public PromotionFrame(JFrame master, int color) {
                super();
                this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                Container con = this.getContentPane();
                setSize(400,200);
                this.master = master;
                setLocation(master.getX()+master.getWidth()/2-200,master.getY()+master.getHeight()/2-100);
                setLocation(0,0);
                con.setLayout(new GridLayout(1,4));
                ImageIcon imgicon1;
                Image img1;
                Image newimg;
                ImageIcon img;
                ImageLabel label;
                piece = null;
     
                label = new ImageLabel("queen");
                label.addMouseListener(this);
                con.add(label);
                label = new ImageLabel("rook");
                label.addMouseListener(this);
                con.add(label);
                label = new ImageLabel("bishop");
                label.addMouseListener(this);
                con.add(label);
                label = new ImageLabel("knight");
                label.addMouseListener(this);
                con.add(label);
     
                this.show();
            }
     
            public String getPiece() {
                return piece;
            }
     
            /* Empty method definition. */
            public void mousePressed(MouseEvent e) {
                ImageLabel label = (ImageLabel)e.getSource();
                piece = label.getPiece();
            }
     
            /* Empty method definition. */
            public void mouseReleased(MouseEvent e) {
            }
     
            /* Empty method definition. */
            public void mouseEntered(MouseEvent e) {
            }
     
            /* Empty method definition. */
            public void mouseExited(MouseEvent e) {
            }
     
            public void mouseClicked(MouseEvent e) {
     
            }
     
            private class ImageLabel extends JLabel {
                private String piece;
     
                ImageLabel(String piece) {
                    super(piece);
                    this.piece = piece;
                }
     
                public String getPiece() {
                    return piece;
                }
            }
        }
     
        public static void main(String[] args) {
            new Example();
     
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: frame not displaying correct contents

    The problem is with your while loops. They are executing on the EDT, which is eating up all your resources and not allowing your program to paint (which is also done on the EDT).

    You're going to want to return your pieces in a better way than simply busy-waiting like that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Can anybody correct the code?
    By ur2cdanger in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 24th, 2011, 12:50 AM
  2. problem displaying a new frame
    By yemista in forum AWT / Java Swing
    Replies: 0
    Last Post: October 18th, 2011, 02:08 PM
  3. Is My answers correct??
    By Java.Coder() in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 28th, 2010, 06:22 AM
  4. Correct Coupling
    By poulenc in forum Java Theory & Questions
    Replies: 0
    Last Post: December 26th, 2010, 04:28 AM
  5. Reading contents of another window
    By jimmys in forum Java Theory & Questions
    Replies: 8
    Last Post: October 4th, 2010, 11:40 PM