GeeCON Prague 2014*
The moose likes Applets and the fly likes Problemwith freehand drawing on image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Applets
Bookmark "Problemwith freehand drawing on image" Watch "Problemwith freehand drawing on image" New topic
Author

Problemwith freehand drawing on image

Shweta Tripathi
Greenhorn

Joined: Oct 08, 2004
Posts: 1
as soon as i click on the image for freehand drawing, the whole window that us all the panels etc get drawn at the back again, plz solve my prob.
there is also prob with saving the buffered image as jpg image,i don't understand what the prob is as the code seems to be alright.
plz solve my prob
plz doit ASAP
my whole code is as follows:



import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.net.URL;
import javax.swing.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;
import javax.swing.border.*;
import java.applet.*;


public class newdraw extends JApplet
{
Image fimg;
Image imimg;
MediaTracker tracker;
DrawingCanvas canvas;


public void init()
{
JPanel colorpanel = new JPanel(new FlowLayout());
JPanel menupanel = new JPanel(new FlowLayout());
JPanel toolspanel = new JPanel(new GridLayout(10, 1));
JPanel drawpanel = new JPanel(new BorderLayout());
ButtonGroup actionButtonGroup = new ButtonGroup();

String s = getParameter("imagename");
if (s != null)
{
fimg = getImage(getDocumentBase(), s);
tracker = new MediaTracker(this);
tracker.addImage(fimg, 0);

try
{
tracker.waitForID(0);
}
catch(Exception e)
{ }
}

canvas = new DrawingCanvas(fimg.getWidth(this), fimg.getHeight(this), fimg);

JToggleButton red = new JToggleButton("Red");
JToggleButton green = new JToggleButton("Green");
JToggleButton blue = new JToggleButton("Blue");
JToggleButton white = new JToggleButton("White");
JToggleButton black = new JToggleButton("Black");
JToggleButton orange = new JToggleButton("Orange");
JToggleButton magenta = new JToggleButton("Magenta");
JToggleButton purple = new JToggleButton("Purple");
JToggleButton brown = new JToggleButton("Brown");
JToggleButton maroon = new JToggleButton("Maroon");

colorpanel.add(red);
colorpanel.add(green);
colorpanel.add(blue);
colorpanel.add(white);
colorpanel.add(black);
colorpanel.add(orange);
colorpanel.add(magenta);
colorpanel.add(purple);
colorpanel.add(brown);
colorpanel.add(maroon);

JToggleButton save = new JToggleButton("Save As");
save.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile_actionPerformed(e);
}
});

JToggleButton line = new JToggleButton("Line");
line.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
line_actionPerformed(e);
}
});

JToggleButton normal = new JToggleButton("Normal");
normal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
normal_actionPerformed(e);
}
});

JToggleButton rect = new JToggleButton("Rectangle");
rect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
rect_actionPerformed(e);
}
});

JToggleButton oval = new JToggleButton("Oval");
oval.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
oval_actionPerformed(e);
}
});

JToggleButton frect = new JToggleButton("Fill Rectangle");
frect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
frect_actionPerformed(e);
}
});

JToggleButton foval = new JToggleButton("Fill Oval");
foval.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
foval_actionPerformed(e);
}
});
JToggleButton sarea = new JToggleButton("Select Area");
sarea.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
sarea_actionPerformed(e);
}
});

JToggleButton rotate = new JToggleButton("Rotate");
rotate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rotate_actionPerformed(e);
}
});

JToggleButton addtext = new JToggleButton("Add Text");
addtext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
addtext_actionPerformed(e);
}
});

JToggleButton imimage = new JToggleButton("Import Image");
imimage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
imimage_actionPerformed(e);
}
});

toolspanel.add(normal);
toolspanel.add(line);
toolspanel.add(rect);
toolspanel.add(oval);
toolspanel.add(frect);
toolspanel.add(foval);
toolspanel.add(sarea);
toolspanel.add(addtext);
toolspanel.add(rotate);

menupanel.add(imimage);
menupanel.add(save);

actionButtonGroup.add(normal);
actionButtonGroup.add(line);
actionButtonGroup.add(rect);
actionButtonGroup.add(frect);
actionButtonGroup.add(oval);
actionButtonGroup.add(foval);
actionButtonGroup.add(sarea);
actionButtonGroup.add(addtext);
actionButtonGroup.add(rotate);
actionButtonGroup.add(imimage);
actionButtonGroup.add(save);

drawpanel.add(canvas, BorderLayout.CENTER);

///////////////////////////////////////////////////////////////////---add action listeners-------////////////////////////////////////////////////////////////////////////////////

getContentPane().add(menupanel, BorderLayout.NORTH);
getContentPane().add(toolspanel, BorderLayout.WEST);
getContentPane().add(colorpanel, BorderLayout.SOUTH);
getContentPane().add(drawpanel, BorderLayout.CENTER);


}

void line_actionPerformed(ActionEvent e) {
canvas.setMode(DrawingCanvas.MODE_LINE);
}

