Hi Ezzaral... As per my last problem which is solved. So as I told you that I want to add GradiantPaint. This is the code that I came up with. This code is compling but giving me some runtime errors:. Please find the error report below-

import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.Rectangle;
      import java.awt.event.MouseEvent;
      import java.awt.event.MouseMotionListener;
      import java.util.ArrayList;
      import java.util.List;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import java.io.FileNotFoundException;
      import java.io.BufferedReader;
      import java.awt.GradientPaint;

      public class MouseOverExample extends JFrame {
              public Color color;
              public int start;
              public int fixvalue1;
              public int fixvalue2;
              public int width;
              public String text;
              
              private JPanel paintPanel;
              public MouseOverExample(){
                  
              }
              
      public MouseOverExample(int start, int fixvalue1, int width, int fixvalue2, Color c,String text) {
        this.color = c;
        this.start = start;
        this.fixvalue1 = fixvalue1;
        this.width = width;
        this.fixvalue2 = fixvalue2;
        this.text = text;

      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setMinimumSize(new Dimension(800, 400));
      paintPanel = new PaintPanel();
      getContentPane().add(paintPanel, BorderLayout.CENTER);
      pack();
      }
      class PaintPanel extends JPanel implements MouseMotionListener {
      private List<Glyph> glyphs;
          public int start;
          public int width;
          public String f[];
          private final static int NUM_FIELDS = 4;
          
      public PaintPanel(){
      super();
      addMouseMotionListener(this);
      glyphs = new ArrayList<Glyph>();
              String n = null;  
        try{
            BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
            while((n = fh.readLine()) != null && (n = n.trim()).length() > 0){

                f = n.split("\t");
                int start = Integer.parseInt(f[0].trim());
                int width = Integer.parseInt(f[1].trim());
                String text = f[2];
                Color color = new Color(Integer.parseInt(f[3]));
                int fixvalue1 = 60;
                int fixvalue2 = 27;

                glyphs.add(new Glyph(start, fixvalue1, width, fixvalue2, color, text));

            }
            fh.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
      }
      public void paintComponent(Graphics g) {
      super.paintComponent(g);
      for (Glyph glyph : glyphs){
      glyph.draw(g);
      }
      }
      public void mouseDragged(MouseEvent e) {}
      public void mouseMoved(MouseEvent e) {
      for(Glyph g : glyphs){
      g.showLabel( g.contains(e.getX(), e.getY()) );
      }
      repaint();
      }
      }
      class Glyph {
      private Rectangle bounds;
      private Color color;
      private String label;
      private boolean showLabel = false;
      public Glyph(int x, int y, int width, int height, Color color, String label) {
      MouseOverExample c = new MouseOverExample();    
      GradientPaint bounds = new GradientPaint(x, y ,c.getColor(), 0, 20, Color.white, true);
      //bounds = new Rectangle(x, y, width, height);
      this.color = color;
      this.label = label;
      }
      public void draw(Graphics g){
      Graphics2D g2 = (Graphics2D)g;
      g2.setColor(color);
      g2.draw(bounds);
      if (showLabel){
      g2.setColor(Color.RED);
      int labelWidth = g2.getFontMetrics().stringWidth(label);
      int fontHeight = g2.getFontMetrics().getHeight();
      g2.drawString( label,
      (int)(bounds.getX() + (bounds.getWidth()-labelWidth) / 2),
      (int)(bounds.getY() + (bounds.getHeight()+fontHeight/2) / 2d));
      }
      }
      public boolean contains(int x, int y){
      return bounds.contains(x,y);
      }
      public void showLabel(boolean show){
      showLabel = show;
      }
      }
      public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
      new MouseOverExample(0, 0, 0, 0, null, null).setVisible(true);
      }
      });
      }
    public Color getColor(){
        return color;
    }

    public String toString() {
        return String.format("Color=%s,top=%d,bottom=%d,width=%d", color.toString(), start, fixvalue1, width, fixvalue2, text);
    }
      }
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Colors cannot be null
        at java.awt.GradientPaint.<init>(GradientPaint.java:71)
        at java.awt.GradientPaint.<init>(GradientPaint.java:131)
        at MyPackage.MouseOverExample$Glyph.<init>(MouseOverExample.java:109)
        at MyPackage.MouseOverExample$PaintPanel.<init>(MouseOverExample.java:78)
        at MyPackage.MouseOverExample.<init>(MouseOverExample.java:48)
        at MyPackage.MouseOverExample$1.run(MouseOverExample.java:137)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Can you help me to figure out what is the problem with this ?

