BufferImage Help

Hi can someone please help me fix my program? I've been working on it for the longest time and I dont know why the BufferedImage does not work. I'm quite sure I implemented it right. Can someone, anyone please help me out?

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.lang.StringBuffer;

import java.awt.Graphics;

import java.awt.Color;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.*;

publicclass JPanelsextends JFrameimplements MouseListener, MouseMotionListener, KeyListener

{

private Point oldPoint, currentPoint;

private String s;

private FontMetrics fm;

private JButton circBtn, rectBtn, lineBtn, eraseBtn, clearBtn, sendBtn;

JPanel drawingArea =new JPanel();

JPanel jp =new JPanel();

TextField message =new TextField("Draw away! Click anywhere on canvas to type");

int option;

int endx, endy;

int startX, startY;// The location of the mouse when the dragging started.

int mousex, mousey;// The location of the mouse during dragging.

int height, width;

BufferedImage img;

publicvoid init(){

//initialize the "old" point

oldPoint =new Point(0,0);

this.setSize(new Dimension(600, 500));

}

publicstaticvoid main(String[] args){

new JPanels();

}

public JPanels(){

super("Welcome to JPanels!");

Container content = getContentPane();

content.setBackground(Color.lightGray);

circBtn =new JButton("Circle");

rectBtn =new JButton("Rectangle");

lineBtn =new JButton("Line");

eraseBtn =new JButton("Eraser");

clearBtn =new JButton("Clear");

sendBtn =new JButton("Send");

jp.setLayout(new GridLayout(0,6));

jp.add(circBtn,0);

jp.add(rectBtn,1);

jp.add(lineBtn,2);

jp.add(eraseBtn,3);

jp.add(clearBtn,4);

jp.add(sendBtn,5);

message.setBackground(Color.gray);

message.setEditable(false);

message.setFocusable(false);

drawingArea.setPreferredSize(new Dimension(500, 400));

drawingArea.setBorder (BorderFactory.createLineBorder (Color.blue, 2));

drawingArea.setBackground(Color.white);

drawingArea.setFocusable(true);

content.add(jp, BorderLayout.NORTH);

content.add(drawingArea, BorderLayout.CENTER);

content.add(message, BorderLayout.SOUTH);

pack();

this.setVisible(true);

this.setResizable(false);

drawingArea.addMouseListener(this);

drawingArea.addMouseMotionListener(this);

drawingArea.addKeyListener(this);

//ActionPerformed methods for the buttons on the side panel

circBtn.setFocusable(false);

circBtn.setToolTipText("Draw a Circle");

circBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e)

{

circBtn_actionPerformed(e);

}

});

rectBtn.setFocusable(false);

rectBtn.setToolTipText("Draw a Rectangle");

rectBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e)

{

rectBtn_actionPerformed(e);

}

});

lineBtn.setFocusable(false);

lineBtn.setToolTipText("Draw a Line");

lineBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e)

{

lineBtn_actionPerformed(e);

}

});

eraseBtn.setFocusable(false);

eraseBtn.setToolTipText("Eraser");

eraseBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e)

{

eraseBtn_actionPerformed(e);

}

});

clearBtn.setFocusable(false);

clearBtn.setToolTipText("Clear Canvas");

clearBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e)

{

clearBtn_actionPerformed(e);

}

});

sendBtn.setFocusable(false);

sendBtn.setToolTipText("Send drawing to friend");

sendBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e)

{

sendBtn_actionPerformed(e);

}

});

}

privatevoid circBtn_actionPerformed(ActionEvent e)

{

circBtn.requestFocus();

System.out.println("This is the Circle Button test");

// TODO: Add any handling code here

option = 1;

}

privatevoid rectBtn_actionPerformed(ActionEvent e)

{

rectBtn.requestFocus();

System.out.println("This is the Rect Button test");

// TODO: Add any handling code here

drawingArea.requestFocus();

option = 2;

}

privatevoid lineBtn_actionPerformed(ActionEvent e)

{

lineBtn.requestFocus();

System.out.println("This is the Line Button test");

// TODO: Add any handling code here

option = 3;

}

privatevoid eraseBtn_actionPerformed(ActionEvent e)

{

eraseBtn.requestFocus();

System.out.println("This is the Erase Button test");

// TODO: Add any handling code here

option = 4;

}

privatevoid clearBtn_actionPerformed(ActionEvent e)

{

clearBtn.requestFocus();

System.out.println("This is the Clear Button test");

// TODO: Add any handling code here

drawingArea.repaint();

message.setText("Canvas cleared!");

}

privatevoid sendBtn_actionPerformed(ActionEvent e)

