Hibiflobu
Hibiflobu

Reputation: 43

Why is the same program running differently on Windows than Mac

I was running a piece of code on my mac and it was able to run as I wanted it to but when I ran the same exact thing on my windows PC the output of the program was totally different. It was a java swing window that would allow you to move a dot around the screen. On the mac it was able to work perfectly fine, but on my windows PC the dot leaves a trail of afterimages. Basically every time the panel is repainted the graphics are not being cleared. What should I do to fix this? I already tried uninstalling and reinstalling java on my PC but it hasn't changed the output of the program. If you need to take a look at the graphics code it is here: https://github.com/Nathaniel-github/NetworkingTrialClient in the GraphicsPanel class.

Here is my paint component:

public void paintComponent(Graphics g) {

            g.setColor(Color.WHITE);

            if (notConnectedToServer()) {

                g.drawString("Connecting to server...", 680, 400);

            } else if (serverConnection) {

                try {

                    int numberOfPlayers = dataIn.readInt();

                    fileWriter.write("Data received(Number of Players): " + numberOfPlayers);

                    for (int i = 0; i < numberOfPlayers; i ++) {

                        int x = dataIn.readInt();
                        fileWriter.write("Data received(X coordinate), Iteration " + i + ": " + x);
                        int y = dataIn.readInt();
                        fileWriter.write("Data received(Y coordinate), Iteration " + i + ": " + y);

                        g.fillOval(x - 5, y - 5, 10, 10);

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else {

                g.fillOval(player.getX() - 5, player.getY() - 5, 10, 10);

            }

        }

Upvotes: 0

Views: 223

Answers (1)

khelwood
khelwood

Reputation: 59113

When you override paintComponent, typically you want to start by calling the overridden paintComponent method, which will fill in the space, painting over whatever was there from the last paint.

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    // Draw your stuff
}

Upvotes: 3

Related Questions