3

im developing an GUI Application, using JSWing, i load XML file, then deserialize it, then i add all created object to the JPanel. However, until i move the window, or click on the panel, this is how they looks like

enter image description here

After i move the window, they look correctly, so how to fix this issue <

I looked at this link http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics) and it might be the answer, since in the constructor of the JComponent i use

setOpaque(true);

but im still not sure how to fix the issue since that part of documentation is very hard to understand (it somehow just does not make any sense to me :-D )

by the way, the painting itselfs goes something like this

for (NetObject o : objects) {

                addNewObject(o);
            }

and addNewObject (not whole code)

public void addNewObject(NetObject o) {



         DraggableComponent object = new DraggableComponent(o, editorIndex); //Creates a draggableComponent
        this.add(object);//Adds this component to main container

        object.setOverbearing(true); //On click ,this panel gains lowest z-buffer


            object.setSize(48, 48);
            object.setLocation(o.x - 23, o.y - 23);
            object.setBackground(Color.WHITE);
            this.repaint(); //this = JPanel

}

and the overriden paintComponent code

@Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (isOpaque()) {
            if (object.type == 2) { //tarnsition
                g.drawImage(transition, 0, 0, this);

            } else if (object.type == 1) {
                boolean test = g.drawImage(place, 0, 0, this); //place
                g.drawString(object.loadTokens(), 3, 27); // 1,2,3,4...
            }
        }

    }

i tried to call this.revalidate - after FOR EACH LOOP - didnt help, the only way that works is to move somehow with the window, strangely, this problem exists only @ Windows, my collegue is developing this exact same application under Linux, and he does not experience ani graphical issues.

I know that there been an awfully lot of topics like this, but i honestly was not able to figure out the solution.

Thanks for the answer, OSiRiS

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Anton Giertli
  • 896
  • 2
  • 10
  • 29

1 Answers1

4

The setBackground() API mentions that "It is up to the look and feel to honor this property, some may choose to ignore it." Set the graphics context's color explicitly in paintComponent() and invoke fillRect().

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also the [opacity](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props) property. – trashgod Mar 30 '12 at 00:46
  • Thanks, simply g.setColor(Color.WHITE); g.fillRect(0, 0, 48, 48); boolean test = g.drawImage(config.place, 0, 0, this); //place g.drawString(object.loadTokens(), 3, 27); // 1,2,3,4... works ! – Anton Giertli Mar 30 '12 at 08:59