problem with jmenu and jframe

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 4999 Views - Last Post: 05 January 2011 - 12:38 PM Rate Topic: -----

#1 beni.vd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-January 11

problem with jmenu and jframe

Posted 01 January 2011 - 01:46 PM

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


the shortest code that i can give...
i need that you tell me why the jmenu is in the same focus of th content pane and how i can manage that when i press the jmenu the menu don't erase me the circle...

beside of that why when i draw a circle it come on my menu how i can separate between them?



thank's beni...
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class MyFrame extends JFrame implements ActionListener	{ 
public static void main(String[] a) {
MyFrame win = new MyFrame();
win.start();
}	

private ShapeContainer _draws;
private int _ink;
private int _stage;
private int[] _selected; 
private Point _p1; 

public MyFrame() {

setTitle("EX5.java Beni vagenfeld 065631004");
setSize(700, 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;
}
/**
* @param adds Drawable to _draws
*/
public void add(Drawable d) {
_draws.add(d);} 

public void paint(Graphics g) { super.paint(g);

//	 System.out.println("Stage: "+_stage+" _selected:"+_selected);
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 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);
}

}

}
/**
* @param presents the Frame
*/
public void start() {	
this.show(); 
Dialog();}
/**
* @param menu details
*/
public void Dialog()
{ 

JMenuBar mBar = new JMenuBar();
setJMenuBar(mBar);
JMenu main = new JMenu("File");
JMenuItem subMain;
main = new JMenu("Input");
subMain = new JMenuItem("Circle");
subMain.addActionListener(this);
main.add(subMain);
mBar.add(main);


this.addMouseListener(new mouseManeger());
}
public void actionPerformed(ActionEvent evt)
{ 
String arg = evt.getActionCommand();

if(arg.equals("Circle")) {_stage = Const.Circle1;_selected=null;}
}

class mouseManeger extends MouseAdapter{ // inner class!!	
public void mousePressed(MouseEvent e) {
int xx = e.getX();
int yy = e.getY();
switch(_stage) {
case (Const.Gen):{
_selected = null;
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;
}	
}


}
}
}


This post has been edited by macosxnerd101: 01 January 2011 - 01:52 PM
Reason for edit:: Please use code tags. Also, please properly indent your code so others can read it.


Is This A Good Question/Topic? 0
  • +

Replies To: problem with jmenu and jframe

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: problem with jmenu and jframe

Posted 02 January 2011 - 12:52 PM

Not a good idea to overload the paint() method of a JFrame

In your JFrame add(new JLabel(""), BorderLayout.NORTH);

then create your own class MyPanel that extends JPanel
put and instance of MyPanel BorderLayout.CENTER
overload the paint() method of that JPanel and do your drawing there
Was This Post Helpful? 1
  • +
  • -

#3 beni.vd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-January 11

Re: problem with jmenu and jframe

Posted 04 January 2011 - 12:38 AM

View Postpbl, on 02 January 2011 - 11:52 AM, said:

Not a good idea to overload the paint() method of a JFrame

In your JFrame add(new JLabel(""), BorderLayout.NORTH);

then create your own class MyPanel that extends JPanel
put and instance of MyPanel BorderLayout.CENTER
overload the paint() method of that JPanel and do your drawing there

hi, i try to move the paint and the other part beside the menu to MyPanel but i have a problem to move the
mousemanager beacuse the line in the menu of
this.addMouseListener(new mouseManeger());

can you tell me how to do what you say beacuse i try but it doesn't work for me...
thank's
beni
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: problem with jmenu and jframe

Posted 04 January 2011 - 02:08 AM

View Postbeni.vd, on 04 January 2011 - 01:38 AM, said:

View Postpbl, on 02 January 2011 - 11:52 AM, said:

Not a good idea to overload the paint() method of a JFrame

In your JFrame add(new JLabel(""), BorderLayout.NORTH);

then create your own class MyPanel that extends JPanel
put and instance of MyPanel BorderLayout.CENTER
overload the paint() method of that JPanel and do your drawing there

