1

I'm currently searching for a way to keep focus on my window. I use a Controller/View design pattern.

So this is my window : Preview of our app Using some keys we can do some change on our shapes : first ground, second groud, delete,... JmenuBar "Rectangle", "Circle" are used to create new shapes. When you click on it it opens a JColorChooser dialog.

What appear when i click on rectangle

ISSUE : -When we press a key and then we press on rectangle to create a rectangle, the rectangle doesnt appear. -When we press on rectangle, it creates a shape on our windows but we cant use the keys anymore.

I have try some solution posted on stack but i don't find anything working.

public class Editor extends JFrame
{
    ShapesView sview;
    SCollection model=new SCollection();
    ColorChooser cc = new ColorChooser();


    public Editor()
    {   
        super("Shapes Editor");



        this.addWindowStateListener(new WindowStateListener() {


            public void windowStateChanged(WindowEvent e) {

                System.out.println("windowState.newState = " + e.getNewState());
            }
        });


        this.addWindowListener(new java.awt.event.WindowAdapter()
        {
            public void windowClosing(WindowEvent evt)
            {
                System.exit(0);
            }


        });

        this.buildModel();

        this.sview = new ShapesView(this.model);
        this.sview.setPreferredSize(new Dimension(800,800));
        this.getContentPane().add(this.sview, java.awt.BorderLayout.CENTER);

        ImageIcon image = new ImageIcon("..\\img\\APP1.png");
        this.setIconImage(image.getImage());
        createMenuBar();  
    }   


    private void buildModel()
    {
        this.model = new SCollection();
        this.model.addAttributes(new SelectionAttributes());
        SRectangle r = new SRectangle(new Point(50,170),40,30);
        r.addAttributes(new ColorAttributes(true,false,Color.YELLOW,Color.RED));
        r.addAttributes(new SelectionAttributes());
        this.model.add(r);

        SRectangle r2 = new SRectangle(new Point(150,100),190,140);
        r2.addAttributes(new ColorAttributes(true,true,Color.GREEN,Color.GREEN));
        r2.addAttributes(new SelectionAttributes());
        this.model.add(r2);

        SCircle c = new SCircle(new Point(400,400),20);
        c.addAttributes(new ColorAttributes(false,true,Color.BLUE,Color.BLUE));
        c.addAttributes(new SelectionAttributes());
        this.model.add(c);

        SText t= new SText(new Point(100,200),"hello");
        t.addAttributes(new ColorAttributes(true,true,Color.YELLOW,Color.BLUE));
        t.addAttributes(new FontAttributes());
        t.addAttributes(new SelectionAttributes());
        this.model.add(t);

        SCollection sc = new SCollection();
        sc.addAttributes(new SelectionAttributes());
        r= new SRectangle(new Point(20,30),30,30);
        r.addAttributes(new ColorAttributes(true,false,Color.MAGENTA,Color.BLUE));
        r.addAttributes(new SelectionAttributes());
        sc.add(r);
        c = new SCircle(new Point(150,100),40);
        c.addAttributes(new ColorAttributes(false,true,Color.BLUE,Color.DARK_GRAY));
        c.addAttributes(new SelectionAttributes());
        sc.add(c);
        this.model.add(sc);
    }

    public static void main(String[] args)
    {
        Editor frame = new Editor();

        //createColorChooser(frame); //new

        frame.pack();
        frame.setVisible(true);
    }


/************************************ SECTION BARRE DE MENU ************************************/


    public static void createColorChooser(Editor frame)
    {
        ColorChooser cc = new ColorChooser();
        Dialog color= new Dialog(frame, "Color Panel");
        color.setPreferredSize(new Dimension(600, 600));
        color.setLocationRelativeTo(frame);
        color.pack();
        color.add(cc);
        color.setVisible(true);
        color.pack();
    }

    public void createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);

        JMenu menu = new JMenu("File");
        JMenu circle = new JMenu("Circle");
        JMenu rectangle = new JMenu("Rectangle");
        JMenu text = new JMenu("Text");

        menuBar.add(menu);
        menuBar.add(circle);
        menuBar.add(rectangle);
        menuBar.add(text);




        rectangle.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(MenuEvent e) {
                createDefaultRectangle();                               
            }
            @Override
            public void menuDeselected(MenuEvent e) {
            }
            @Override
            public void menuCanceled(MenuEvent e) {
            }
        });

        circle.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(MenuEvent e) {
                createDefaultCircle();                  
            }
            @Override
            public void menuDeselected(MenuEvent e) {
            }
            @Override
            public void menuCanceled(MenuEvent e) {
            }
        });

        text.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(MenuEvent e) {
                createDefaultText();    
            }
            @Override
            public void menuDeselected(MenuEvent e) {
            }
            @Override
            public void menuCanceled(MenuEvent e) {
            }
        });



    }

    public void createDefaultCircle() {
        Color Filled = JColorChooser.showDialog(this.cc,"Choose Circle Fill Color",cc.banner.getBackground());
        Color Stroked = JColorChooser.showDialog(this.cc,"Choose Circle Strock Color",cc.banner.getBackground());
        Rectangle windowBounds= this.getBounds();
        SCircle c = new SCircle(new Point(windowBounds.width/2,windowBounds.height/2), Math.min(windowBounds.width, windowBounds.height)/5);
        c.addAttributes(new ColorAttributes(true,true,Filled,Stroked));
        c.addAttributes(new SelectionAttributes());
        this.model.add(c);
        this.sview.invalidate();

    }   

    public void createDefaultRectangle() {
        Color Filled = JColorChooser.showDialog(this.cc,"Choose Rectangle Fill Color",cc.banner.getBackground());
        Color Stroked = JColorChooser.showDialog(this.cc,"Choose Rectangle Stock Color",cc.banner.getBackground());
        Rectangle windowBounds= this.getBounds();
        SRectangle r = new SRectangle(new Point(windowBounds.width/2,windowBounds.height/2),Math.min(windowBounds.width, windowBounds.height)/5,Math.min(windowBounds.width, windowBounds.height)/5);
        r.addAttributes(new ColorAttributes(true,true,Filled,Stroked));
        r.addAttributes(new SelectionAttributes());
        this.model.add(r);
        this.sview.invalidate();

    }

    public void createDefaultText(){
        Color Filled = JColorChooser.showDialog(this.cc,"Choose Back Color",cc.banner.getBackground());
        Color Stroked = JColorChooser.showDialog(this.cc,"Choose Text Color",cc.banner.getBackground());
        String response = JOptionPane.showInputDialog("What's your text ?");
        Rectangle windowBounds=this.getBounds();
        SText t= new SText(new Point(windowBounds.width/2,windowBounds.height/2),response);
        t.addAttributes(new ColorAttributes(true,true,Filled,Stroked));
        t.addAttributes(new SelectionAttributes());
        t.addAttributes(new FontAttributes());
        this.model.add(t);
        this.sview.invalidate();
    }
    }

0 Answers0