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);

}

}

}

[4823 byte] By [cheese2005a] at [2007-11-26 13:11:45]
# 1

> ******************************************

> 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 you have to restore my vintage automobile so that it's like new again. For free too.

warnerjaa at 2007-7-7 17:27:46 > top of Java-index,Java Essentials,New To Java...
# 2
this is for a grade and i really need help please.
cheese2005a at 2007-7-7 17:27:46 > top of Java-index,Java Essentials,New To Java...
# 3
your grade or my grade?if its your grade, maybe you should do it yourself.
mkoryaka at 2007-7-7 17:27:46 > top of Java-index,Java Essentials,New To Java...
# 4
I am stuck please help, this will differ from my B to an A
cheese2005a at 2007-7-7 17:27:46 > top of Java-index,Java Essentials,New To Java...
# 5

ok, no problem, i actually have a project that i havent had time to work on in advanced networking, it has to do with using JIST/Swans to simulate a MANET which runs a distributed traffic optimization system.

i got some code, but its due today and i am at work so i dont think i can get it done.

if you do it for me, ill do yours.

mine might be a difference between a B and a D, so its more important that it gets done.

mkoryaka at 2007-7-7 17:27:46 > top of Java-index,Java Essentials,New To Java...