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