Paint
******************************************
I was wondering if some one could edit this code so that i could be able to pick a pen, earser, and a line tool. it would be like buttons.
first run it and you will see
here is the code
******************************************
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
public class TryPainting extends JApplet implements ActionListener
{
CanvasBACKROUND;
JPanelbuttonPanel;
JButton red, blue, green, black, orange, yellow, pink;
JButton clearButton, upSize, downSize, pen, line, eraser ;
int startX,startY,endX,endY;
public void init()
{
BACKROUND = new Canvas();
BACKROUND.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.blue));
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2,4));
buttonPanel.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.blue));
red= addButton(Color.red);
blue= addButton(Color.blue);
green = addButton(Color.green);
black = addButton(Color.black);
orange = addButton(Color.orange);
pink = addButton(Color.pink);
yellow = addButton(Color.yellow);
clearButton = addButton(Color.black);
clearButton.setText("Clear");
downSize= addButton(null);
downSize.setText("-");
upSize= addButton(null);
upSize.setText("+");
pen = addButton(Color.black);
pen.setText("Pen");
line = addButton(Color.black);
line.setText("Line");
eraser = addButton(Color.black);
eraser.setText("Eraser");
getContentPane().add("Center",BACKROUND);
getContentPane().add("South",buttonPanel);
}
private JButton addButton(Color color)
{
JButton but = new JButton();
but.setBackground(new Color(230,240,250));
but.setBorder(BorderFactory.createEtchedBorder());
but.setForeground(color);
but.setText("Color");
buttonPanel.add(but);
but.addActionListener(this);
return(but);
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("Color"))
{
JButton but = (JButton)e.getSource();
BACKROUND.setPaintColor(but.getForeground());
}
if (s.equals("Clear"))
{
BACKROUND.clearPaint();
}
if (s.equals("+"))
{
BACKROUND.increaseBrushSize();
}
if (s.equals("-"))
{
BACKROUND.decreaseBrushSize();
}
if (e.equals("Pen"))
{
BACKROUND.pen();
}
if (e.equals("Line"))
{
BACKROUND.line();
}
if (e.equals("Eraser"))
{
BACKROUND.eraser();
}
}
public class Canvas extends JPanel implements MouseListener, MouseMotionListener
{
Imageimage;
Graphics pag,img;
private Point start, end;
ColorpaintBrushColor;
intbrushSize;
public Canvas()
{
paintBrushColor = Color.black;
setBackground(Color.white);
brushSize = 10;
addMouseListener(this);
addMouseMotionListener(this);
}
public void mousePressed(MouseEvent e)
{
start = new Point(e.getX(), e.getY());
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e) {}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e)
{
end= new Point(e.getX(), e.getY());
pag.fillOval(start.x-(brushSize/2), start.y-(brushSize/2), brushSize,brushSize);
img.fillOval(start.x-(brushSize/2), start.y-(brushSize/2), brushSize,brushSize);
start = end;
}
public void setPaintColor(Color color)
{
paintBrushColor = color;
pag.setColor(paintBrushColor);
img.setColor(paintBrushColor);
}
public void clearPaint()
{
img.setColor(Color.white);
img.fillRect(0,0,getWidth(),getHeight());
repaint();
}
public void increaseBrushSize()
{
brushSize = brushSize+1;
}
public void decreaseBrushSize()
{
brushSize = brushSize-1;
if (brushSize <= 0) brushSize=1;
}
public void pen()
{
}
public void line()
{
}
public void eraser()
{
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (image == null)
{
image = createImage(getWidth(),getHeight());
img= image.getGraphics();
pag= getGraphics();
}
g.drawImage(image,0,0,this);
}
}
}