Thanks for your guidance

Can you point out which is the line specified here :-

at MyPackage.MouseOverExample$Glyph.<init>(MouseOverExample.java:109)

This line:-

GradientPaint bounds = new GradientPaint(x, y ,c.getColor(), 0, 20, Color.white, true);

Sorry for not including line number. I am still new to forums.
Thanks

These are the lines which are giving me trouble:-

lyphs.add(new Glyph(start, fixvalue1, width, fixvalue2, color, text)); // line 78

paintPanel = new PaintPanel(); //line 48

new MouseOverExample(0, 0, 0, 0, null, null).setVisible(true);

Thanks

This is my input file :-

10 40 Web_Sailor 003
51 60 Ezzaral 000
112 46 DaniWeb 933

No problems, with the line numbers, I appreciate the fact that even though new you even care to use code tags.
The line numbers will come up if you wrap you code inside [code=java] and [/code] tags.

Anyways the problem is as follows:-
the NullPointerException was thrown on the line:-

GradientPaint bounds = new GradientPaint(x, y ,c.getColor(), 0, 20, Color.white, true);

Because c.getColor() returned "null".
If you look at the previous line you have initialized "c" with a new object of class MouseOverExample as shown below.

MouseOverExample c = new MouseOverExample();

But since you have used the default constructor, none of its internal properties are initialized, they are just set to null. As a result of which c.getColor() returns null, and hence the reason for the NullPointerException thrown from the GradientPaint constructor.

Thanks for pointing out. I will work on it and try to make the code work.
Thanks for the question posting tip :)

Thanks

Hi I am stuck here .... I am trying to do something like this in Glyph class:-

PaintPanel zz = new PaintPanel();
      RectangleMouseover  h = zz.glyphs.get(i);
      GradientPaint bounds = new GradientPaint(x, y ,h.getColor(), 0, 20, Color.white, true); }

I actually want to call my getColor() method from RectangleMouseover class but then its not of Glyph type ...... and when I make it Glyph type then I am unable to call getColor() method :(

Is there any solution to come out of this situation ? Will I be able to make GradiantPaint work this way ?

Thanks

This is the full piece of code that I am using. Need some guidance here:-

PaintPanel zz = new PaintPanel();
    for(int i=0; i<zz.glyphs.size(); i++){
      MouseOverExample h = zz.glyphs.get(i);
      GradientPaint bounds = new GradientPaint(x, y ,h.getColor(), 0, 20, Color.white, true); 
}

Thanks

MouseOverExample is just the top level frame class and has nothing at all to do with the Glyphs. It's just a window for the PaintPanel. There is no need for the constructor you're created for it and no need to create additional instances of it in your code.

As far as the GradientPaint, you cannot simply replace the Rectangle instance inside the Glyph class with a GradientPaint and expect everything to work the same. Those are two completely different things. The Rectangle is a Shape, which you are drawing. The GradientPaint is the paint used to render that shape. Here is the Glyph class modified to use a GradientPaint to fill the rectangle.

class Glyph {
    private Rectangle bounds;
    private Color color;
    private Paint paint;
    private String label;
    private boolean showLabel = false;

    public Glyph(int x, int y, int width, int height, Color color, String label) {
        bounds = new Rectangle(x, y, width, height);
        this.color = color;
        // create GradientPaint that runs top to bottom from 'color' to white
        this.paint = new GradientPaint(x, y, color, x, y+height, Color.WHITE);
        this.label = label;
    }

    public void draw(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        //g2.setColor(color);  /** Use setPaint() instead */
        g2.setPaint(paint);
        //g2.draw(bounds); /** Use fill() instead */
        g2.fill(bounds);
        if (showLabel){
            g2.setColor(Color.BLACK);
            int labelWidth = g2.getFontMetrics().stringWidth(label);
            int fontHeight = g2.getFontMetrics().getHeight();
            g2.drawString( label, 
                (int)(bounds.getX() + (bounds.getWidth()-labelWidth) / 2), 
                (int)(bounds.getY() + (bounds.getHeight()+fontHeight/2) / 2d));
        }
    }

    public boolean contains(int x, int y){
        return bounds.contains(x,y);
    }

    public void showLabel(boolean show){
        showLabel = show;
    }
}

ohh.!!! Yes I was making a mistake of using GradiantPaint as something which will do the drawing for me.... but seriously I am loving to play around with this code :)

Thanks for your kind guidance ;)

Great :) It works ... It works :)
Ezzaral thanks a lot for your guidance. You are my GURU :). So here it goes one more solved thread.
Thanks Stephan84s also.

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.