hi, i try to move the paint and the other part beside the menu to MyPanel but i have a problem to move the
mousemanager beacuse the line in the menu of
this.addMouseListener(new mouseManeger());

can you tell me how to do what you say beacuse i try but it doesn't work for me...
thank's
beni

You will have to move your MouseManeger() as an inner class of your MyPanel
so "this" will be your new JPanel
or do myPanel.add(new MouseManeger());

Happy coding
Was This Post Helpful? 0
  • +
  • -

#5 beni.vd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-January 11

Re: problem with jmenu and jframe

Posted 04 January 2011 - 05:46 AM

View Postpbl, on 04 January 2011 - 01:08 AM, said:

View Postbeni.vd, on 04 January 2011 - 01:38 AM, said:

View Postpbl, on 02 January 2011 - 11:52 AM, said:

Not a good idea to overload the paint() method of a JFrame

In your JFrame add(new JLabel(""), BorderLayout.NORTH);

then create your own class MyPanel that extends JPanel
put and instance of MyPanel BorderLayout.CENTER
overload the paint() method of that JPanel and do your drawing there

hi, i try to move the paint and the other part beside the menu to MyPanel but i have a problem to move the
mousemanager beacuse the line in the menu of
this.addMouseListener(new mouseManeger());

can you tell me how to do what you say beacuse i try but it doesn't work for me...
thank's
beni

You will have to move your MouseManeger() as an inner class of your MyPanel
so "this" will be your new JPanel
or do myPanel.add(new MouseManeger());

Happy coding


it won't work me...
i add you the my pannel that i worth and my frame
please help me to understand were is the problematic code
i want to send you all but i can't in this forum
thank's for your help...

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: problem with jmenu and jframe

Posted 04 January 2011 - 11:46 AM

Post your code
Was This Post Helpful? 0
  • +
  • -

#7 beni.vd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-January 11

Re: problem with jmenu and jframe

Posted 04 January 2011 - 03:46 PM

View Postpbl, on 04 January 2011 - 10:46 AM, said:

Post your code

what do you mean i put the code in the comment...
in txt file beacuse it long...
thank's
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: problem with jmenu and jframe

Posted 04 January 2011 - 03:53 PM

We'd generally prefer it to :code: unless it's just absolutely massive.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10179
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: problem with jmenu and jframe

Posted 04 January 2011 - 03:56 PM

Please post your code in the body of your post using code tags. Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: problem with jmenu and jframe

Posted 04 January 2011 - 04:16 PM

View Postbeni.vd, on 04 January 2011 - 04:46 PM, said:

View Postpbl, on 04 January 2011 - 10:46 AM, said:

Post your code

what do you mean i put the code in the comment...
in txt file beacuse it long...
thank's

We hate downloading files here
This site has been attacked more than once with malicious dowloads and....
And attachments defeat the pedagogical approach of this site... viewers won't bother downloading the files and missed the corrections
Was This Post Helpful? 0
  • +
  • -

#11 beni.vd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-January 11

Re: problem with jmenu and jframe

Posted 04 January 2011 - 04:56 PM

View Postpbl, on 04 January 2011 - 03:16 PM, said:

View Postbeni.vd, on 04 January 2011 - 04:46 PM, said:

View Postpbl, on 04 January 2011 - 10:46 AM, said:

Post your code

what do you mean i put the code in the comment...
in txt file beacuse it long...
thank's

We hate downloading files here
This site has been attacked more than once with malicious dowloads and....
And attachments defeat the pedagogical approach of this site... viewers won't bother downloading the files and missed the corrections


ok... i'm new sorry

so MyPanel class is:
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;

import javax.swing.JPanel;



public class MyPanel extends JPanel {
	
	public static final Drawable MouseManeger = null;

	public static ShapeContainer _draws;
	