void normal_actionPerformed(ActionEvent e) {
canvas.setMode(DrawingCanvas.MODE_NORMAL);
}

void rect_actionPerformed(ActionEvent e) {
canvas.setMode(DrawingCanvas.MODE_DRAWRECT);
}

void frect_actionPerformed(ActionEvent e) {
canvas.setMode(DrawingCanvas.MODE_FILLRECT);
}

void oval_actionPerformed(ActionEvent e) {
canvas.setMode(DrawingCanvas.MODE_DRAWOVAL);
}

void foval_actionPerformed(ActionEvent e) {
canvas.setMode(DrawingCanvas.MODE_FILLOVAL);
}

void sarea_actionPerformed(ActionEvent e) {
canvas.setMode(DrawingCanvas.MODE_SELECT);
}

void rotate_actionPerformed(ActionEvent e)
{
Object[] possibleValues = { "45", "90", "135", "180", "225", "270", "315" };
Object selectedValue = JOptionPane.showInputDialog(canvas, "Choose_angle", "Rotate_Angle",
JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
if (selectedValue != null)
{
int angle = Integer.parseInt((String)selectedValue);
canvas.rotate(angle);
}
}

void addtext_actionPerformed(ActionEvent e) {
Object selectedValue = JOptionPane.showInputDialog(canvas, "Enter Text");
if (selectedValue != null)
{
String text = (String)selectedValue;
canvas.setString(text);
System.out.println(text);
}
canvas.setMode(DrawingCanvas.MODE_ADDTEXT);
}

void imimage_actionPerformed(ActionEvent e) {

//JFileChooser chooser = new JFileChooser(".");

imimg = getImage(getDocumentBase(), "welcome.jpg");
tracker = new MediaTracker(this);
tracker.addImage(imimg, 0);

try
{
tracker.waitForID(0);
}
catch(Exception ie)
{ }
canvas.importimage(imimg);

canvas.setMode(DrawingCanvas.MODE_IMPORT);
}

void saveFile_actionPerformed(ActionEvent e)
{
canvas.savefile();
}




}

/////////////////////////////////////////////////////////////////////////Class DrawingCanvas Started/////////////////////////////////////////////////////////////////
class DrawingCanvas extends JComponent implements MouseListener, MouseMotionListener
{
public static final int MODE_NORMAL = 0;
public static final int MODE_LINE = 1;
public static final int MODE_DRAWRECT = 2;
public static final int MODE_FILLRECT = 3;
public static final int MODE_DRAWOVAL = 4;
public static final int MODE_FILLOVAL = 5;
public static final int MODE_SELECT = 6;
public static final int MODE_ADDTEXT = 7;
public static final int MODE_IMPORT = 8;

private BufferedImage buffer;
private Dimension oldSize;
private Rectangle selectedArea;
private int lastX, lastY;
private int currentMode = MODE_NORMAL;
private Stroke stroke;
Image img;
Image img1;
Toolkit toolkit;
URL url;
String str;
String re;

public DrawingCanvas(int width, int height, Image im)
{
setPreferredSize(new Dimension(width, height));
setBackground(Color.white);
setForeground(Color.black);
setStrokeSize(1);
setOpaque(true);
addMouseListener(this);
addMouseMotionListener(this);
img = im;
}

public void mouseClicked(MouseEvent e)
{ }

public void mouseEntered(MouseEvent e)
{ }

public void mouseExited(MouseEvent e)
{ }

public void mouseMoved(MouseEvent e)
{ }

public void mousePressed(MouseEvent e)
{

lastX = e.getX();
lastY = e.getY();
}

public void mouseDragged(MouseEvent e)
{
Graphics g;

int newX = e.getX();
int newY = e.getY();

switch (currentMode)
{
case MODE_NORMAL:
default:
g = buffer.getGraphics();
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(getForeground());
g2d.setStroke(stroke);
g2.drawLine(lastX, lastY, newX, newY);
lastX = newX;
lastY = newY;
re = "re";
repaint();
g.dispose();
break;
case MODE_LINE:
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.setColor(Color.red);
g.drawLine(lastX, lastY, newX, newY);
g.dispose();
break;
case MODE_DRAWRECT:
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.setColor(Color.red);
g.drawRect(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX), Math.abs(lastY - newY));
g.dispose();
break;
case MODE_FILLRECT:
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.setColor(Color.red);
g.fillRect(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX), Math.abs(lastY - newY));
g.dispose();
break;
case MODE_DRAWOVAL:
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.setColor(Color.red);
g.drawOval(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX), Math.abs(lastY - newY));
g.dispose();
break;
case MODE_FILLOVAL:
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.setColor(Color.red);
g.fillOval(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX), Math.abs(lastY - newY));
g.dispose();
break;
case MODE_SELECT:
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.setColor(Color.red);
g.drawRect(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX), Math.abs(lastY - newY));
g.dispose();
break;
case MODE_ADDTEXT:
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.setColor(Color.red);
g.drawString(str, newX, newY);
g.dispose();
break;
case MODE_IMPORT:
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
//g.setColor(Color.red);
g.drawImage(img1, newX, newY, DrawingCanvas.this);
g.dispose();
break;

}
}

