Hi everyone. I wonder why the following code doesn't draw anything.

package AssProgLang;

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class AssOutput extends JPanel {
    public AssOutput(Queue translatedQueue) {        
        this.translatedQueue = translatedQueue;
        
        outputFrame = new JFrame();
        outputFrame.getContentPane().add(this);
        
        // outputPanel attributes
        outputFrame.setLocationRelativeTo(null);        
        outputFrame.setPreferredSize(new Dimension(640, 480));
        //this.setPreferredSize(new Dimension(640, 480));
        outputFrame.pack();
        outputFrame.setTitle("ASS™ Output Window");
        outputFrame.setResizable(false);
        outputFrame.setVisible(true);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int functionNb, thingNb, attribNb;
        
        while(translatedQueue.peek() != null) {
            functionNb = (Integer)translatedQueue.poll();
            thingNb = (Integer)translatedQueue.poll();
            attribNb = (Integer)translatedQueue.poll();
            switch(functionNb) {
                case AssConsts.DISPLAY: // display
                    switch(thingNb) {
                        case AssConsts.DOT: // dot
                            switch(attribNb) {
                                case 0: color = Color.BLACK; break;
                                case 1: color = Color.BLUE; break;
                                case 2: color = Color.CYAN; break;
                                case 3: color = Color.DARK_GRAY; break;
                                case 4: color = Color.GRAY; break;
                                case 5: color = Color.GREEN; break;
                                case 6: color = Color.LIGHT_GRAY; break;
                                case 7: color = Color.MAGENTA; break;
                                case 8: color = Color.ORANGE; break;
                                case 9: color = Color.PINK; break;
                                case 10: color = Color.RED; break;
                                case 11: color = Color.WHITE; break;
                                case 12: color = Color.YELLOW; break;
                            } // end attribNb switch
                    } // end thingNb switch
                    g.setColor(color);            // <-- doesn't draw here
                    g.fillRect(xpos, ypos, 5, 5); // <-- doesn't draw here
                    break; // end display case

                case 2: // move
                    switch(thingNb) {
                        case 3: // cursor
                            switch(attribNb) {
                                case 16: xpos += 5; break;
                                case 17: xpos -= 5; break;
                                case 18: ypos -= 5; break;
                                case 19: ypos += 5; break;
                            }
                    } // end
                    break; // end move case
            } // end functionNb switch
        }
    }

    // field
    private JFrame outputFrame;
    private Color color = Color.BLACK;
    private int xpos = 0, ypos = 0;
    private Queue translatedQueue;    
}

Please help me. Thank you.

There is a display when the

g.setColor(color); // <-- doesn't draw here
g.fillRect(xpos, ypos, 5, 5); // <-- doesn't draw here

are placed outside the while loop

Check that the while loop is being executed by putting a print statement in it. Similarly use prints to confirm that the right cases are being executed in all those nested switches

Yeah... Ive put tests i.e. System.out.println("Pass"); before and after the said lines and it does passes there. But I wonder why the g.fillRect() line is not working. Thanks for the reply.. Please help me more.

Are xpos and ypos set properly? - print those as well

Yes. Actually, as said, I copied and pasted the g.setColor(color); and g.fillRect(xpos, ypos, 5, 5); outside the while loop and it does display a filled rectangle. I tried pasting them just after the while() { line and it doesn't display anything... :( I don't know what's going on.

Did you actually print xpos & ypos immediately before the fillRect? - I notice that the code changes these values inside the while loop, so you can't assume they are correct in all places and at all times.

Yes I did.. It displays the desired xpos and ypos of the calling class.

OK. hmmm. only other thing i can think of is to print those 3 ints at the top of the while to see if they are funny in any way.. this isn't easy, is it?

Yeah i did. It was 0 because i had it initialized to 0. (xpos = 0, ypos = 0). When at runtime, case 2 is accessed, the xpos and ypos will be changed depending on the inner switch case. The ints are working normally as expected... :(

oh i get it. line 12 should be placed after line 21

I'm stuck. Based on the info you have given me I have no idea what could explain the symptoms you describe.
Let me summarise:
With those 2 lines (52,53) at that position you have print statements immediately before and after them that confirm (1) they are executed and (2) the x/ypos values are correct. Move the same 2 statements to after the while (is after line 69) and they do work.
I'm sorry, this is going to need a bigger brain than mine.
If/when you get the answer please post it here.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.