	private int _ink;
	private int _stage;
	private int[] _selected; 
	private Point _p1,_p2; // tmp Points for selection
	public MyPanel() {
		_stage = Const.Gen; 
		_ink = Const.blue;
		_p1=null;
		_selected = null;
	_draws =  new ShapeContainer();//Vector<Drawable>();
	}
	
	public static void add(Drawable d) {
		 _draws.add(d);} 

	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 paint(Graphics g) { 	super.paint(g);

	//		System.out.println("Stage: "+_stage+"  _selected:"+_selected);
	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));
		}
		// **** Point ****
		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);
		}
		//			**** Rectangle ****
		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);
		}
		//			**** Triangle ****
		if (d instanceof Triangle){
			Triangle t=(Triangle)d;
			if (t.getFill())g.fillPolygon(t.xField(), t.yField(), 3);
			else g.drawPolygon(t.xField(), t.yField(), 3);
		}
	}

	}

	class mouseManeger extends MouseAdapter{   // inner class!!										 	
		public void mousePressed(MouseEvent e) {

			Graphics g = getGraphics();
			int xx = e.getX();
			int yy = e.getY();
			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<MyPanel._draws.size();i++) {
					if(MyPanel._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<MyPanel._draws.size();i++) {
					if(MyPanel._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;
			}
			case (Const.T2): {
				_p2 = new Point(xx,yy);
				_stage = Const.T3;
				break;
			}


			case (Const.T3): {
				add(new Triangle(_p1,_p2,new Point(xx,yy),_ink));
				_stage = Const.T1;
				_selected =null;
				repaint();
				break;
			}

			case (Const.Move1): {
				{
					Point p = new Point(xx, yy);
					if(_selected != null)
					{
						Point arr[] = MyPanel._draws.elementAt(_selected[0]).points();
						double x = p.x() - arr[0].x();
						double y = p.y() - arr[0].y();
						p = new Point(x, y);
						for(int a = 0; a < _selected.length; a++)
						{
							MyPanel._draws.elementAt(_selected[a]).translate(p);
						}

						_selected = null;
						repaint();
					}
					// fall through
				}
				break;
			}
			case (Const.Rescale1): 
			{
				Point p = new Point(xx, yy);
				if(_selected != null)
				{
					for(int a = 0; a < _selected.length; a++)
					{
						MyPanel._draws.elementAt(_selected[a]).rescale(p, 1.1);
					}

					_selected =null;
					repaint();
				}
				// fall through
			}
			case (Const.Rescale2): 
			{
				Point p = new Point(xx, yy);
				if(_selected != null)
				{
					for(int a = 0; a < _selected.length; a++)
					{
						MyPanel._draws.elementAt(_selected[a]).rescale(p, 0.9);
					}

					_selected =null;
					repaint();
				}
				// fall through
			}
			}


		}
	}





}





and MyFrame class is:
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu.Separator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.Abstractdocument.Content;

import MyPanel.mouseManeger;


/**
this class is the window manager including all GUI parts: 
paint method, data Editable (Drawable container),GUI, mouse manager,
 */

public class MyFrame extends JFrame implements ActionListener	{ 
	
	 
	


	// *** private data ***
	public static void main(String[] a) {
		System.out.println("Ex5 GUI example is now running!");
		
		MyFrame win = new MyFrame();
		MyPanel pane=new MyPanel();
		win.add(pane);
		win.start();
	}	
	// *** private data ***
	//private <Drawable> _draws; // using 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() {
		
		setTitle("EX5.java  Beni vagenfeld 065631004");
		setSize(700, 500);
		_stage = Const.Gen; 
	
		_ink = Const.blue;
		_p1=null;
		_selected = null;
		 
	   	   
  	  

