import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.Polygon;
import java.lang.Integer;
import java.util.ArrayList;
import javax.swing.JTextArea;
public class CanvasTextPanel extends JTextArea implements MouseMotionListener, MouseListener{
private ArrayList<ColoredShape> shapes;
private int drawMode;
private int initX1;
private int initY1;
private int initX2;
private int initY2;
private int xPol, yPol;
private int length;
private boolean polygonBuffer;
private ColoredShape currentShape;
protected final static int LINE=1,SQUARE=2,OVAL=3,POLYGON=4,ROUND_RECT=5,FREE_HAND=6,SHADOW=7,
SOLID_SQUARE=22, SOLID_OVAL=33, SOLID_POLYGON=44, TEXT=0,
SOLID_ROUND_RECT=55;
public CanvasTextPanel() {
shapes=new ArrayList<ColoredShape>();
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setBackground(Color.white);
this.drawMode=0;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.6f));
for(ColoredShape s: shapes) {
g2.setColor(s.getColor());
if(s.isDrawShadow()) {
drawShadow(s,g2);
}
g2.setColor(s.getColor());
if(s.isFilled()) {
g2.fill(s.getShape());
} else {
g2.draw(s.getShape());
}
}
}
public void drawShadow(ColoredShape s, Graphics2D g2)
{
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Paint orig = g2.getPaint();
AffineTransform origAt=g2.getTransform();
Composite comp=g2.getComposite();
AffineTransform at = AffineTransform.getTranslateInstance(3.0,2.0);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.6f));
g2.transform(at);
g2.setPaint(Color.black.brighter());
if(s.isFilled()) {
g2.fill(s.getShape());
} else {
g2.draw(s.getShape());
}
g2.setPaint(orig);
g2.setComposite(comp);
g2.setTransform(origAt);
}
private ColoredShape drawShape() {
ColoredShape s=new ColoredShape();
int curX=initX1;
int curY=initY1;
if(initX1>initX2) {
int temp=initX2;
initX2=initX1;
curX=temp;
}
if(initY1>initY2) {
int temp=initY2;
initY2=initY1;
curY=temp;
}
float width=initX2-curX;
float height=initY2-curY;
System.out.println(width+" "+height);
switch(this.drawMode) {
case CanvasTextPanel.LINE:
Line2D.Float line;
line=new Line2D.Float(initX1,initY1,initX2, initY2);
s.setShape(line);
s.setFilled(false);
break;
case CanvasTextPanel.SQUARE:
Rectangle2D.Float rect;
rect=new Rectangle2D.Float(curX,curY,width,height);
s.setShape(rect);
s.setFilled(false);
break;
case CanvasTextPanel.SOLID_SQUARE:
Rectangle2D.Float rectFill;
rectFill=new Rectangle2D.Float(curX,curY,width,height);
s.setShape(rectFill);
s.setFilled(true);
break;
case CanvasTextPanel.OVAL:
Ellipse2D.Float circ=new Ellipse2D.Float(curX,curY,width,height);
s.setShape(circ);
s.setFilled(false);
break;
case CanvasTextPanel.SOLID_OVAL:
Ellipse2D.Float circFill=new Ellipse2D.Float(curX,curY,width,height);
s.setShape(circFill);
s.setFilled(true);
break;
case CanvasTextPanel.ROUND_RECT:
RoundRectangle2D.Float rndRect=new RoundRectangle2D.Float(curX,curY,width,height,25,25);
s.setShape(rndRect);
s.setFilled(false);
break;
case CanvasTextPanel.SOLID_ROUND_RECT:
RoundRectangle2D.Float rndRectFill=new RoundRectangle2D.Float(curX,curY,width,height,25,25);
s.setShape(rndRectFill);
s.setFilled(true);
break;
case CanvasTextPanel.POLYGON:
int xPoint[] = new int[xPol.length()];
int yPoint[] = new int[yPol.length()];
for(int count=0;count<xPoint.length;count++)
{
xPoint[count] = ((int)(xPol.elementAt(count))).intValue();
yPoint[count] = ((int)(yPol.elementAt(count))).intValue();
}
Polygon polygon = new Polygon(xPoint, yPoint, xPoint.length);
polygonBuffer=true;
s.setShape(polygon);
s.setFilled(true);
break;
}
s.setColor(this.getForeground());
s.setDrawShadow(false);
return s;
}
public void setDrawMode(int mode) {
System.out.println(mode);
this.drawMode=mode;
}
public Color getForeGroundColor() {
return this.getForeground();
}
public void setForeGroundColor(Color c) {
this.setForeground(c);
}
public void setBackGroundColor(Color c) {
this.setBackground(c);
}
public void setSolidMode(boolean b) {}
public void flushPolygonBuffer() {}
public boolean isExistPolygonBuffer() {
return true;
}
public void clearCanvas() {
this.shapes.clear();
}
/*
* Implement MouseListener and MouseMotionListener
*/
public void mouseDragged(MouseEvent e) {
if(drawMode==CanvasTextPanel.TEXT) {
super.processMouseMotionEvent(e);
return;
}
initX2=e.getX();
initY2=e.getY();
if(currentShape!=null) {
this.shapes.set(this.shapes.size()-1, drawShape());
}
repaint();
}
public void mousePressed(MouseEvent e) {
if(drawMode==CanvasTextPanel.TEXT) {
super.processMouseEvent(e);
return;
}
if(drawMode!=CanvasTextPanel.POLYGON||drawMode!=CanvasTextPanel.SOLID_POLYGON||drawMode!=CanvasTextPanel.FREE_HAND) {
//reset our position markers
initX1=e.getX();
initY1=e.getY();
initX2=e.getX();
initY2=e.getY();
currentShape=this.drawShape();
shapes.add(currentShape);
repaint();
}
}
public void mouseReleased(MouseEvent e) {
if(drawMode==CanvasTextPanel.TEXT) {
super.processMouseEvent(e);
return;
}
currentShape=null;
repaint();
}
public void mouseClicked(MouseEvent e) {
if(drawMode==CanvasTextPanel.TEXT) {
super.processMouseEvent(e);
return;
}
//if (e.getSource()==shadowBtn) {
for(int i=shapes.size()-1;i>=0;i--) {
ColoredShape s=shapes.get(i);
if(s.getShape().contains(e.getPoint())) {
s.setDrawShadow(!s.isDrawShadow());
i=-1;
//}
}
}
}
public void mouseMoved(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
/**
* A class to associate a Shape, the colour to draw it and whether the Shape has a shadow
* @author tbanks
*
*/
private class ColoredShape {
private Shape shape;
private Color color;
private boolean drawShadow;
private boolean filled;
public ColoredShape() {
}
public ColoredShape(Shape s, Color c, boolean shadowed, boolean filled) {
this.shape=s;
this.color=c;
this.drawShadow=shadowed;
this.filled=filled;
}
public void setShape(Shape s) {
this.shape=s;
}
public Shape getShape() {
return shape;
}
public void setColor(Color c) {
this.color=c;
}
public Color getColor() {
return color;
}
public void setDrawShadow(boolean b) {
this.drawShadow=b;
}
public boolean isDrawShadow() {
return this.drawShadow;
}
public void setFilled(boolean f) {
this.filled=f;
}
public boolean isFilled() {
return this.filled;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("Shape: "+this.shape+"\n");
sb.append("Color: "+this.color+"\n");
sb.append("Draw Shadow: "+this.drawShadow+"\n");
sb.append("Fill Shape: "+this.filled+"\n");
return sb.toString();
}
}
}
canvasText\CanvasTextPanel.java:154: int cannot be dereferenced
int xPoint[] = new int[xPol.length()];
^
canvasText\CanvasTextPanel.java:155: int cannot be dereferenced
int yPoint[] = new int[yPol.length()];
^
canvasText\CanvasTextPanel.java:160: int cannot be dereferenced
xPoint[count] = ((int)(xPol.elementAt(count))).intValue();
^
canvasText\CanvasTextPanel.java:160: int cannot be dereferenced
xPoint[count] = ((int)(xPol.elementAt(count))).intValue();
^
canvasText\CanvasTextPanel.java:161: int cannot be dereferenced
yPoint[count] = ((int)(yPol.elementAt(count))).intValue();
^
canvasText\CanvasTextPanel.java:161: int cannot be dereferenced
yPoint[count] = ((int)(yPol.elementAt(count))).intValue();
^
6 errors
Process completed.
as u now,this a code tht u gave to me.i just want to add polygon..i dont know where are the problems come from..i hope u can help me..anyway, thanx a lot travis..