Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Jeff Higgins Newsgroups: comp.lang.java.programmer Subject: Re: JPanel and paintComponent different results between Windows/Linux Date: Mon, 07 Sep 2015 22:41:16 -0400 Organization: A noiseless patient Spider Lines: 107 Message-ID: References: <64b2edb0-379c-4e60-b985-891d9c0a320c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 8 Sep 2015 02:41:06 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="a78aad8b483612fbf45d18e918df0cca"; logging-data="15976"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+aG7fR8KUS8qBAdP+n2TrVOmVMnGaI10I=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: Cancel-Lock: sha1:GqqMXG5HBF79Nj8Mpv9qjOGsURw= Xref: csiph.com comp.lang.java.programmer:32557 On 09/07/2015 09:21 PM, Jeff Higgins wrote: > On 09/07/2015 08:27 PM, Dark Mornings wrote: >> 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? >> > > You have code that needs to run on the AWT event thread; > > public static void main(String args[]) { > SwingUtilities.invokeLater(new Runnable() { > public void run() { > new BarGraph(); // or however you want to arrange it. > } > }); > > You should handle your InputDialog cancel option. > import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; public class BarGraph { JFrame frame = new JFrame(); GraphPattern graphPanel = new GraphPattern(); private void getUserInput() { String inp = JOptionPane.showInputDialog( "Enter 5 decimal digits separated by spaces:"); if(inp == null || inp == "") { System.exit(1); } String[] nDigits = inp.split(" "); List bars = new ArrayList(); for(String s : nDigits) { try { Integer b = Integer.parseInt(s); if(b < 0 || b > 9) { throw new NumberFormatException(); } bars.add(b); } catch (NumberFormatException e) {} } graphPanel.setBars(bars); display(); } private void display() { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(graphPanel); frame.setSize(500, 500); frame.setTitle("Bar Graph"); frame.setVisible(true); } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { new BarGraph().getUserInput(); } }); } } import java.awt.Color; import java.awt.Graphics; import java.util.List; import javax.swing.JPanel; @SuppressWarnings("serial") 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 List bars; public void setBars(List data) { if(data.size() > 5) { bars = data.subList(0, 5); } else { bars = data; } } public void paintComponent(Graphics gr) { super.paintComponent(gr); // set up canvas for drawing gr.setColor(Color.green); for (int b : bars) { gr.fillRect(nStartX, nStartY, nWidth * b, nHeight); nStartY += (nHeight + nGap); } } }