		addWindowListener(new WindowAdapter() { public void
			windowClosing(WindowEvent e) { System.exit(0); } } );
	}
	
	 
	///****************************** Points Container##############################
	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;
	}
	/**
	 * @param adds Drawable to _draws
	 */
	public void add(Drawable d) {
		MyPanel._draws.add(d);} 
	/**
	 * @param presents the objects on the screen
	 */
	
	
	/**
	 * @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);
         subMain = new JMenuItem("Triangle");
         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");
         JMenu subMenu = new JMenu("colors");
         subMain = new JMenuItem("Green");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("Red");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("Blue");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("Black");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("yellow");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("orange");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("magenta");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("cyan");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("white");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         main.add(subMenu);
         mBar.add(main);
         subMain = new JMenuItem("Delete");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Fill");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Empty");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Move");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Rescale +10%");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Rescale -10%");
         subMain.addActionListener(this);
         main.add(subMain);
         main.addSeparator();
         subMain = new JMenuItem("Info");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Number of polygons");
         subMain.addActionListener(this);
         main.add(subMain);
        
         this.addMouseListener(new mouseManeger());
         





//         //creating JMenuItems instances and adding them to editMenu, the JMenu instance
//
//         cutItem = new JMenuItem("Cut", new ImageIcon("cut.gif"));
//
//         cutItem.setHorizontalTextPosition(SwingConstants.RIGHT);
//
//         editMenu.add(cutItem);

         
         msgTf = new JTextField("Here you will see your actions");

         getContentPane().add(msgTf,"South");
         

   }           


	public void actionPerformed(ActionEvent evt)
	{  
		String arg = evt.getActionCommand();
	
	
	

	if(arg.equals("Clear")) {
		_stage = Const.Gen;
		_selected = null;
		MyPanel._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)
			MyPanel._draws.elementAt(_selected[a]).setFill(true);	
		repaint();		
	}
	else if(arg.equals("Move"))	{_stage = Const.Move1;}	
	else if(arg.equals("Rescale +10%"))_stage=Const.Rescale1;
	else if(arg.equals("Rescale null0%"))_stage=Const.Rescale2;
	else if(arg.equals("Empty")&& _selected!=null) {
		for(int a=0;a<_selected.length;a=a+1)
			MyPanel._draws.elementAt(_selected[a]).setFill(false);	
		repaint();		
	}      
	else if(arg.equals("Number of polygons"))
	{
		System.out.println((new StringBuilder("The number of the polygons is: ")).append(MyPanel._draws.size()).toString());
		repaint();
	}
	else if(arg.equals("Red")) { 
		if( _selected!=null) {
			for(int a=0;a<_selected.length;a=a+1)
				MyPanel._draws.elementAt(_selected[a]).setColor(Const.red);	
			_selected=null;
			repaint();
		}
		_ink = Const.red;
	}
	else if(arg.equals("Blue")) {  
		if( _selected!=null) {
			for(int a=0;a<_selected.length;a=a+1)
				MyPanel._draws.elementAt(_selected[a]).setColor(Const.blue);	
			_selected=null;
			repaint();
		}
		_ink = Const.blue;
	}
	else if(arg.equals("Green")) { 
		if( _selected!=null) {
			//	_draws.elementAt(_selected).setColor(Const.green);	
			for(int a=0;a<_selected.length;a=a+1)
				MyPanel._draws.elementAt(_selected[a]).setColor(Const.blue);	
			_selected=null;
			repaint();
		}
		_ink = Const.green;
	}
	else if(arg.equals("White")) { 
		if( _selected!=null) {
			for(int a=0;a<_selected.length;a=a+1)
				MyPanel._draws.elementAt(_selected[a]).setColor(Const.blue);	
			_selected=null;
			repaint();
		}
		_ink = Const.white;
	}
	else if(arg.equals("Pink")) { 
		if( _selected!=null) {
			for(int a=0;a<_selected.length;a=a+1)
				MyPanel._draws.elementAt(_selected[a]).setColor(Const.blue);	
			_selected=null;
			repaint();
		}
		_ink = Const.pink;
	}
	else if(arg.equals("Delete")&& _selected!=null) {
		//		_draws.removeElementAt(_selected);
		_selected = null;
		repaint();
	}
	else if(arg.equals("Info")&& _selected!=null) {
		for(int a=0;a<_selected.length;a=a+1)
			System.out.println(_selected+") "+MyPanel._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
	 */
	




}





