Results 1 to 2 of 2

Thread: gui problem

  1. #1
    beni.vd is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default gui problem

    hi,

    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;
    }
    }


    }
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    Or paste your code between a pair of {code} tags.

    db

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •