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


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

JPanel and paintComponent different results between Windows/Linux

Started byDark Mornings <pwong18@gmail.com>
First post2015-09-07 17:27 -0700
Last post2015-09-08 23:04 -0400
Articles 5 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#32555 — JPanel and paintComponent different results between Windows/Linux

FromDark Mornings <pwong18@gmail.com>
Date2015-09-07 17:27 -0700
SubjectJPanel and paintComponent different results between Windows/Linux
Message-ID<64b2edb0-379c-4e60-b985-891d9c0a320c@googlegroups.com>
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]

[toc] | [next] | [standalone]


#32556

FromJeff Higgins <jeff@invalid.invalid>
Date2015-09-07 21:21 -0400
Message-ID<msld73$969$1@dont-email.me>
In reply to#32555
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.

[toc] | [prev] | [next] | [standalone]


#32557

FromJeff Higgins <jeff@invalid.invalid>
Date2015-09-07 22:41 -0400
Message-ID<mslhs2$fj8$1@dont-email.me>
In reply to#32556
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<Integer> bars = new ArrayList<Integer>();
     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<Integer> bars;

   public void setBars(List<Integer> 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);
     }
   }
}

[toc] | [prev] | [next] | [standalone]


#32559

FromNigel Wade <nmw@ion.le.ac.uk>
Date2015-09-08 14:37 +0100
Message-ID<d586lfFeu3cU1@mid.individual.net>
In reply to#32556
On 08/09/15 02:21, 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.
>
>

To the OP: If you are using NetBeans and create a "new JFrame Form" (in the "new" menu) it will do the boiler-plate for 
you. It's then up to you to design the form either in Matisse, or by editing the constructor and adding code after 
initComponents().

Be aware, though, that the default action for "new JFrame Form" is to add a pack() at the end of initComponents(). If 
you add components to the GUI after initComponents() they might not be properly packed. You can modify the default 
action (JFrame Properties, Code tab, Form Size Policy option) and manually pack() at the end of the constructor.

[toc] | [prev] | [next] | [standalone]


#32565

FromJeff Higgins <jeff@invalid.invalid>
Date2015-09-08 23:04 -0400
Message-ID<mso7kc$os0$1@dont-email.me>
In reply to#32559
On 09/08/2015 09:37 AM, Nigel Wade wrote:
> On 08/09/15 02:21, 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.
>>
>>
>
> To the OP: If you are using NetBeans and create a "new JFrame Form" (in
> the "new" menu) it will do the boiler-plate for you. It's then up to you
> to design the form either in Matisse, or by editing the constructor and
> adding code after initComponents().
>
> Be aware, though, that the default action for "new JFrame Form" is to
> add a pack() at the end of initComponents(). If you add components to
> the GUI after initComponents() they might not be properly packed. You
> can modify the default action (JFrame Properties, Code tab, Form Size
> Policy option) and manually pack() at the end of the constructor.
>

Another alternative for someone who has a need for a simple
Swing executable class and doesn't wish to use the Matisse builder.

Follow the tutorial located here:
<https://platform.netbeans.org/tutorials/nbm-filetemplates.html>
to create and install his own template.

Perhaps extending the Java Main Class template such as:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "${project.licensePath}">

<#if package?? && package != "">
package ${package};

</#if>
/**
  *
  * @author ${user}
  */
public class ${name} {

   public ${name}(String args) {
     // TODO auto generated constructor
   }

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
     SwingUtilities.invokeLater(new Runnable() {
       public void run() {
         new ${name}(args);
       }
     });
   }
}

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web