package Kucchi;
public class ToolNum{
static int ToolNum;
}
Next comes the ImageFrame.java
package Kucchi;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import Kucchi.ToolNum;
public class ImageFrame extends JFrame{
public BufferedImage image;
public Graphics g,g1;
public Graphics2D g2;
private BasicStroke dashStroke;
int height,width;
public Point2D pointstart,pointend;
private boolean flag = true;
int startx,starty,endx,endy;
/** Creates a new instance of ImageFrame */
public ImageFrame(BufferedImage img,String f_name) {
super(f_name);
image = img;
g = image.createGraphics();
float []dash = {1.5f};
dashStroke = new BasicStroke(2.0f,BasicStroke.CAP_BUTT,BasicStroke.
JOIN_MITER,10.0f,dash,0.0f);
this.getContentPane().add(new ImageFrame.ImagePanel(),new BorderLayout().CENTER);
}
public class ImagePanel extends JPanel implements MouseListener,MouseMotionListener{
public ImagePanel(){
addMouseListener(this);
addMouseMotionListener(this);
}
/**
* Paints the container. This forwards the paint to any lightweight
* components that are children of this container. If this method is
* reimplemented, super.paint(g) should be called so that lightweight
* components are properly rendered. If a child component is entirely
* clipped by the current clipping setting in g, paint() will not be
* forwarded to that child.
*
* @param g the specified Graphics window
* @see Component#update(Graphics)
*
*/
public void paint(Graphics g) {
g.drawImage(image,0,0,null);
if (g != null && pointstart != null && pointend != null){
startx = (int)pointstart.getX();
starty = (int)pointstart.getY();
endx = (int)pointend.getX();
endy = (int)pointend.getY();
if(ToolNum.ToolNum != 1){
if(startx > endx){
int swap = endx;
endx = startx;
startx = swap;
}
if(starty > endy){
int swap = endy;
endy = starty;
starty = swap;
}
}
switch(ToolNum.ToolNum){
case 1:
g.drawLine(startx,starty,endx,endy);
break;
case 2:
g.drawOval(startx,starty,endx-startx,endy-starty);
break;
case 3:
g.drawRect(startx,starty,endx-startx,endy-starty);
break;
case 11:
g2 = (Graphics2D)g.create();
g2.setStroke(dashStroke);
g2.drawRect(startx,starty,endx-startx,endy-starty);
g2.dispose();
break;
//similarly for other shapes
}
}
}
public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
}
public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
}
public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
}
public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
pointstart = mouseEvent.getPoint();
}
public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
pointend = mouseEvent.getPoint();
if(ToolNum.ToolNum != 11){
update(g);
}
repaint();
}
public void mouseDragged(java.awt.event.MouseEvent mouseEvent) {
}
public void mouseMoved(java.awt.event.MouseEvent mouseEvent) {
}
}
}
Next: The Entry Point >>
More Java Articles
More By A.P.Rajshekhar