let me explain you exactly what is working and not, i have 4 buttons , one for drawing rectangles , other for drawing lines , other for clear all the rectangles and lines and other fro drawing text inside rectangles.I draw first rectangles and lines but when i change the button , drawing text, it draws text but when i change the button to dtraw another rectangle then the textarea dissapear and if i press the clear button to clear all , it still remains there...
this is the main class from where i call the rest.
class Rectanglet
declaration
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.TextArea;[/QUOTE]
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea
public class RectangleTest extends FrameSet
{
//Line s;
//Oval o;
Line l;
Rectanglet r;
//Rectangle1 r2;
JTextArea a;
Point startpos, endpos; //Declare the start and end positions
static int check;
Button btnLine, btnRect,btnClear, btnAddText;
static Point pct;
Vector vt = new Vector(); //Vector for storing the shapes
Vector vtLines = new Vector();
Vector vtRect = new Vector();
Vector vtLine = new Vector();
Vector vtText = new Vector();
int i = 0; //Vector index to keep count of elements(i.e.shapes)
int j = 0;
int m = 0;
int n = 0;
int nheight1, nwidth1;
boolean bline = false; //booleans to know which button was
boolean bText = false;
boolean brect = false;
Methods
PHP Code:
public void RecrangleTest()
{
//Do nothing in the constructor of the applet
}
public void init()
{
setLayout(new FlowLayout());
//Create and Add the buttons
btnLine = new Button("Line");
btnAddText = new Button("AddText");
btnRect = new Button("Rectangle");
btnClear = new Button("Clear");
add(btnLine);
add(btnAddText);
add(btnRect);
add(btnClear);
}
public void paint(Graphics g)
{
Boolean drawBool = false;
int count = 0;
//To get a shadow effect
g.setColor(Color.black);
g.fillRect(0,0,size().width,size().height);
g.setColor(new Color(255,255,154));
g.fillRect(1,1,size().width-3,size().height-3);
for (int t = 0; t < vt.size(); t++)
{
//Add the shapes to the vector
Rectanglet sh = (Rectanglet)vt.elementAt(t); //elementAt(i);
sh.Draw(g);
// drawBool=true;
}
/* for(int a=0;a<vtLine.size();a++)
{
Line lines = (Line)vtLine.elementAt(a);
lines.Draw(g);
}
*/
for(int k = 0;k < vtLines.size();k++)
{
Line lines = (Line)vtLines.elementAt(k);
lines.Draw(g);
}
for(int k =0 ;k < vtText.size();k++)
{
JTextArea jtext= (JTextArea)vtText.elementAt(k);
jtext.setVisible(true);
// jtext.paint(g);
}
}
public void repaint2(){
repaint();
for(int k =0 ;k < vtText.size();k++)
{
JTextArea jtext= (JTextArea)vtText.elementAt(k);
jtext.setVisible(true);
// jtext.paint(g);
}
}
@Override
public boolean action(Event e, Object o)
{
//See which button was clicked and
//set flags accordingly
if(e.target.equals(btnAddText))
{
bline = false;
brect = false;
bText = true;
//!!!!!
}
if (e.target.equals(btnLine))
{
bline = true;
brect = false;
bText = false;
}
if (e.target.equals(btnRect))
{
bline = false;
bText = false;
brect = true;
}
if (e.target.equals(btnClear))
{
//Clear the entire drawing screen
//First remove all elements
vt.removeAllElements();
vtLines.removeAllElements();
vtText.removeAllElements();
// then make vector index zero
i =0;
j= 0;
m = 0;
n = 0;
// a.setVisible(false);
// a.removeAll();
//finally, call repaint()
// remove(a);
repaint();
remove(a);
}
repaint2();
return true;
}
Still methods but here are just Mouse events
PHP Code:
public boolean mouseUp(Event evt, int x, int y)
{
//Fianlly the mouse is up indicating shape drawing is over.
//So set these mouseUp coordinates to set the end position.
//Then update the Vector count.
endpos = new Point(x,y);
if(bText)
{
for(int t = 0;t <vt.size();t++)
{
Rectanglet rect = (Rectanglet)vt.elementAt(t);
if((startpos.x - rect.getStart().x > 0)&& (startpos.x - rect.getEnd().x < 0)&&(startpos.y - rect.getEnd().y < 0)&&(startpos.y - rect.getStart().y > 0))
{
// Rectangle rect1 = new Rectangle(rect.getStart().x + 2, rect.getStart().y + 2 , rect.getHeight()/3, rect.getHeight()/3);
JTextArea a = new JTextArea();
// textArea.setBounds(rect.x+rect.width/4, rect.y + rect.width/4, rect.width/2, rect.height/2);
//textArea.setBorder();
// Rectangle rec = new Rectangle(rect.getStart().x + rect.getHeight()/4, rect.getStart().y + rect.getHeight()/4 , rect.getHeight()/2, rect.getHeight()/2);
//a.setBounds(rect1);
a.setBounds(rect.getStart().x + 2, rect.getStart().y + 2 , rect.getHeight()/3, rect.getHeight()/3);
// Rectanglet rect2 = new Rectanglet(rect.getStart().x + 3, rect.getStart().y + 3 , rect.getHeight()/3, rect.getHeight()/3);
a.setLineWrap(true);
a.requestFocusInWindow();
a.setEditable(true);
// a.setVisible(true);
add(a);//za text area doar cand largesti imaginea
a.setCaretColor(Color.white);
a.setVisible(true);
//a.gett
///////vtText.add(m, a.getText());
vtText.add(m, a);
// vtRect.add(n, rect2);
m++;
// n++;
}
}
}
if(bline)
{
for(int s=0;s<vt.size();s++)
{
if(s!=check)
{
Rectanglet rect = (Rectanglet)vt.elementAt(s);
if((endpos.x - rect.getStart().x > 0)&& (endpos.x - rect.getEnd().x < 0)&&(endpos.y - rect.getEnd().y < 0)&&(endpos.y - rect.getStart().y > 0) )
{
// check = s;
//Point newstart = new Point(startpos.x, startpos.y);
// Point drawStop = new Point(endpos.x, endpos.y);
// l.setStart(newstart); de ce setam de 2 ori startul liniei?
l.setEnd(endpos);
vtLines.addElement(l);
l = (Line)vtLines.elementAt(j);
// vtLine.add(j, l);
j++;
}
}
}
}
if (brect)
{
Point drawto = new Point( Math.max(x,startpos.x),Math.max(y,startpos.y));
Point newstart = new Point(Math.min(x,startpos.x),Math.min(y,startpos.y));
Point PointS = newstart;
Point PointF = drawto;
r = (Rectanglet)vt.elementAt(i);
r.setWidth(drawto,newstart);
r.setHeight(drawto, newstart);
r.setEnd(drawto);
r.setStart(newstart);
i++;
}
repaint2();
return true;
}
public boolean mouseDown(Event evt, int x, int y)
{
startpos = new Point(x,y);
if(bline)
{
for(int s=0;s<vt.size();s++)
{
Rectanglet rect = (Rectanglet)vt.elementAt(s);
if((startpos.x - rect.getStart().x > 0)&& (startpos.x - rect.getEnd().x < 0)&&(startpos.y - rect.getEnd().y < 0)&&(startpos.y - rect.getStart().y > 0) )
{
check = s;
// pct = startpos;
l = new Line();
l.setStart(startpos);
// vtLines.addElement(l);
}
}
}
if (brect)
{
r = new Rectanglet(); //Create the shape - Rectangle
r.setStart(startpos);//Set the start position where mouse went down
vt.addElement(r); //and add the shape (Rectangle) to the vector vt
// vtStartPos.addElement(startpos);
}
if(bText)
{
for(int t = 0;t <vt.size();t++)
{
Rectanglet rect = (Rectanglet)vt.elementAt(t);
if((startpos.x - rect.getStart().x > 0)&& (startpos.x - rect.getEnd().x < 0)&&(startpos.y - rect.getEnd().y < 0)&&(startpos.y - rect.getStart().y > 0))
{
// RectangleText rect1 = new RectangleText();
JTextArea a = new JTextArea();
// textArea.setBounds(rect.x+rect.width/4, rect.y + rect.width/4, rect.width/2, rect.height/2);
//textArea.setBorder();
// Rectangle rec = new Rectangle(rect.getStart().x + rect.getHeight()/4, rect.getStart().y + rect.getHeight()/4 , rect.getHeight()/2, rect.getHeight()/2);
//a.setBounds(rect1);
a.setBounds(rect.getStart().x + 2, rect.getStart().y + 2 , rect.getHeight(), rect.getHeight());
// Rectanglet rect2 = new Rectanglet(rect.getStart().x + 3, rect.getStart().y + 3 , rect.getHeight()/3, rect.getHeight()/3);
// JScrollPane scrollingResult = new JScrollPane(a);
//content.add(scrollingResult);
// JPanel content = new JPanel();
//content.setLayout(new BorderLayout());
// add(scrollingResult);
a.setLineWrap(true);
a.requestFocusInWindow();
a.setEditable(true);
// a.setVisible(true);
add(a);//za text area doar cand largesti imaginea
a.setCaretColor(Color.white);
a.setVisible(true);
//vtText.add(m, a.getText());
vtText.add(m, a);
// vtRect.add(n, rect2);
}
}
}
repaint2();
return true;
}
public boolean mouseDrag(Event evt, int x, int y)
{
//Boolean okrect=false;
//Now the mouse is being dragged without releasing,
//which means that the user may stop his mouse over a
//point but not release it. So that point is the
//current endpoint
endpos = new Point(x,y);
if(bline)
{
for(int s=0;s<vt.size();s++)
{
if(s!=check){
Rectanglet rect = (Rectanglet)vt.elementAt(s);
//if((startpos.x - rect.getStart().x > 0)&& (startpos.x - rect.getEnd().x < 0)&&(startpos.y - rect.getEnd().y < 0)&&(startpos.y - rect.getStart().y > 0))
//{
if((endpos.x - rect.getStart().x > 0)&& (endpos.x - rect.getEnd().x < 0)&&(endpos.y - rect.getEnd().y < 0)&&(endpos.y - rect.getStart().y > 0) )
{
Point newstart = new Point(startpos.x, startpos.y);
//Point drawStop = new Point(endpos.x, endpos.y);
//l = (Line)vtLines.elementAt(j); ///era i inainte
l.setStart(newstart);
//l.setEnd(drawStop);
//j++; //era i inainte
}
}
//}
}
}
if (brect)
{
Point drawto = new Point( Math.max(x,startpos.x),Math.max(y,startpos.y));
Point newstart = new Point(Math.min(x,startpos.x),Math.min(y,startpos.y));
// nwidth1 = Math.abs((drawto.x - newstart.x));
//nheight1 = Math.abs((drawto.y - newstart.y));
//r = (Rectanglet)vt.elementAt(i);
r.setWidth(drawto, newstart);
r.setHeight(drawto, newstart);
r.setEnd(drawto);
r.setStart(newstart);
// vtStartPos.addElement(newstart);
// vtEndPos.addElement(drawto);
// i++;
// okrect=true;
}
repaint2();
return true;
}
}
Class Rectangle
PHP Code:
C
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
class Rectanglet extends Object
{
private int length;
private int width;
public Point ptStart; //Start point of shape
public Point ptEnd; //End point of shape
// public int width; //Width of shape
public int height;
public Color clrFront;
void Draw(Graphics g)
{
g.setColor(Color.blue.brighter()); //Set default color
g.drawRect(ptStart.x, ptStart.y, width, height);
}
public Rectanglet(int x, int y, int w, int h)
{
//ptStart = new Point();
//ptEnd = new Point();
this.ptStart.x = x;
this.ptStart.y = y;
this.width = w;
this.height = h;
}
public Rectanglet()
{
clrFront = Color.red;
}
Boolean containes(Point p)
{
if((ptStart.x - p.x < 0)&& (ptEnd.x - p.x > 0)&&(p.y - ptStart.y > 0)&&(p.y - ptEnd.y < 0))
{
return true;
}
else
return false;
}
public Rectanglet getbounds()
{
return new Rectanglet(ptStart.x, ptStart.y, width, height);
// return new Rectanglet(x1,x2,x3, x4);
}
void setStart(Point ptStart)
{
this.ptStart = ptStart;
}
void setEnd(Point ptEnd)
{
this.ptEnd = ptEnd;
}
Point getStart()
{
return ptStart;
}
int getHeight()
{
return height;
}
public double getWidth(){
return width;
}
public void setWidth(Point P1, Point P2){
this.width = Math.abs(P1.x - P2.x);
}
public void setHeight(Point P1, Point P2)
{
this.height = Math.abs(P1.y - P2.y);
}
Point getEnd()
{
return ptEnd;
}
}
//Drawing routine
Sorry but i dont know how to do it more than this. If u need the other classes let me know. I did my best. If its not ok tell how i must do and i will