problem with jmenu,please help!!!
public static void main(String[] a) {
MyFrame win = new MyFrame();
win.start();
}
private ShapeContainer _draws;Generics!
private int _ink;
private boolean _mark = true;
private int _maxX,_maxY;
private int _stage;
private int[] _selected;
private Point _p1,_p2; // tmp Points for selection
private JTextField msgTf;
// *** text area ***
public MyFrame() {
setSize(600, 500);
_stage = Const.Gen;
_draws = new ShapeContainer();//Vector<Drawable>();
_ink = Const.blue;
_p1=null;
_selected = null;
addWindowListener(new WindowAdapter() { public void
windowClosing(WindowEvent e) { System.exit(0); } } );
}
private static boolean isIn(int[] arr, int k){
boolean ans = false;
if(arr!=null) {
for(int i=0;i<arr.length;i=i+1) {
if(arr[i] ==k) ans = true;
}
}
return ans;
}
public void add(Drawable d) {
_draws.add(d);}
public void paint(Graphics g) { super.paint(g);
for(int ind=0;ind<_draws.size();ind++) {
Drawable d = _draws.elementAt(ind);
if(isIn(_selected,ind)) g.setColor(Color.pink);
else {
int c =d.getColor();
g.setColor(Const.color(c));
}
if (d instanceof Point) {
Point p = (Point)d;
int x = (int)p.x(), y=(int)p.y();
g.fillOval(x-2, y-2,4 ,4);
}
if (d instanceof Circle) {
Circle cr = (Circle)d;
Point center = cr.points()[0];
double radius = center.distance(cr.points()[1]);
int x1 = (int)(center.x()-radius);
int x2 = (int)(center.x()+radius);
int y1 = (int)(center.y()-radius);
int y2 = (int)(center.y()+radius);
if(cr.getFill()) g.fillOval(x1,y1,x2-x1,y2-y1);
else g.drawOval(x1,y1,x2-x1,y2-y1);
}
if (d instanceof Rectangle) {
Rectangle cr = (Rectangle)d;
int x1 = (int)cr.points()[0].x();
int x2 = (int)cr.points()[1].x();
int y1 = (int)cr.points()[0].y();
int y2 = (int)cr.points()[1].y();
if(cr.getFill()) g.fillRect(x1,y1,x2-x1,y2-y1);
else g.drawRect(x1,y1,x2-x1,y2-y1);
}
}
}
/**
* @param presents the Frame
*/
public void start() {
this.show();
Dialog();}
/**
* @param menu details
*/
public void Dialog()
{
//creating a JMenuBar instance
JMenuBar mBar = new JMenuBar();
//setting the JMennuBar instance as the menu bar of this frame
setJMenuBar(mBar);
//creating a JMenu instances and adding them to the mBar
JMenu main = new JMenu("File");
JMenuItem subMain;
subMain = new JMenuItem("Open");
subMain.addActionListener(this);
main.add(subMain);
subMain = new JMenuItem("Save");
subMain.addActionListener(this);
main.add(subMain);
subMain = new JMenuItem("Clear");
subMain.addActionListener(this);
main.add(subMain);
subMain = new JMenuItem("Exit");
subMain.addActionListener(this);
main.add(subMain);
mBar.add(main);
main = new JMenu("Input");
subMain = new JMenuItem("Point");
subMain.addActionListener(this);
main.add(subMain);
subMain = new JMenuItem("Circle");
subMain.addActionListener(this);
main.add(subMain);
subMain = new JMenuItem("Rectangle");
subMain.addActionListener(this);
main.add(subMain);
mBar.add(main);
main = new JMenu("Select");
subMain = new JMenuItem("PointS");
subMain.addActionListener(this);
main.add(subMain);
subMain = new JMenuItem("None");
subMain.addActionListener(this);
main.add(subMain);
mBar.add(main);
main = new JMenu("Edit");
subMain = new JMenuItem("Fill");
subMain.addActionListener(this);
main.add(subMain);
subMain = new JMenuItem("Empty");
subMain.addActionListener(this);
main.add(subMain);
main.addSeparator();
subMain = new JMenuItem("Info");
subMain.addActionListener(this);
main.add(subMain);
this.addMouseListener(new mouseManeger());
}
/**
* @param describe the action order of each sub menu
*/
public void actionPerformed(ActionEvent evt)
{
String arg = evt.getActionCommand();
msgTf.setText("You have chosen " + arg);
if (arg.equals("Open")) openTextFile();
else if (arg.equals("Save")) saveTextFile();
else if(arg.equals("Clear")) {
_stage = Const.Gen;
_selected = null;
_draws = new ShapeContainer();//Vector<Drawable>();
repaint();
}
else if(arg.equals("Exit")) {System.exit(209);}
else if(arg.equals("Point")) {_stage = Const.Point; _selected=null;}
else if(arg.equals("Circle")) {_stage = Const.Circle1;_selected=null;}
else if(arg.equals("Rectangle")) {_stage = Const.Rect1;_selected=null;}
else if(arg.equals("Triangle")) {_stage = Const.T1;_selected=null;}
else if(arg.equals("PointS")) _stage = Const.PointS;
else if(arg.equals("None")) {_selected = null; repaint();}
else if(arg.equals("Fill")&& _selected!=null) {
for(int a=0;a<_selected.length;a=a+1)
_draws.elementAt(_selected[a]).setFill(true);
repaint();
}
else if(arg.equals("Empty")&& _selected!=null) {
for(int a=0;a<_selected.length;a=a+1)
_draws.elementAt(_selected[a]).setFill(false);
repaint();
}
else if(arg.equals("Info")&& _selected!=null) {
for(int a=0;a<_selected.length;a=a+1)
System.out.println(_selected+") "+_draws.elementAt(_selected[a]).toString());
repaint();
}
}
// ********** Private methodes (open,save...) ********
private void openTextFile() {
_stage = Const.Gen;
_selected = null;
FileDialog d = new FileDialog(this,"Open text file", FileDialog.LOAD);
d.show();
String dr = d.getDirectory();
String fi = d.getFile();
if (fi != null) { // the user actually choose a file.
System.out.println("*** should load file: "+dr+fi);
}
}
private void saveTextFile() {
_stage = Const.Gen;
_selected = null;
FileDialog d = new FileDialog(this,"Saving text file", FileDialog.SAVE);
d.show();
String dr = d.getDirectory();
String fi = d.getFile();
try{
// Create file
FileWriter fstream = new FileWriter(dr+fi);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
System.out.println("*** should save to file: "+dr+fi);
}
/**
* @param mouse pointer details
*/
class mouseManeger extends MouseAdapter{ // inner class!!
public void mousePressed(MouseEvent e) {
Graphics g = getGraphics();
int xx = e.getX();
int yy = e.getY();
int len =0;
switch(_stage) {
case (Const.Gen):{
_selected = null;
break;
}
case (Const.PointS):{
Point p = new Point(xx,yy);
int size = 0, j=0;
if(_selected!=null) size = _selected.length;
for(int i=0; i<_draws.size();i++) {
if(_draws.elementAt(i).contains(p)&& !isIn(_selected,i)) j=j+1;;
}
int[] tt=new int[j+size];j=size;
for(int i=0;i<size;i=i+1) tt[i] = _selected[i];
for(int i=0; i<_draws.size();i++) {
if(_draws.elementAt(i).contains(p)&& !isIn(_selected,i)) {
tt[j] = i;
j=j+1;
}
_selected = tt;
repaint();
}
break;
}
case (Const.Point): {
add(new Point(xx, yy, _ink));
_selected =null;
repaint();
break;
}
case (Const.Circle1): {
_p1 = new Point(xx,yy);
_stage = Const.Circle2;
break;
}
case (Const.Circle2): {
add(new Circle(_p1, _p1.distance(new Point(xx,yy)), _ink));
_stage = Const.Circle1;
_selected =null;
repaint();
break;
}
case (Const.Rect1): {
_p1 = new Point(xx,yy);
_stage = Const.Rect2;
break;
}
case (Const.Rect2): {
add(new Rectangle(_p1, new Point(xx,yy), _ink));
_stage = Const.Rect1;
_selected =null;
repaint();
break;
}
case (Const.T1): {
_p1 = new Point(xx,yy);
_stage = Const.T2;
break;
}
}
}
}
}
the other class of the program if someone need... rhanks
import java.util.*;
/**
* this class represents a 2d point in the plane. <br>
* supports several operations on points in the plane.
*/
public class Point implements Drawable {
// ***** private data *****
private double _x, _y;
private int _color; // the color table is in class Const.
private boolean _fill; // so it could implements Drawable
// ****** constructors ******
public Point (double x1, double y1,int c){
_color = c;
_x = x1;
_y = y1;
}
// ****** constructors ******
public Point (double x1, double y1){
_color = Const.black;
_x = x1;
_y = y1;
}
/**copy constructor */
public Point (Point p) {
_x = p._x;
_y = p._y;
_color=p._color;
}
// ***** public methodes *****
public Point midPoint(Point p) {
return new Point((_x+p._x)/2,(_y+p._y)/2);}
/**return the first (x) coordinate */
public double x() {return _x;}
/**return the second (y) coordinate */
public double y() {return _y;}
/**@return the L2 distanse */
public double distance (Point p)
{
double temp = Math.pow (p.x() - _x, 2) + Math.pow (p.y() - _y, 2);
return Math.sqrt (temp);
}
/**
@return a String that contains the Point data*/
public String toString() {
return this.getClass().getName()+" "+_x+" "+_y+" "+_color;
}
// *********************** the Drawable interface ***************************
/** logical equals
@param p other Object (Point).
@return true if p instance of Point && logicly the same) */
public boolean equals (Object p) {
boolean ans = false;
if(p!= null && p instanceof Drawable) {
Drawable d = (Drawable)p;
if (d instanceof Point) ans = same((Point)d);
ans = ans & (d.getColor() == _color & d.getFill() == _fill);
}
return ans;
}
/* @param p other Object (Point).
@return true iff p instance of Point && logicly the same) */
private boolean same (Point p){return (p._x == _x && p._y==_y);}
/** returns this Color (C Const for the color table).*/
public int getColor() { return _color;}
/** changes this Color to c, (see Const.java for the color table.*/
public void setColor(int c) { _color = c; }
/**do nothing! - just so the point could B displayed.*/
public boolean getFill(){return false;}
/**do nothing! - just so the point could B displayed.*/
public void setFill(boolean fill){;}
/**@return an array that include the point only*/
public Point[] points() {
Point[] ans = new Point[1];
ans[0]= new Point(this);
return ans;
}
// /**@return true if num is inside this.*/
// public boolean check(int num){
// return num==this._x||num==this._y;
// }
/**@return true if p is inside this.*/
public boolean contains(Point p) {return this.equals(p);}
/** @return the perimeter of this point --> 0. */
public double perimeter() {return 0;}
/** @return the area of this point --> 0. */
public double area() {return 0;}
/**
translate this by the Point p (as it was a vector from 0,0)
<br> NOTE! this method changes the data members of the object.*/
public void translate (Point p) {
if(p!=null) {
_x += p._x;
_y += p._y;
}
}
/** rescale this point according to the center point.
@param center represent a 'center point'.
@param size represent a size of scaling.
*/
public void rescale(Point center, double size) {
if(center!=null && size>0)
rescale(center,size,size);
}
private void rescale(Point center, double sizeX,double sizeY) {
_x = center._x + ((_x - center._x) * sizeX);
_y = center._y + ((_y - center._y) * sizeY);
}
/** @return a copy (deep copy semantic) of this. */
public Drawable copy() {return new Point(this);}
/** creates a new Drawable from the String s (must work on this toString).
if fails to construct an Exception should be thrown. */
public Drawable create(String s) throws Exception {
Drawable ans = null;
if(s==null) throw new Exception("** Error: can not create a Point from null **");
StringTokenizer st = new StringTokenizer(s);
if(!st.nextToken().equals(this.getClass().getName( )))
throw new Exception("** Error: "+s+" does not seems to be a Point! **");
try {
double x = (new Double(st.nextToken())).doubleValue();
double y = (new Double(st.nextToken())).doubleValue();
int c = (new Integer(st.nextToken())).intValue();
ans = new Point(x,y,c);
}
catch(NumberFormatException e)
{
System.err.println(e);
throw new Exception("** Error: "+s+" does not seems to be a Point! **");
}
return ans;
}
}// class Point
import java.util.*;
/**
* this class represent 2D Acix parallel Rectangle.
*/
public class Rectangle implements Drawable {
/**
* @param args
*/
//**********Private Data***********
private Point _p1,_p2;
private int _color;
private boolean _fill;
// ****** constructors ******
public Rectangle(Point p1, Point p2){
this(p1,p2,Const.black, false);
}
public Rectangle(Point p1, Point p2, int c){
this(p1,p2,c, false);
}
public Rectangle(Point p1, Point p2, int c,boolean fi){
if(p1==null || p2 == null)
throw new RuntimeException("** Error: null vertices parameters for Rectangle constructor **");
_fill = fi;
_p1 = new Point(Math.min(p1.x(), p2.x()), Math.min(p1.y(), p2.y()));
_p2 = new Point(Math.max(p1.x(), p2.x()), Math.max(p1.y(), p2.y()));
_color = c;
}
public Rectangle(Rectangle r){
if(r==null) throw new RuntimeException("** Error: null parameter for Rectangle (copy) constructor **");
_fill = r._fill;
_color = r._color;
_p1 = new Point(r._p1);
_p2 = new Point(r._p2);
}
public Point p1(){return _p1;}
public Point p2(){return _p2;}
public double dx(){return Math.abs(_p1.x()-_p2.x());}
public double dy() {return Math.abs(_p1.y()-_p2.y());}
public String toString() { return(this.getClass().getName()+" "+_p1+" "+_p2+" "+_color+" "+_fill);}
public boolean equals (Rectangle r){
return (r!=null)
&&
((this._p1.equals(r._p1))&&(this._p2.equals(r._p2) ))
||
((this._p1.equals(r._p2))&&(this._p2.equals(r._p1) ))
;}
public double perimeter() {
double dx = _p1.x() - _p2.x();
double dy = _p1.y() - _p2.y();
return Math.abs(dx+dy)*2;
}
public double area() {
double dx = _p1.x() - _p2.x();
double dy = _p1.y() - _p2.y();
return Math.abs(dx*dy);
}
public Point[] getvertices() {
Point[] ans = new Point[4];
ans[0] = new Point(_p1);
ans[1] = new Point(_p2);
ans[2] = new Point(_p1.x(), _p2.y());
ans[3] = new Point(_p2.x(), _p1.y());
return ans;
}
public Point centerOfGravity() {
double x = (_p1.x() + _p2.x())/2;
double y = (_p1.y() + _p2.y())/2;
return new Point(x,y);
}
public void translate(Point vec) {
if (vec!=null) {
_p1.translate(vec);
_p2.translate(vec);
}
}
public Point[] points()
{
Point ans[] = new Point[2];
ans[0] = new Point(_p1);
ans[1] = new Point(_p2);
return ans;
}
public boolean equals(Object d1)
{
boolean ans = false;
if(d1!=null && d1 instanceof Rectangle) {
Rectangle d = (Rectangle)d1;
ans = this._p1.equals(d._p1) & this._p2.equals(d._p2) & _color == d.getColor() & _fill == d.getFill();
}
return ans;
}
public boolean getFill(){return this._fill;}
public void setFill(boolean fill){this._fill=fill;}
public int getColor(){return this._color;}
public void setColor(int c){
_color=c;
}
public Drawable copy(){return new Rectangle(this);}
public boolean contains(Point p){
return p!=null
&& p.x()<= Math.max(_p1.x(), _p2.x())
&& p.y()<= Math.max(_p1.y(), _p2.y())
&& p.x()>= Math.min(_p1.x(), _p2.x())
&& p.y()>= Math.min(_p1.y(), _p2.y());
}
public boolean containedIn(Rectangle r){
return (r!=null && r.contains(_p1) && r.contains(_p2));
}
public Drawable create(String s) throws Exception {
Drawable ans = null;
if(s==null) throw new Exception("** Error: can not create a Rectangle from null **");
StringTokenizer st = new StringTokenizer(s);
if(!st.nextToken().equals(this.getClass().getName( )))
throw new Exception("** Error: "+s+" does not seems to be a Rectangle! **");
try {
String ps1 = st.nextToken()+" "+st.nextToken()+" "+st.nextToken()+" "+st.nextToken();
String ps2 = st.nextToken()+" "+st.nextToken()+" "+st.nextToken()+" "+st.nextToken();
Point c1 = new Point(0,0);
Point c2 = new Point(0,0);
c1 = (Point)c1.create(ps1);
c2 = (Point)c2.create(ps2);
int co = (new Integer(st.nextToken())).intValue();
boolean fi = (new Boolean(st.nextToken())).booleanValue();
ans = new Rectangle(c1,c2,co,fi);
}
catch(NumberFormatException e)
{
System.err.println(e);
throw new Exception("** Error: "+s+" does not seems to be a Rectangle! **");
}
return ans;
}
}// class Rectangle
and the intefaces...
import java.util.*;
/**
* this class represent 2D Acix parallel Rectangle.
*/
public class Rectangle implements Drawable {
/**
* @param args
*/
//**********Private Data***********
private Point _p1,_p2;
private int _color;
private boolean _fill;
// ****** constructors ******
public Rectangle(Point p1, Point p2){
this(p1,p2,Const.black, false);
}
public Rectangle(Point p1, Point p2, int c){
this(p1,p2,c, false);
}
public Rectangle(Point p1, Point p2, int c,boolean fi){
if(p1==null || p2 == null)
throw new RuntimeException("** Error: null vertices parameters for Rectangle constructor **");
_fill = fi;
_p1 = new Point(Math.min(p1.x(), p2.x()), Math.min(p1.y(), p2.y()));
_p2 = new Point(Math.max(p1.x(), p2.x()), Math.max(p1.y(), p2.y()));
_color = c;
}
public Rectangle(Rectangle r){
if(r==null) throw new RuntimeException("** Error: null parameter for Rectangle (copy) constructor **");
_fill = r._fill;
_color = r._color;
_p1 = new Point(r._p1);
_p2 = new Point(r._p2);
}
public Point p1(){return _p1;}
public Point p2(){return _p2;}
public double dx(){return Math.abs(_p1.x()-_p2.x());}
public double dy() {return Math.abs(_p1.y()-_p2.y());}
public String toString() { return(this.getClass().getName()+" "+_p1+" "+_p2+" "+_color+" "+_fill);}
public boolean equals (Rectangle r){
return (r!=null)
&&
((this._p1.equals(r._p1))&&(this._p2.equals(r._p2) ))
||
((this._p1.equals(r._p2))&&(this._p2.equals(r._p1) ))
;}
public double perimeter() {
double dx = _p1.x() - _p2.x();
double dy = _p1.y() - _p2.y();
return Math.abs(dx+dy)*2;
}
public double area() {
double dx = _p1.x() - _p2.x();
double dy = _p1.y() - _p2.y();
return Math.abs(dx*dy);
}
public Point[] getvertices() {
Point[] ans = new Point[4];
ans[0] = new Point(_p1);
ans[1] = new Point(_p2);
ans[2] = new Point(_p1.x(), _p2.y());
ans[3] = new Point(_p2.x(), _p1.y());
return ans;
}
public Point centerOfGravity() {
double x = (_p1.x() + _p2.x())/2;
double y = (_p1.y() + _p2.y())/2;
return new Point(x,y);
}
public void translate(Point vec) {
if (vec!=null) {
_p1.translate(vec);
_p2.translate(vec);
}
}
public Point[] points()
{
Point ans[] = new Point[2];
ans[0] = new Point(_p1);
ans[1] = new Point(_p2);
return ans;
}
public boolean equals(Object d1)
{
boolean ans = false;
if(d1!=null && d1 instanceof Rectangle) {
Rectangle d = (Rectangle)d1;
ans = this._p1.equals(d._p1) & this._p2.equals(d._p2) & _color == d.getColor() & _fill == d.getFill();
}
return ans;
}
public boolean getFill(){return this._fill;}
public void setFill(boolean fill){this._fill=fill;}
public int getColor(){return this._color;}
public void setColor(int c){
_color=c;
}
public Drawable copy(){return new Rectangle(this);}
public boolean contains(Point p){
return p!=null
&& p.x()<= Math.max(_p1.x(), _p2.x())
&& p.y()<= Math.max(_p1.y(), _p2.y())
&& p.x()>= Math.min(_p1.x(), _p2.x())
&& p.y()>= Math.min(_p1.y(), _p2.y());
}
public boolean containedIn(Rectangle r){
return (r!=null && r.contains(_p1) && r.contains(_p2));
}
public Drawable create(String s) throws Exception {
Drawable ans = null;
if(s==null) throw new Exception("** Error: can not create a Rectangle from null **");
StringTokenizer st = new StringTokenizer(s);
if(!st.nextToken().equals(this.getClass().getName( )))
throw new Exception("** Error: "+s+" does not seems to be a Rectangle! **");
try {
String ps1 = st.nextToken()+" "+st.nextToken()+" "+st.nextToken()+" "+st.nextToken();
String ps2 = st.nextToken()+" "+st.nextToken()+" "+st.nextToken()+" "+st.nextToken();
Point c1 = new Point(0,0);
Point c2 = new Point(0,0);
c1 = (Point)c1.create(ps1);
c2 = (Point)c2.create(ps2);
int co = (new Integer(st.nextToken())).intValue();
boolean fi = (new Boolean(st.nextToken())).booleanValue();
ans = new Rectangle(c1,c2,co,fi);
}
catch(NumberFormatException e)
{
System.err.println(e);
throw new Exception("** Error: "+s+" does not seems to be a Rectangle! **");
}
return ans;
}
}// class Rectangle
/**
this interface represents a group (container) of Drawables that can B selected and edited.<br>
It can be implemented using any ADT u desire. e.g.: Vector, LinkedList*/
public interface Editable{
/** adds a new Drawable (deep copy semantic)
<p> Note: this method changes the Object state!*/
public void add(Drawable d);
/** finds all the Drawables which contains p.
@return an array of each index of the Drawable that contains p, null if none
for indices u may use any numbering scheme*/
public int[] contains(Point p);
/** @return the number of Drawables (contained in this) */
public int size();
/** @return a new -copy- of the Drawable in the i-th place (deep copy)*/
public Drawable elementAt(int i);
/** removes all the Drawables with the indices specified
in (arr[i]) (if such Drawbles exist).<p>
Note: this method shrinks the group size().<br>
Note: this method changes the Object state!*/
public void remove(int[] arr);
/** compare if this is logical the same container as other.<p>
false: other is null || not instance of Editable || this does not contains the same Drawables as other . <br>
true: this & other contains the same Drawables . */
public boolean equals(Object d);
/** this method creates a DEEP copy of this (Editable) */
public Editable copy();
/** changes the Color to c, for all the Drawables included in selected.
<p> Note: this method changes the Object state!
@see Const Const.java for color table.
*/
public void setColor(int[] selected, int c);
/** if fill=true, fill the shape, for all the Drawables included in selected.
<p> Note: this method changes the Object state!*/
public void setFill(int[] selected, boolean fill);
/** translate all the selected Drawables by the vector origin -> p.
<p> Note: this method changes the Object state!
@param p represent a vector from the origin to p.
*/
public void translate(int[] selected, Point p);
/** copy all the selected Drawables by the vector origin -> p.
* <p> Note: this method changes the Object state!
@param p represent a vector from the origin to p.
*/
public void copy(int[] selected, Point p);
/** rescale all the selected Drawables by the center point.
<p> Note: this method changes the Object state!
@param cen represents the 'center point'.
@param size represents the rescale factor.
*/
public void rescale(int[] selected, Point cen, double size);
/** checks if d is already member in this container (using equals - logical).
* @return true iff this group of Drawables contains d.
**/
public boolean member(Drawable d);
}
wow i don't know how the problem is erase...
hi,
i have two problems with out exceptions :
1. when i run the code the shape that i create coen on my menu bar if i draw them near by...
2. when i draw shape near the menu and open the menu he erase from the screen some of the shape and after i do something else the shape come back...
if you want i can send you need the zip code/jar file i can send you but you understand i guess what i mean...
thanks for everyone who help me