import java.io.*;
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.File;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
public class RectangleMouseover extends JFrame {
public Color color;
public int height;
public int fixvalue1;
public int fixvalue2;
public int width;
public String text;
private JPanel paintPanel;
public RectangleMouseover(){
}
public RectangleMouseover(int height, int fixvalue1, int width, int fixvalue2, Color c,String text) {
this.color = c;
this.height = height;
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 height;
public int width;
public String f[];
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 height = 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(height, 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) {
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.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;
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RectangleMouseover().setVisible(true);
}
});
}
public Color getColor(){
return color;
}
public String toString() {
return String.format("Color=%s,top=%d,bottom=%d,width=%d", color.toString(), height, fixvalue1, width, fixvalue2, text);
}
}