Results 1 to 9 of 9
Like Tree3Likes
  • 1 Post By pbrockway2
  • 1 Post By pbrockway2
  • 1 Post By pbrockway2

Thread: error during changing the color of paintComponent of my JPanel

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Exclamation error during changing the color of paintComponent of my JPanel

    Hi,
    my JPanel consists of only one paintComponent,

    I've drawn the graph by scaling, as Norm told me,
    Java Code:
    g2.setPaint(Color.green.darker());
    for(int i = 0; i < this.yArray.length-1; i++) {
    	double x1 = PAD + i/**xInc*/;
                double y1 = scale*((this.fftofyArray[i])-getMin());
                double x2 = PAD + (i+1)/**xInc*/;
                double y2 = scale*(this.fftofyArray[i+1]-getMin());
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
                
                
            }
    //draws lines of dark-green color

    Just like my 'double[] fftofyArray' array, I've another array 'String[] zero' which is a string array of same length. (of course, the two arrays are from a same excel file representing each column of the excel)
    and my 'zero[]' array consists of two cases:
    1.String which contains sub-string "inferior_values"
    2.String which doesn't contain sub-string "inferior_values"

    Now I've got to change the color of the line drawn to red, if the zero[] array indicates the word "inferior_value"

    So, I've tried this, but shows run-time error.
    Java Code:
    for(int i = 0; i < this.yArray.length-1; i++) {
            	
            	if(this.zero[i].contains("inferior_value")) g2.setPaint(Color.red.darker());
            	else g2.setPaint(Color.green.darker());
            	
            	double x1 = PAD + i/**xInc*/;
                double y1 = scale*((this.fftofyArray[i])-getMin());
                double x2 = PAD + (i+1)/**xInc*/;
                double y2 = scale*(this.fftofyArray[i+1]-getMin());
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
                
                
            }

    please note this: I tried a sample condition "if (i%2==0)", and it runs fine
    Java Code:
    for(int i = 0; i < this.yArray.length-1; i++) {
            	
    if(  /*this.zero[i].contains("inferior_value")*/(i%2)==0 ) g2.setPaint(Color.red.darker());
    //this one worked fine, but when I change it to the 'this.zero[i].contains("inferior_value"),
    // it gets struck at run-time :(
            	else g2.setPaint(Color.green.darker());
            	
            	double x1 = PAD + i/**xInc*/;
                double y1 = scale*((this.fftofyArray[i])-getMin());
                double x2 = PAD + (i+1)/**xInc*/;
                double y2 = scale*(this.fftofyArray[i+1]-getMin());
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
                
                
            }
    regards
    dhilip

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: error during changing the color of paintComponent of my JPanel

    shows run-time error.
    What is the error?

    The stack trace can be rather long in such cases, but it worth posting the whole thing. And indicating which lines of your code it is referring to.

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Red face Re: error during changing the color of paintComponent of my JPanel

    Quote Originally Posted by pbrockway2 View Post
    What is the error?

    The stack trace can be rather long in such cases, but it worth posting the whole thing. And indicating which lines of your code it is referring to.
    Hi, sorry for delay,

    my run-time errors were
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at ReadPlat2.paintComponent(ReadPlat2.java:92)
    My full coding is :
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.MouseWheelListener;
    import java.awt.font.FontRenderContext;
    import java.awt.font.LineMetrics;
    import java.awt.geom.Line2D;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.io.*;
    import java.util.Arrays;
    import java.util.Collections;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextPane;
    import javax.swing.ToolTipManager;
    
    public class ReadPlat2 extends JPanel
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 4157203125948260492L;
    	final int PAD = 30;
    	String[] zero;
    	double[] xArray ;
    	double[] yArray ;
    	double[] fftofyArray;
    	int Size;
    	
    //////////////////////////////////////////////////////////////////////////////////////////
    	public void paintComponent(Graphics g)
    	{
    	    super.paintComponent(g);
    
    		Graphics2D g2 = (Graphics2D)g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    		
    		int w = getWidth();
            int h = getHeight();
            
            // Draw ordinate.
            g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
            
            // Draw abscissa.
            g2.draw(new Line2D.Double(PAD, 300, w-PAD, 300));
            
            // Draw labels.
            Font font = g2.getFont();
            FontRenderContext frc = g2.getFontRenderContext();
            LineMetrics lm = font.getLineMetrics("0", frc);
            float sh = lm.getAscent() + lm.getDescent();
            // Ordinate label.
            String s = "Amplitude g";
            float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
            for(int i = 0; i < s.length(); i++) {
                String letter = String.valueOf(s.charAt(i));
                float sw = (float)font.getStringBounds(letter, frc).getWidth();
                float sx = (PAD - sw)/2;
                g2.drawString(letter, sx, sy);
                sy += sh;
            }
            s = "Frequency Hz";
            sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
            float sw = (float)font.getStringBounds(s, frc).getWidth();
            float sx = (w - sw)/2;
            g2.drawString(s, 700, sy);
            
            double xInc = (double)(w - 2*PAD)/(this.yArray.length-1);
            double scale = (double)(h - 2*PAD)/getMax();
            
            for(int i=PAD;i<w-PAD;i=i+100)
            {
            	
            	g2.drawString(Double.toString(Math.round(1000*this.xArray[i])), i, 330);
            }
                    	
            	g2.drawString("0.053",  PAD,2*PAD);
                   
                    
            g2.setPaint(Color.green.darker());
            for(int i = 0; i < this.yArray.length-1; i++) {
            	
            	if(!this.zero[i].contains("inf")) g2.setPaint(Color.green.darker());
            	else g2.setPaint(Color.red.darker());
            	
            	double x1 = PAD + i/**xInc*/;
                double y1 = PAD+scale*((this.fftofyArray[i])-getMin());
                double x2 = PAD + (i+1)/**xInc*/;
                double y2 = PAD+scale*(this.fftofyArray[i+1]-getMin());
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
                
                
            }
            
            // Mark data points.
            g2.setPaint(Color.red);
         
            
    //////////////////////////////////////////////////////////////////////////////////////////
    	}
    	
    	private double getMax() {
    	       double max = -Integer.MAX_VALUE;
    	        for(int i = 0; i < this.yArray.length; i++) {
    	            if(this.fftofyArray[i] > max)
    	                max = this.fftofyArray[i];
    	        }
    	        double min = Integer.MAX_VALUE;
    	        for(int i = 0; i < this.yArray.length; i++) {
    	            if(this.fftofyArray[i] < min)
    	                min = this.fftofyArray[i];
    	        }
    	        return (max-min);
    	    }
    	private double getMin() {
    		double min=Integer.MAX_VALUE;
    		for(int i = 0; i < this.yArray.length; i++) {
                if(this.fftofyArray[i] < min)
                    min = this.fftofyArray[i];
            }
    		return min;
    	}
    	
    	
    	public ReadPlat2(int size)
    	{
    		this.Size=size;
    		zero=new String[this.Size];
    		xArray= new double[this.Size];
    		yArray= new double[this.Size];
    		fftofyArray=new double[this.Size];
    	}
    	
    	public void setData(int i,double x,String z,double y)
    	{
    		if(i>this.Size)
    			return;
    		zero[i]=z;
    		xArray[i]=x;
    		yArray[i]=y;
    	}
    	
    	public static void main(String[] args) throws IOException
    	{   
    		
    		ReadPlat2 ReadPlatInstance;
    		String filename;
    		FileReader file_in;
    		BufferedReader in;	
    		String input_line;
    		String[]input_split;
    		int TotalLineCount=0;
    		
    		filename = args[0];
    		file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);
    	    
    	    input_line ="Starting";
    	    
    	    
    	    //Reading total lines 
    	    while((input_line=in.readLine())!=null)
    	    {	
    	    	//input_line =in.readLine();
    	    	TotalLineCount++;
    	    }
    	    //System.out.println(TotalLineCount);
    	    ReadPlatInstance = new  ReadPlat2(TotalLineCount);
    	    
    	    //Closing the File and reopening
    	    TotalLineCount=0;
    	    input_line ="ReStarting";
    	    in.close();
    	    file_in.close();
    	    file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);
    	    input_line=in.readLine();
    	    		input_line=in.readLine();
    	    				input_line=in.readLine();
    	    						input_line=in.readLine();
    	    								input_line=in.readLine();
    	    								input_line=in.readLine();
    //my sixth line consists of the first input value 
    	    while((input_line=in.readLine())!=null) 
    	    {	
    	    	
    	    	 	    	
    	    	
    	    	
    	    		    	input_split = input_line.split(",");    	   	
    	    		    	 	    	 
    	    	ReadPlatInstance.setData(TotalLineCount, TotalLineCount,input_split[0].trim(),Double.valueOf(input_split[1].trim()).doubleValue()   )	;    	
    	    		    		    	ReadPlatInstance.fftofyArray[TotalLineCount]=ReadPlatInstance.yArray[TotalLineCount]*Math.cos(360*TotalLineCount*0.001);
    	    	System.out.println("Frequency="+(ReadPlatInstance.xArray[TotalLineCount]/1000)+"Hz\tAmplitude="+ReadPlatInstance.fftofyArray[TotalLineCount]+"g");
    	    	TotalLineCount++;
    	    	 
    	    }
    	    	    	
    	    
    
    		
    	    
    	    	    
    	    //Create a scrollbar using JScrollPane and add panel into it's viewport
    	    //Set vertical and horizontal scrollbar always show
    	    //////
    
    	    JFrame f=new JFrame("ReadPlatInstance");//////
    	    JScrollPane spp=new JScrollPane();
    	    JTextPane jtp=new JTextPane();
    	    
    	    ReadPlatInstance.setPreferredSize(new Dimension(TotalLineCount,71));
    	    spp.setViewportView(ReadPlatInstance);
    	    
    	    spp.setVisible(true);
    	    spp.setPreferredSize(new Dimension(1350,650/*718*/));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(spp);
            
            f.pack();
                     f.setVisible(true);
    	    in.close();
    	    file_in.close();
    		
    	    
    	    
    	    
    	    
    	    
    	    
    	}
    	
    }
    this is my argument file Seat130APG11.csv.arff

    regards
    dhilip]
    Last edited by noobplus; 04-07-2012 at 10:59 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: error during changing the color of paintComponent of my JPanel

    Java Code:
    if(!this.zero[i].contains("inf")) g2.setPaint(Color.green.darker());
    Is this line 98?

    If so you have to begin by finding out which variable or expression is null. You are probably getting the NullPointerException because you use [] or . with an expression that is null. The chief suspects would have to be zero and zero[i]. You can check out what's happening with System.out.println().

    Java Code:
    g2.setPaint(Color.green.darker());
    System.out.println("About to start for loop, zero=" + zero);
    for(int i = 0; i < this.yArray.length-1; i++) {
        System.out.println("i=" + i + ": zero[i]=" + zero[i]);
        if(!this.zero[i].contains("inf")) g2.setPaint(Color.green.darker());
    Once you have determined which expression is null you have to go back through your code to where you thought you had given it a non null value and figure out why that didn't happen.
    noobplus likes this.

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: error during changing the color of paintComponent of my JPanel

    Quote Originally Posted by pbrockway2 View Post
    Is this line 98?
    sorry yep it is line 98. I edited some spaces in this editor..

    Actually, I'd tried using the System.out.println() .
    Java Code:
    for(int i = 0; i < this.yArray.length-1; i++) 
      { 
        if(!this.zero[i].contains("inf")) 
            {
                System.out.println("inf NOT found"+zero[i]);
                g2.setPaint(Color.green.darker()); 
            }
       else
            {
                 System.out.println("int found"+zero[i]);
                 g2.setPaint(Color.red.darker());
             }
          g2.draw(new Line2D( // my arrayvalues);
    }
    and the output,
    everything got printed right, and after the zero[] array gets printed with the message (found or not found) the run-time error gets displayed :(

    regards
    dhilip
    Last edited by noobplus; 04-07-2012 at 11:16 AM.

  6. #6
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: error during changing the color of paintComponent of my JPanel

    Hi,
    power-cut time in my city (chennai,India).
    be right back in 2 hours..power cut for next 2 hours

    regards
    dhilip

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: error during changing the color of paintComponent of my JPanel

    Maybe you are getting output one time through the loop then zero[i] is null the *next* time. The debugging code I posted was supposed to isolate the value of i associated with a null value of zero[i].

    I'll be gone before you're back online, but good luck and I'll check back tomorrow.
    noobplus likes this.

  8. #8
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: error during changing the color of paintComponent of my JPanel

    Quote Originally Posted by pbrockway2 View Post
    good luck
    I added lines 186 - 191 of my post#3 recently 2 days back(for skipping the first six lines of my data file), which I forgot to add the lines skipping at checking the number of lines and making them the arrays' size (before line 171) and now I've got it into my brain, using your
    Java Code:
    System.out.println("i=" + i + ": zero[i]=" + zero[i]);
    (the last 5 values were null in that output)
    now it's running good

    -thx for the power of good luck-
    dhilip

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: error during changing the color of paintComponent of my JPanel

    You're welcome - I'm glad you've got it sorted out.
    noobplus likes this.

Similar Threads

  1. JPanel paintComponent, OS differences
    By hvince95 in forum Java 2D
    Replies: 2
    Last Post: 02-13-2012, 06:01 AM
  2. Replies: 5
    Last Post: 10-02-2011, 03:55 AM
  3. JPanel PaintComponent
    By capiono in forum AWT / Swing
    Replies: 5
    Last Post: 10-31-2010, 03:36 AM
  4. paint vs paintComponent for a JPanel
    By lightstream in forum AWT / Swing
    Replies: 4
    Last Post: 01-29-2009, 03:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •