Hi,
I am trying to do something like this:-
1 10 20 30
##########|##########|##########
rect 1 rect2 rect3
5 ########### 15
user rect - (transparent and temporary based on user mousedrag)
Now If a user clicks suppose in the middle of rect1 and drags it to the middle of rect2. I want to create a transparent rectangle or just a simple rectangle from the start point to the end point. Then it should print the values 5 and 15 in line and compared to the original rectangles.
Here is my input file:-
1 50 993 Andrew
51 100 399 Peacock
101 143 993 John
144 155 333 Mathew
156 200 993 David
201 235 300 Peter
236 256 993 Austin
Here is my code so far:-
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.FileNotFoundException;
import java.io.BufferedReader;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
public class Demo extends JFrame {
public Color color;
public int height;
public int fixvalue1;
public int fixvalue2;
public int width;
public String text;
private JPanel paintPanel;
public Demo(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(1000, 200));
paintPanel = new PaintPanel();
getContentPane().add(paintPanel, BorderLayout.CENTER);
pack();
}
class PaintPanel extends JPanel implements MouseMotionListener {
public List<Glyph> glyphs;
public int height;
public int width;
public String f[];
private final static int NUM_FIELDS = 4;
BufferedImage image;
Graphics2D g2d;
Point startPoint = null;
Point endPoint = null;
private int xMin;
private int xMax;
private int yMin;
private int yMax;
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[3];
Color color = new Color(Integer.parseInt(f[2]));
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);
}
if (image == null)
{
createEmptyImage();
}
g.drawImage(image, 0, 0, null);
// Paint the Rectangle as the mouse is being dragged
if (startPoint != null && endPoint != null)
{
int x = Math.min(startPoint.x, endPoint.x);
int y = Math.min(startPoint.y, endPoint.y);
int width = Math.abs(startPoint.x - endPoint.x);
int height = Math.abs(startPoint.y - endPoint.y);
g.drawRect(x, y, width, height);
}
}
private void createEmptyImage()
{
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
g2d = (Graphics2D)image.getGraphics();
g2d.setColor(Color.BLACK);
g2d.drawString("Add a rectangle by doing mouse press, drag and release!", 40, 15);
}
public void clear()
{
createEmptyImage();
repaint();
}
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
xMin = startPoint.x;
xMax = startPoint.x;
yMin = startPoint.y;
yMax = startPoint.y;
}
public void mouseDragged(MouseEvent e) {
endPoint = e.getPoint();
xMin = Math.min(xMin, endPoint.x);
xMax = Math.max(xMax, endPoint.x);
yMin = Math.min(yMin, endPoint.y);
yMax = Math.max(yMax, endPoint.y);
repaint(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1);
}
public void mouseReleased(MouseEvent e) {
int x = Math.min(startPoint.x, endPoint.x);
int y = Math.min(startPoint.y, endPoint.y);
int width = Math.abs(startPoint.x - endPoint.x);
int height = Math.abs(startPoint.y - endPoint.y);
g2d.setColor( e.getComponent().getForeground() );
g2d.drawRect(x, y, width, height);
startPoint = null;
}
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 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;
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.setPaint(paint);
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()),
(int)(bounds.getY()));
}
}
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 Demo(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(), height, fixvalue1, width, fixvalue2, text);
}
}
Thanks in advance and need guidance.