i try to take the add and mousemanger to my panel but i have too many problem.
i hope you can help me...
like you see i try but it's not work...
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10179
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: problem with jmenu and jframe

Posted 04 January 2011 - 04:59 PM

Please post your specific error messages as well.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: problem with jmenu and jframe

Posted 04 January 2011 - 05:22 PM

Haven't seen such an horror since I teach at the university
Kids are still making such horrible code

         JMenu subMenu = new JMenu("colors");
         subMain = new JMenuItem("Green");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("Red");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("Blue");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("Black");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("yellow");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("orange");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("magenta");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("cyan");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("white");
         subMain.addActionListener(this);
         subMenu.add(subMain);


should be
String[] colorStr = {"Green", "Red", "Blue", "Black", "Yellow", ....};
Color[] color = {Color.GREEN, Color.RED, Color.BLUE,.....};

// build it
for(int i = 0; i < colorStr.length; ++i) {
    subMain = new JMenuItem(colorStr[i]);
    subMain.addActionListener(this);
    subMenu.add(subMain);



in actionPerformed()
for(int i = 0; i < colorStr.length; ++i) {
   if(args.equals(colorStr[i]) {
     g.setColor(color[i]);
     break;
   }
   MyPanel.drawElement(....



If you have to repeat the same task more than twice
put it in a for() loop

This post has been edited by pbl: 04 January 2011 - 05:25 PM

Was This Post Helpful? 1
  • +
  • -

#14 beni.vd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-January 11

Re: problem with jmenu and jframe

Posted 04 January 2011 - 05:23 PM

View Postmacosxnerd101, on 04 January 2011 - 03:59 PM, said:

Please post your specific error messages as well.


ok..(can i edit the post beacuse i enter class MyFrame not good from the old try...)

the MyFrame class look like that

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu.Separator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.Abstractdocument.Content;




/**
this class is the window manager including all GUI parts: 
paint method, data Editable (Drawable container),GUI, mouse manager,
 */

public class MyFrame extends JFrame implements ActionListener	{ 
	
	 
	


	// *** private data ***
	public static void main(String[] a) {
		System.out.println("Ex5 GUI example is now running!");
		MyPanel pane=new MyPanel();
		MyFrame win = new MyFrame();
	    win.add(pane);
		win.start();
	}	
	// *** private data ***
	//private <Drawable> _draws; // using 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(700, 500);
		_stage = Const.Gen; 
	
		_ink = Const.blue;
		_p1=null;
		_selected = null;
		 
	   	   
  	  

		addWindowListener(new WindowAdapter() { public void
			windowClosing(WindowEvent e) { System.exit(0); } } );
	}
	
	 
	///****************************** Points Container##############################
	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;
	}
	/**
	 * @param adds Drawable to _draws
	 */
	public void add(Drawable d) {
		MyPanel._draws.add(d);} 
	/**
	 * @param presents the objects on the screen
	 */
	
	
	/**
	 * @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);
         subMain = new JMenuItem("Triangle");
         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");
         JMenu subMenu = new JMenu("colors");
         subMain = new JMenuItem("Green");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("Red");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("Blue");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("Black");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("yellow");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("orange");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("magenta");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("cyan");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         subMain = new JMenuItem("white");
         subMain.addActionListener(this);
         subMenu.add(subMain);
         main.add(subMenu);
         mBar.add(main);
         subMain = new JMenuItem("Delete");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Fill");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Empty");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Move");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Rescale +10%");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Rescale -10%");
         subMain.addActionListener(this);
         main.add(subMain);
         main.addSeparator();
         subMain = new JMenuItem("Info");
         subMain.addActionListener(this);
         main.add(subMain);
         subMain = new JMenuItem("Number of polygons");
         subMain.addActionListener(this);
         main.add(subMain);
        
         this.addMouseListener(new mouseManeger());
         





//         //creating JMenuItems instances and adding them to editMenu, the JMenu instance
//
//         cutItem = new JMenuItem("Cut", new ImageIcon("cut.gif"));
//
//         cutItem.setHorizontalTextPosition(SwingConstants.RIGHT);
//
//         editMenu.add(cutItem);

         
         msgTf = new JTextField("Here you will see your actions");

         getContentPane().add(msgTf,"South");
         

   }           


	public void actionPerformed(ActionEvent evt)
	{  
		String arg = evt.getActionCommand();
	
	
	

	if(arg.equals("Clear")) {
		_stage = Const.Gen;
		_selected = null;
		MyPanel._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)
			MyPanel._draws.elementAt(_selected[a]).setFill(true);	
		repaint();		
	}
	else if(arg.equals("Move"))	{_stage = Const.Move1;}	
	else if(arg.equals("Rescale +10%"))_stage=Const.Rescale1;
	else if(arg.equals("Rescale null0%"))_stage=Const.Rescale2;
	else if(arg.equals("Empty")&& _selected!=null) {
		for(int a=0;a<_selected.length;a=a+1)
			MyPanel._draws.elementAt(_selected[a]).setFill(false);	
		repaint();		
	}      
	else if(arg.equals("Number of polygons"))
	{
		System.out.println((new StringBuilder("The number of the polygons is: ")).append(MyPanel._draws.size()).toString());
		repaint();
	}
	else if(arg.equals("Red")) { 
		if( _selected!=null) {
			for(int a=0;a<_selected.length;a=a+1)
				MyPanel._draws.elementAt(_selected[a]).setColor(Const.red);	
			_selected=null;
			repaint();
		}
		_ink = Const.red;
	}
	else if(arg.equals("Blue")) {  
		if( _selected!=null) {
			for(int a=0;a<_selected.length;a=a+1)
				MyPanel._draws.elementAt(_selected[a]).setColor(Const.blue);	
			_selected=null;
			repaint();
		}
		_ink = Const.blue;
	}
	else if(arg.equals("Green")) { 
		if( _selected!=null) {
			//	_draws.elementAt(_selected).setColor(Const.green);	
			for(int a=0;a<_selected.length;a=a+1)
				MyPanel._draws.elementAt(_selected[a]).setColor(Const.blue);	
			_selected=null;
			repaint();
		}
		_ink = Const.green;
	}
	
	else if(arg.equals("Pink")) { 
		if( _selected!=null) {
			for(int a=0;a<_selected.length;a=a+1)
				MyPanel._draws.elementAt(_selected[a]).setColor(Const.blue);	
			_selected=null;
			repaint();
		}
		_ink = Const.pink;
	}
	else if(arg.equals("Delete")&& _selected!=null) {
		//		_draws.removeElementAt(_selected);
		_selected = null;
		repaint();
	}
	else if(arg.equals("Info")&& _selected!=null) {
		for(int a=0;a<_selected.length;a=a+1)
			System.out.println(_selected+") "+MyPanel._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
	 */
	




}




and the problem is with the line:
this.addMouseListener(new mouseManeger());

the error the compile say is:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
mouseManeger cannot be resolved to a type

at MyFrame.Dialog(MyFrame.java:208)
at MyFrame.start(MyFrame.java:93)
at MyFrame.main(MyFrame.java:37)

when i try to tell him to go to MyPannel it doesn't work what i try to do is to move all the class of myframe beside the menu to the mypannel class but i can't - please help...
thanks
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: problem with jmenu and jframe

Posted 04 January 2011 - 06:07 PM

Hey. First off, pbl gave you some fantastic ideas for making your code smaller. I'd follow his advice...

Now, since it's an inner class, you cannot just reference it in the way you have. You need:
MyPanel.MouseManeger manager = myPanelObject.new MouseManager()



That means that you should probably put this in the constructor, not the main():
        MyPanel pane=new MyPanel();
        add(pane);


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2