{

sendBtn.requestFocus();

System.out.println("This is the Send Button test");

// TODO: Add any handling code here

// img = new BufferedImage(this.getWidth(), this.getHeight(),BufferedImage.TYPE_INT_RGB);

try{

ImageIO.write(img,"bmp",new File("drawingArea"));

ByteArrayOutputStream baos =new ByteArrayOutputStream();

ImageIO.write(img,"png", baos);

byte[] imagebytes = baos.toByteArray();

img = ImageIO.read(new ByteArrayInputStream(imagebytes));

}catch(IOException ioe){

System.out.println(ioe.getMessage());

}

}

//when the mouse is pressed, a point is formed

publicvoid mousePressed(MouseEvent e){

drawingArea.requestFocus();

startX = mousex = e.getX();// Save coords of mouse position.

startY = mousey = e.getY();

oldPoint = e.getPoint();

String msg = ("Mouse Pressed: (" + oldPoint.x +", " + oldPoint.y +")");

System.out.println(msg);

message.setText(msg);

s ="";

}

//mouseDragged will do the line drawing

publicvoid mouseDragged(MouseEvent e){

mousex = e.getX();

mousey = e.getY();

currentPoint = e.getPoint();

String msg ="Mouse Dragged: (" + currentPoint.x +", " + currentPoint.y +")";

System.out.println(msg);

message.setText(msg);

if(option == 1){

Graphics g = img.createGraphics();

drawOval(g);

}

if (option == 2){

Graphics g = img.createGraphics();

drawRect(g);

}

elseif (option == 3){

drawingArea.getGraphics().drawLine(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);

}

elseif (option == 4){

Graphics g = img.createGraphics();

g.setColor(Color.white);

g.fillOval(oldPoint.x,oldPoint.y,20,20);

}

oldPoint = currentPoint;//set the oldPoint to the most recent coordinates

}

publicvoid mouseReleased(MouseEvent e){

endx = e.getX();

endy = e.getY();

System.out.println(endx +" "+endy);

}

publicvoid keyTyped(KeyEvent e){

drawingArea.requestFocus();

s = s + e.getKeyChar();

drawingArea.getGraphics().drawString(s, oldPoint.x, oldPoint.y);

Font font =new Font("Arial", Font.BOLD, 12);

fm = getFontMetrics(font);

}

void drawRect(Graphics g){

int x, y;// Top left corner of the rectangle.

int w, h;// Width and height of the rectangle.

// x,y,w,h must be computed from the coordinates

// of the two corner points.

if (mousex > startX){

x = startX;

w = mousex - startX;

}

else{

x = mousex;

w = startX - mousex;

}

if (mousey > startY){

y = startY;

h = mousey - startY;

}

else{

y = mousey;

h = startY - mousey;

}

g.setColor(Color.white);

g.fillRect(x, y, w, h);

g.setColor(Color.black);

g.drawRect(x, y, w-1, h-1);

}

// end drawRect()

void drawOval(Graphics g){

int x, y;// Top left corner of the rectangle.

int w, h;// Width and height of the rectangle.

// x,y,w,h must be computed from the coordinates

// of the two corner points.

if (endx > startX){

x = startX;

w = endx - startX;

}

else{

x = endx;

w = startX - endx;

}

if (endy > startY){

y = startY;

h = endy - startY;

}

else{

y = endy;

h = startY - endy;

}

g.setColor(Color.white);

g.fillOval(x, y, w, h);

g.fillRect(x,y,w/2,h/2);

g.setColor(Color.black);

g.drawOval(x, y, w-1, h-1);

}

// end drawRect()

//abstract methods promised by the MouseListener and MouseMotionListener interfaces

publicvoid mouseEntered(MouseEvent e){}

publicvoid mouseExited(MouseEvent e){}

//public void mouseReleased(MouseEvent e){}

publicvoid mouseClicked(MouseEvent e){}

publicvoid mouseMoved(MouseEvent e){}

publicvoid keyReleased(KeyEvent e){}

publicvoid keyPressed(KeyEvent e){}

}

[18471 byte] By [Dishala] at [2007-11-27 8:18:09]
# 1

Make arrangements to draw the image in your displayArea.

BufferedImage img;

JPanel drawingArea = new JPanel() {

protected void paintComponent(Graphics g) {

if(img == null) {

img = new BufferedImage(this.getWidth(), this.getHeight(),

BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = img.createGraphics();

g2.setBackground(getBackground());

g2.clearRect(0,0,getWidth(),getHeight());

g2.dispose();

}

g.drawImage(img, 0, 0, this);

}

};

This is an Applet method.

public void init() {

crwooda at 2007-7-12 20:03:37 > top of Java-index,Security,Cryptography...