Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32555

JPanel and paintComponent different results between Windows/Linux

Newsgroups comp.lang.java.programmer
Date 2015-09-07 17:27 -0700
Message-ID <64b2edb0-379c-4e60-b985-891d9c0a320c@googlegroups.com> (permalink)
Subject JPanel and paintComponent different results between Windows/Linux
From Dark Mornings <pwong18@gmail.com>

Show all headers | View raw


Hello. I have a small program that displays a bar graph of 5 bars of varying lengths. The bars display in Windows but not in Linux. I used NetBeans 8/JDK 1.8 to compile the code in Windows and OpenJDK in Ubuntu 14.04. I know paintComponent() is called because I inserted a println() statement into the body of the do-while loop and saw the output. It seems paintComponent() is called 3 times.

Why does the my program work in Windows but not in Linux?

Here's the code:

[code]
import javax.swing.JFrame;
import javax.swing.JOptionPane;

// BarGraph.java
public class BarGraph
{
    public static void main(String args[])
    {
        int bars[];
        String inp;
        String nDigits[];
        
        bars = new int[5];
        nDigits = new String[5];
            
        inp = JOptionPane.showInputDialog(
                "Enter 5 numbers separated by spaces:");
        nDigits = inp.split(" ");
        for (int count = 0; count < nDigits.length; count++) {
            bars[count] = Integer.parseInt(nDigits[count]);
        }
        
        GraphPattern pnl = new GraphPattern(bars[4], bars[3], bars[2], 
                bars[1], bars[0]);
        JFrame app = new JFrame();
        
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.add(pnl);
        app.setSize(500, 500);
        app.setTitle("Bar Graph");
        app.setVisible(true);
    }
}
[/code]

[code]
// GraphPattern.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class GraphPattern extends JPanel
{
    private static int nStartX = 20;
    private static int nStartY = 20;
    private static final int nHeight = 50;
    private static final int nWidth = 12;
    private static final int nGap = 5;
    private static final int nBarCount = 5;
    
    private int bars[];
    
    public GraphPattern() 
    { 
        initGraphPattern();
    }
    
    public GraphPattern(int nBar1, int nBar2, int nBar3, int nBar4, int nBar5)
    {
        initGraphPattern();
        bars[0] = nBar1;
        bars[1] = nBar2;
        bars[2] = nBar3;
        bars[3] = nBar4;
        bars[4] = nBar5;
    }
    
    private void initGraphPattern()
    {
        bars = new int[nBarCount];
        for (int counter = 0; counter < bars.length; counter++)
        {
            bars[counter] = 1;
        }
    }
    
    public void paintComponent(Graphics gr)
    {
        int count = 4;
        
        super.paintComponent(gr);  // set up canvas for drawing
        
        do {
            gr.setColor(Color.green);
            gr.fillRect(nStartX, nStartY, nWidth * bars[count], nHeight);
            nStartY += (nHeight + nGap);
            count--;
        } while (count >= 0);
        
    }
}
[/code]

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

JPanel and paintComponent different results between Windows/Linux Dark Mornings <pwong18@gmail.com> - 2015-09-07 17:27 -0700
  Re: JPanel and paintComponent different results between Windows/Linux Jeff Higgins <jeff@invalid.invalid> - 2015-09-07 21:21 -0400
    Re: JPanel and paintComponent different results between Windows/Linux Jeff Higgins <jeff@invalid.invalid> - 2015-09-07 22:41 -0400
    Re: JPanel and paintComponent different results between Windows/Linux Nigel Wade <nmw@ion.le.ac.uk> - 2015-09-08 14:37 +0100
      Re: JPanel and paintComponent different results between Windows/Linux Jeff Higgins <jeff@invalid.invalid> - 2015-09-08 23:04 -0400

csiph-web