can u plz help me with this problem..if so,i send u this progm and hoping that u will review it..
i dont know how can i edit the shape that have been drawn..as u can see in word processor app..where the shape can be editted with their line color,fill color,line style,dash style..here is my code :it consists of 4 classes:
Painter.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class Painter extends JFrame
{
private CanvasPanel canvasPanel;
private ToolButtonPaneltoolButtonPanel;
private ColorButtonPanelcolorButtonPanel;
private Container mainContainer;
private String fileName;
JMenuBar mainBar;
public Painter()
{
super("Drawing Tools");
fileName = null;
mainBar = new JMenuBar();
setJMenuBar(mainBar);
/*-*/
canvasPanel= new CanvasPanel();
toolButtonPanel= new ToolButtonPanel(canvasPanel);
colorButtonPanel = new ColorButtonPanel(canvasPanel);
mainContainer = getContentPane();
mainContainer.add(toolButtonPanel,BorderLayout.NORTH);
mainContainer.add(canvasPanel,BorderLayout.CENTER);
mainContainer.add(colorButtonPanel,BorderLayout.SOUTH);
setSize(600,500);
this.setResizable(false);
setVisible(true);
addWindowListener (
new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
public void windowDeiconified (WindowEvent e)
{
canvasPanel.repaint();
}
public void windowActivated (WindowEvent e)
{
canvasPanel.repaint();
}
}
);
}
/*-*/
public static void main(String args[])
{
Painter application = new Painter();
application.show();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
ToolButtonPanel.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ToolButtonPanel extends JPanel
{
private JButton lineBtn,arrowBtn, squareBtn, ovalBtn, polygonBtn, roundRectBtn, freeHandBtn, LStyleBtn, dashBtn, aStyleBtn, shadeBtn, clearBtn;
private JCheckBox fullChk;
private CanvasPanel canvasPanel;
public ToolButtonPanel(CanvasPanel inCanvasPanel)
{
canvasPanel = inCanvasPanel;
/*-*/
lineBtn= new JButton("",new ImageIcon("lineBtn.gif"));
arrowBtn= new JButton("ARROW");
squareBtn= new JButton("",new ImageIcon("squareBtn.gif"));
roundRectBtn= new JButton("",new ImageIcon("roundRectBtn.gif"));
ovalBtn = new JButton("",new ImageIcon("ovalBtn.gif"));
polygonBtn= new JButton("",new ImageIcon("polygonBtn.gif"));
freeHandBtn= new JButton("",new ImageIcon("freeHandBtn.gif"));
LStyleBtn= new JButton("LINE STYLE");
dashBtn= new JButton("DASH STYLE");
aStyleBtn= new JButton("ARROW STYLE");
shadeBtn= new JButton("SHADOW");
clearBtn= new JButton("",new ImageIcon("clearBtn.gif"));
lineBtn.addActionListener(new ToolButtonListener());
lineBtn.setToolTipText("Line");
arrowBtn.addActionListener(new ToolButtonListener());
arrowBtn.setToolTipText("Arrow");
squareBtn.addActionListener(new ToolButtonListener());
squareBtn.setToolTipText("Retangle");
roundRectBtn.addActionListener(new ToolButtonListener());
roundRectBtn.setToolTipText("Round Rectangle");
ovalBtn.addActionListener(new ToolButtonListener());
ovalBtn.setToolTipText("Oval");
polygonBtn.addActionListener(new ToolButtonListener());
polygonBtn.setToolTipText("Polygon");
freeHandBtn.addActionListener(new ToolButtonListener());
freeHandBtn.setToolTipText("Free Hand");
LStyleBtn.addActionListener(new ToolButtonListener());
LStyleBtn.setToolTipText("Line Style");
dashBtn.addActionListener(new ToolButtonListener());
dashBtn.setToolTipText("Dash Style");
aStyleBtn.addActionListener(new ToolButtonListener());
aStyleBtn.setToolTipText("Arrow Style");
shadeBtn.addActionListener(new ToolButtonListener());
shadeBtn.setToolTipText("Shadow");
clearBtn.addActionListener(new ToolButtonListener());
clearBtn.setToolTipText("Clear Canvas");
/*-*/
fullChk = new JCheckBox("Fill");
fullChk.addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
if(fullChk.isSelected())
canvasPanel.setSolidMode(Boolean.TRUE);
else
canvasPanel.setSolidMode(Boolean.FALSE);
}
}
);
/*-*/
this.setLayout(new GridLayout(1,9)); // 8 Buttons & 1 CheckBox
this.add(lineBtn);
this.add(arrowBtn);
this.add(ovalBtn);
this.add(squareBtn);
this.add(roundRectBtn);
this.add(polygonBtn);
this.add(freeHandBtn);
this.add(LStyleBtn);
this.add(dashBtn);
this.add(aStyleBtn);
this.add(shadeBtn);
this.add(clearBtn);
this.add(fullChk);
}
/*-*/
private class ToolButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(canvasPanel.isExistPolygonBuffer()!= false)
{
canvasPanel.flushPolygonBuffer();
}
if(event.getSource() == lineBtn)
{
canvasPanel.setDrawMode(canvasPanel.LINE);
}
//if(event.getSource() == arrowBtn)
//{
//canvasPanel.setDrawMode(canvasPanel.ARROW);
//}
if(event.getSource() == squareBtn)
{
canvasPanel.setDrawMode(canvasPanel.SQUARE);
}
if(event.getSource() == ovalBtn)
{
canvasPanel.setDrawMode(canvasPanel.OVAL);
}
if(event.getSource() == polygonBtn)
{
canvasPanel.setDrawMode(canvasPanel.POLYGON);
}
if(event.getSource() == roundRectBtn)
{
canvasPanel.setDrawMode(canvasPanel.ROUND_RECT);
}
if(event.getSource() == freeHandBtn)
{
canvasPanel.setDrawMode(canvasPanel.FREE_HAND);
}
//if(event.getSource() == LStyleBtn)
//{
//canvasPanel.setDrawMode(canvasPanel.LINE_STYLE);
//}
//if(event.getSource() == dashBtn)
//{
//canvasPanel.setDrawMode(canvasPanel.DASH_STYLE);
//}
//if(event.getSource() == aStyleBtn)
//{
//canvasPanel.setDrawMode(canvasPanel.ARROW_STYLE);
//if(event.getSource() == shadeBtn)
//{
//canvasPanel.setDrawMode(canvasPanel.SHADOW);
//}
if(event.getSource() == clearBtn)
{
canvasPanel.clearCanvas();
}
}
}
}
ColorButtonPanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ColorButtonPanel extends JPanel
{
private JPanel colorButtonPanel;
private JButton colorBtn,fillBtn;
private JLabel colorLbl,colorbxLbl;
private Color colorbx,fcolor;
private CanvasPanel canvasPanel;
public ColorButtonPanel(CanvasPanel inCanvasPanel)
{
canvasPanel = inCanvasPanel;
colorLbl = new JLabel("");
colorLbl.setOpaque(true);
colorLbl.setBackground(canvasPanel.getForeGroundColor());
colorLbl.setBorder(BorderFactory.createLineBorder(Color.BLACK));
colorBtn = new JButton("Line Color");
colorBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setForeGroundColor();
}
}
);
fillBtn = new JButton("Fill");
fillBtn.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setBackGroundColor();
}
}
);
this.setLayout(new GridLayout(1,2));
this.add(colorBtn);
this.add(colorLbl);
this.add(fillBtn);
}
/*-*/
public void setForeGroundColor()
{
colorbx = JColorChooser.showDialog(null,"Color",colorbx);
if(colorbx!=null)
{
colorLbl.setBackground(colorbx);
canvasPanel.setForeGroundColor(colorbx);
}
}
public void setBackGroundColor()
{
fcolor = JColorChooser.showDialog(null,"Fill Color",fcolor);
if(fcolor!=null)
{
//fcLbl.setBackground(fcolor);
canvasPanel.setBackGroundColor(fcolor);
}
}
}
CanvasPanel.java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;
public class CanvasPanel extends JPanel implements MouseListener,MouseMotionListener, Serializable
{
protected final static int LINE=1,SQUARE=2,OVAL=3,POLYGON=4,ROUND_RECT=5,FREE_HAND=6,
SOLID_SQUARE=22, SOLID_OVAL=33, SOLID_POLYGON=44,
SOLID_ROUND_RECT=55;
protected static Vector vLine,vSquare,vOval,vPolygon,vRoundRect,vFreeHand,
vSolidSquare,vSolidOval,vSolidPolygon,vSolidRoundRect,vFile,
xPolygon, yPolygon;
private Color foreGroundColor, backGroundColor;
private int x1,y1,x2,y2,linex1,linex2,liney1,liney2, drawMode=0;
private boolean solidMode, polygonBuffer;
public CanvasPanel()
{
vLine = new Vector();
vSquare = new Vector();
vOval= new Vector();
vPolygon= new Vector();
vRoundRect= new Vector();
vFreeHand= new Vector();
vSolidSquare= new Vector();
vSolidOval= new Vector();
vSolidPolygon= new Vector();
vSolidRoundRect= new Vector();
vFile= new Vector();
xPolygon= new Vector();
yPolygon= new Vector();
addMouseListener(this);
addMouseMotionListener(this);
solidMode = false;
polygonBuffer = false;
foreGroundColor = Color.BLACK;
backGroundColor = Color.WHITE;
setBackground(backGroundColor);
repaint();
}
/*-*/
public void mousePressed(MouseEvent event)
{
x1 = linex1 = linex2 = event.getX();
y1 = liney1 = liney2 = event.getY();
}
/*-*/
public void mouseClicked(MouseEvent event){}
public void mouseMoved(MouseEvent event){}
/*-*/
public void mouseReleased(MouseEvent event)
{
if (drawMode == LINE)
{
vLine.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
}
if (drawMode == SQUARE)
{
if(solidMode)
{
if(x1 > event.getX() || y1 > event.getY())
{
vSolidSquare.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
}
else
{
vSolidSquare.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
}
}
else
{
if(x1 > event.getX() || y1 > event.getY())
{
vSquare.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
}
else
{
vSquare.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
}
}
}
if (drawMode == this.OVAL)
{
if(solidMode)
{
if(x1 > event.getX() || y1 > event.getY())
{
vSolidOval.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
}
else
{
vSolidOval.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
}
}
else
{
if(x1 > event.getX() || y1 > event.getY())
{
vOval.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
}
else
{
vOval.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
}
}
}
if (drawMode == this.POLYGON || drawMode == this.SOLID_POLYGON)
{
xPolygon.add(new Integer(event.getX()));
yPolygon.add(new Integer(event.getY()));
polygonBuffer = true;
repaint();
}
if (drawMode == this.ROUND_RECT)
{
if(solidMode)
{
if(x1 > event.getX() || y1 > event.getY())
{
vSolidRoundRect.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
}
else
{
vSolidRoundRect.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
}
}
else
{
if(x1 > event.getX() || y1 > event.getY())
{
vRoundRect.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
}
else
{
vRoundRect.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
}
}
}
x1=linex1=x2=linex2=0;
y1=liney1=y2=liney2=0;
}
/*-*/
public void mouseEntered(MouseEvent event)
{
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
/*-*/
public void mouseExited(MouseEvent event)
{
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
/*-*/
public void mouseDragged(MouseEvent event)
{
x2 = event.getX();
y2 = event.getY();
if (drawMode == this.FREE_HAND)
{
linex1 = linex2;
liney1 = liney2;
linex2 = x2;
liney2 = y2;
vFreeHand.add(new Coordinate(linex1,liney1,linex2,liney2,foreGroundColor));
}
repaint();
}
/*-*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
redrawVectorBuffer(g);
g.setColor(foreGroundColor);
if (drawMode == LINE)
{
g.drawLine(x1,y1,x2,y2);
}
if (drawMode == OVAL)
{
if(solidMode)
{
if(x1 > x2 || y1 > y2)
g.fillOval(x2,y2,x1-x2,y1-y2);
else
g.fillOval(x1,y1,x2-x1,y2-y1);
}
else
{
if(x1 > x2 || y1 > y2)
g.drawOval (x2,y2,x1-x2,y1-y2);
else
g.drawOval (x1,y1,x2-x1,y2-y1);
}
}
if (drawMode == ROUND_RECT)
{
if(solidMode)
{
if(x1 > x2 || y1 > y2)
g.fillRoundRect(x2,y2,x1-x2,y1-y2,25,25);
else
g.fillRoundRect(x1,y1,x2-x1,y2-y1,25,25);
}
else
{
if(x1 > x2 || y1 > y2)
g.drawRoundRect(x2,y2,x1-x2,y1-y2,25,25);
else
g.drawRoundRect(x1,y1,x2-x1,y2-y1,25,25);
}
}
if (drawMode == SQUARE)
{
if(solidMode)
{
if(x1 > x2 || y1 > y2)
g.fillRect (x2,y2,x1-x2,y1-y2);
else
g.fillRect (x1,y1,x2-x1,y2-y1);
}
else
{
if(x1 > x2 || y1 > y2)
g.drawRect (x2,y2,x1-x2,y1-y2);
else
g.drawRect (x1,y1,x2-x1,y2-y1);
}
}
if (drawMode == POLYGON || drawMode == SOLID_POLYGON)
{
int xPos[] = new int[xPolygon.size()];
int yPos[] = new int[yPolygon.size()];
for(int count=0;count<xPos.length;count++)
{
xPos[count] = ((Integer)(xPolygon.elementAt(count))).intValue();
yPos[count] = ((Integer)(yPolygon.elementAt(count))).intValue();
}
g.drawPolyline(xPos,yPos,xPos.length);
polygonBuffer = true;
}
if (drawMode == FREE_HAND)
{
g.drawLine(linex1,liney1,linex2,liney2);
}
}
/*-*/
public void setDrawMode(int mode)
{
drawMode = mode;
}
public int getDrawMode()
{
return drawMode;
}
/*-*/
public void setSolidMode(Boolean inSolidMode)
{
solidMode = inSolidMode.booleanValue();
}
public Boolean getSolidMode()
{
return Boolean.valueOf(solidMode);
}
/*-*/
public void setForeGroundColor(Color inputColor)
{
foreGroundColor = inputColor;
}
public Color getForeGroundColor()
{
return foreGroundColor;
}
/*-*/
public void setBackGroundColor(Color inputColor)
{
backGroundColor = inputColor;
this.setBackground(backGroundColor);
}
public Color getBackGroundColor()
{
return backGroundColor;
}
/*-*/
public void clearCanvas()
{
vFreeHand.removeAllElements();
vLine.removeAllElements();
vOval.removeAllElements();
vPolygon.removeAllElements();
vRoundRect.removeAllElements();
vSolidOval.removeAllElements();
vSolidPolygon.removeAllElements();
vSolidRoundRect.removeAllElements();
vSolidSquare.removeAllElements();
vSquare.removeAllElements();
repaint();
}
//this.clearCanvas();
/*-*/
public boolean isExistPolygonBuffer()
{
return polygonBuffer;
}
/*-*/
public void flushPolygonBuffer()
{
if(!solidMode)
{
vPolygon.add(new Coordinate(xPolygon, yPolygon, foreGroundColor));
}
else
{
vSolidPolygon.add(new Coordinate(xPolygon, yPolygon, foreGroundColor));
}
xPolygon.removeAllElements();
yPolygon.removeAllElements();
polygonBuffer = false;
repaint();
}
/*-*/
private class Coordinate implements Serializable
{
private int x1,y1,x2,y2;
private Color foreColor;
private Vector xPoly, yPoly;
public Coordinate (int inx1,int iny1,int inx2, int iny2, Color color)
{
x1 = inx1;
y1 = iny1;
x2 = inx2;
y2 = iny2;
foreColor = color;
}
public Coordinate(Vector inXPolygon, Vector inYPolygon, Color color)
{
xPoly = (Vector)inXPolygon.clone();
yPoly = (Vector)inYPolygon.clone();
foreColor = color;
}
public Color colour()
{
return foreColor;
}
public int getX1 ()
{
return x1;
}
public int getX2 ()
{
return x2;
}
public int getY1 ()
{
return y1;
}
public int getY2 ()
{
return y2;
}
public Vector getXPolygon()
{
return xPoly;
}
public Vector getYPolygon()
{
return yPoly;
}
}
/*-*/
private class StepInfo implements Serializable
{
private int stepType;
private Coordinate stepCoordinate;
public StepInfo(int inStepType, Coordinate inStepCoordinate)
{
stepType = inStepType;
stepCoordinate = inStepCoordinate;
}
public int getStepType()
{
return stepType;
}
public Coordinate getStepCoordinate()
{
return stepCoordinate;
}
}
/*-*/
private RenderedImage myCreateImage()
{
BufferedImage bufferedImage = new BufferedImage(600,390, BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.createGraphics();
redrawVectorBuffer(g);
g.dispose();
return bufferedImage;
}
/*-*/
private void redrawVectorBuffer(Graphics g)
{
for (int i=0;i<vFreeHand.size();i++){
g.setColor(((Coordinate)vFreeHand.elementAt(i)).colour());
g.drawLine(((Coordinate)vFreeHand.elementAt(i)).getX1(),((Coordinate)vFreeHand.elementAt(i)).getY1(),((Coordinate)vFreeHand.elementAt(i)).getX2(),((Coordinate)vFreeHand.elementAt(i)).getY2());
}
for (int i=0;i<vLine.size();i++){
g.setColor(((Coordinate)vLine.elementAt(i)).colour());
g.drawLine(((Coordinate)vLine.elementAt(i)).getX1(),((Coordinate)vLine.elementAt(i)).getY1(),((Coordinate)vLine.elementAt(i)).getX2(),((Coordinate)vLine.elementAt(i)).getY2());
}
for (int i=0;i<vOval.size();i++){
g.setColor(((Coordinate)vOval.elementAt(i)).colour());
g.drawOval(((Coordinate)vOval.elementAt(i)).getX1(),((Coordinate)vOval.elementAt(i)).getY1(),((Coordinate)vOval.elementAt(i)).getX2()-((Coordinate)vOval.elementAt(i)).getX1(),((Coordinate)vOval.elementAt(i)).getY2()-((Coordinate)vOval.elementAt(i)).getY1());
}
for (int i=0;i<vRoundRect.size();i++){
g.setColor(((Coordinate)vRoundRect.elementAt(i)).colour());
g.drawRoundRect(((Coordinate)vRoundRect.elementAt(i)).getX1(),((Coordinate)vRoundRect.elementAt(i)).getY1(),((Coordinate)vRoundRect.elementAt(i)).getX2()-((Coordinate)vRoundRect.elementAt(i)).getX1(),((Coordinate)vRoundRect.elementAt(i)).getY2()-((Coordinate)vRoundRect.elementAt(i)).getY1(),25,25);
}
for (int i=0;i<vSolidOval.size();i++){
g.setColor(((Coordinate)vSolidOval.elementAt(i)).colour());
g.fillOval(((Coordinate)vSolidOval.elementAt(i)).getX1(),((Coordinate)vSolidOval.elementAt(i)).getY1(),((Coordinate)vSolidOval.elementAt(i)).getX2()-((Coordinate)vSolidOval.elementAt(i)).getX1(),((Coordinate)vSolidOval.elementAt(i)).getY2()-((Coordinate)vSolidOval.elementAt(i)).getY1());
}
for (int i=0;i<vSolidRoundRect.size();i++){
g.setColor(((Coordinate)vSolidRoundRect.elementAt(i)).colour());
g.fillRoundRect(((Coordinate)vSolidRoundRect.elementAt(i)).getX1(),((Coordinate)vSolidRoundRect.elementAt(i)).getY1(),((Coordinate)vSolidRoundRect.elementAt(i)).getX2()-((Coordinate)vSolidRoundRect.elementAt(i)).getX1(),((Coordinate)vSolidRoundRect.elementAt(i)).getY2()-((Coordinate)vSolidRoundRect.elementAt(i)).getY1(),25,25);
}
for (int i=0;i<vSquare.size();i++){
g.setColor(((Coordinate)vSquare.elementAt(i)).colour());
g.drawRect(((Coordinate)vSquare.elementAt(i)).getX1(),((Coordinate)vSquare.elementAt(i)).getY1(),((Coordinate)vSquare.elementAt(i)).getX2()-((Coordinate)vSquare.elementAt(i)).getX1(),((Coordinate)vSquare.elementAt(i)).getY2()-((Coordinate)vSquare.elementAt(i)).getY1());
}
for (int i=0;i<vSolidSquare.size();i++){
g.setColor(((Coordinate)vSolidSquare.elementAt(i)).colour());
g.fillRect(((Coordinate)vSolidSquare.elementAt(i)).getX1(),((Coordinate)vSolidSquare.elementAt(i)).getY1(),((Coordinate)vSolidSquare.elementAt(i)).getX2()-((Coordinate)vSolidSquare.elementAt(i)).getX1(),((Coordinate)vSolidSquare.elementAt(i)).getY2()-((Coordinate)vSolidSquare.elementAt(i)).getY1());
}
for(int i=0;i<vPolygon.size();i++){
int xPos[] = new int[((Coordinate)vPolygon.elementAt(i)).getXPolygon().size()];
int yPos[] = new int[((Coordinate)vPolygon.elementAt(i)).getYPolygon().size()];
for(int count=0;count<xPos.length;count++)
{
xPos[count] = ((Integer)((Coordinate)vPolygon.elementAt(i)).getXPolygon().elementAt(count)).intValue();
yPos[count] = ((Integer)((Coordinate)vPolygon.elementAt(i)).getYPolygon().elementAt(count)).intValue();
}
g.setColor(((Coordinate)vPolygon.elementAt(i)).colour());
g.drawPolygon(xPos,yPos,xPos.length);
}
for(int i=0;i<vSolidPolygon.size();i++){
int xPos[] = new int[((Coordinate)vSolidPolygon.elementAt(i)).getXPolygon().size()];
int yPos[] = new int[((Coordinate)vSolidPolygon.elementAt(i)).getYPolygon().size()];
for(int count=0;count<xPos.length;count++)
{
xPos[count] = ((Integer)((Coordinate)vSolidPolygon.elementAt(i)).getXPolygon().elementAt(count)).intValue();
yPos[count] = ((Integer)((Coordinate)vSolidPolygon.elementAt(i)).getYPolygon().elementAt(count)).intValue();
}
g.setColor(((Coordinate)vSolidPolygon.elementAt(i)).colour());
g.fillPolygon(xPos,yPos,xPos.length);
}
}
}
>