public void mouseReleased(MouseEvent e)
{
Graphics2D g = (Graphics2D)buffer.getGraphics();
g.setColor(getForeground());
g.setStroke(stroke);

int newX = e.getX();
int newY = e.getY();

System.out.println(e.getX() + ", " + e.getY());
switch (currentMode)
{
case MODE_NORMAL:
default:
break;
case MODE_LINE:
g.drawLine(lastX, lastY, newX, newY);
break;
case MODE_DRAWRECT:
g.drawRect(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX), Math.abs(lastY - newY));
break;
case MODE_FILLRECT:
g.fillRect(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX), Math.abs(lastY - newY));
break;
case MODE_DRAWOVAL:
g.drawOval(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX), Math.abs(lastY - newY));
break;
case MODE_FILLOVAL:
g.fillOval(Math.min(lastX, newX), Math.min(lastY, newY),Math.abs(lastX - newX), Math.abs(lastY - newY));
break;
case MODE_SELECT:
selectedArea = new Rectangle(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX), Math.abs(lastY - newY));
break;
case MODE_ADDTEXT:
// g.drawString(str, newX, newY);
break;
case MODE_IMPORT:

g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.drawImage(img1, newX, newY, DrawingCanvas.this);
break;

}
// +1 width/height to clear temporary bounding rect
repaint(Math.min(lastX, newX), Math.min(lastY, newY), Math.abs(lastX - newX)+1, Math.abs(lastY - newY)+1);
g.dispose();
}

public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;

if (oldSize == null)
{
oldSize = getSize();
g2d.setBackground(getBackground());
g2d.clearRect(0, 0, oldSize.width, oldSize.height);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
int w = img.getWidth(this);
int h = img.getHeight(this);
System.out.println(w + ", " + h);

buffer = (BufferedImage) createImage(w, h);
Graphics2D big = buffer.createGraphics();
big.drawImage(img, 0, 0, null);
}

g2d.drawImage(buffer, 0, 0, this);
if ((currentMode == MODE_SELECT) && (selectedArea != null))
{
g.setColor(Color.red);
g.drawRect(selectedArea.x, selectedArea.y, selectedArea.width, selectedArea.height);
}
}

public void setStrokeSize(float strokeSize)
{
stroke = new BasicStroke(strokeSize);
}

public void setMode(int mode)
{
this.currentMode = mode;
}

public void clear()
{
oldSize = null;
repaint();
}

public void clearArea()
{
if ((buffer != null) && (selectedArea != null))
{
Graphics g = buffer.getGraphics();
g.setColor(getBackground());
g.fillRect(selectedArea.x, selectedArea.y,
selectedArea.width, selectedArea.height);
// +1 width/height to clear // temporary bounding rect
repaint(selectedArea.x, selectedArea.y, selectedArea.width+1, selectedArea.height+1);
selectedArea = null;
g.dispose();
}
}

public void rotate(int angle)
{
if ((buffer != null) && (selectedArea != null))
{
// Get sub image
BufferedImage subImage = buffer.getSubimage(selectedArea.x, selectedArea.y, selectedArea.width, selectedArea.height);
Graphics2D g = (Graphics2D)buffer.getGraphics();
// Rotate
AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(angle), selectedArea.getCenterX(), selectedArea.getCenterY());
g.setTransform(transform);
g.drawImage(subImage, selectedArea.x, selectedArea.y, this);
selectedArea = null;
g.dispose();
repaint();
}
}

public void setString(String st)
{
str = st;
Graphics g;
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.setColor(Color.red);
g.drawString(str, 40, 40);
g.dispose();
}

public void importimage(Image im)
{
img1 = im;
Graphics g;
g = getGraphics();
g.drawImage(buffer, 0, 0, DrawingCanvas.this);
g.setColor(Color.red);
g.drawImage(img1, 40, 40, DrawingCanvas.this);;
g.dispose();
}

public void savefile()
{
System.out.println("reached1");
try
{
System.out.println("reached2");
//File file = new File("c:/shweta/a1.jpg");
System.out.println("reached3");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("c:/a.jpg"));
System.out.println("reached4");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buffer);
param.setQuality(1.0f, false);
System.out.println("reached5");
encoder.setJPEGEncodeParam(param);
System.out.println("reached6");
encoder.encode(buffer);
System.out.println("reached7");
out.close();
}
catch(Exception e)
{
Graphics g = buffer.getGraphics();
g.setColor(Color.red);
g.drawString("write permissions on images/test.jpg?", 20, 20);
}

}
}

///////////////////////////////////////////////////////////////////////Class DrawingCanvas Finished///////////////////////////////////////////////////////////////////
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
A few changes:
1 — in the paintComponent method

2 — in your switch statements the default option should be the last in the line of case options, eg

3 — typo in this line

under case MODE_NORMAL in mouseDragged method.
[ October 09, 2004: Message edited by: Craig Wood ]
 
GeeCON Prague 2014
 
subject: Problemwith freehand drawing on image