Java SWT - Canvas: why delete images ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zildjian
    New Member
    • Oct 2007
    • 9

    Java SWT - Canvas: why delete images ?

    Hello,
    I have a problem.
    I use SWT to make GUI.
    In the main window I have drawed some images (canvas, gc on canvas) and pressing a button will appear a new window. When I I move the new window over the main window the images will disappear. Why ?

    Sorry for my english :)
    I hope that you will understand.

    Thank you
    Carmine

    Ps: if you want i can send an image
    Attached Files
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by zildjian
    Hello,
    I have a problem.
    I use SWT to make GUI.
    In the main window I have drawed some images (canvas, gc on canvas) and pressing a button will appear a new window. When I I move the new window over the main window the images will disappear. Why ?

    Sorry for my english :)
    I hope that you will understand.

    Thank you
    Carmine

    Ps: if you want i can send an image
    Let's see the code.

    Comment

    • zildjian
      New Member
      • Oct 2007
      • 9

      #3
      public class grafica extends javax.swing.JFr ame {
      Shell shell;
      Text text;
      //GC gc;
      public grafica() throws IOException, Exception {
      Menu menuBar, fileMenu, helpMenu;
      MenuItem fileMenuHeader, aboutMenuHeader ;
      MenuItem fileExitItem, aboutAboutItem;

      display = new Display();
      shell = new Shell(display);
      shell.setText(" Composer");

      menuBar = new Menu(shell, SWT.BAR);
      fileMenuHeader = new MenuItem(menuBa r, SWT.CASCADE);
      fileMenuHeader. setText("&File" );
      fileMenu = new Menu(shell, SWT.DROP_DOWN);
      fileMenuHeader. setMenu(fileMen u);

      MenuItem expertItem = new MenuItem(fileMe nu, SWT.CASCADE);
      expertItem.setT ext("&Expert");
      Menu submenu = new Menu(shell, SWT.DROP_DOWN);
      expertItem.setM enu(submenu);
      MenuItem expertParseInIt em = new MenuItem(submen u, SWT.PUSH);
      expertParseInIt em.setText("Par se Input\tCTRL+P") ;
      expertParseInIt em.setAccelerat or(SWT.CTRL + 'P');
      MenuItem dialogItem = new MenuItem(submen u, SWT.PUSH);
      dialogItem.setT ext("&Parse Output\tCTRL+O" );
      dialogItem.setA ccelerator(SWT. CTRL + 'O');
      MenuItem separator = new MenuItem(fileMe nu, SWT.SEPARATOR);

      fileExitItem = new MenuItem(fileMe nu, SWT.PUSH);
      fileExitItem.se tText("E&xit\tC TRL+C");
      fileExitItem.se tAccelerator(SW T.CTRL + 'C');

      aboutMenuHeader = new MenuItem(menuBa r, SWT.CASCADE);
      aboutMenuHeader .setText("Info" );
      helpMenu = new Menu(shell,SWT. DROP_DOWN);
      aboutMenuHeader .setMenu(helpMe nu);
      aboutAboutItem = new MenuItem(helpMe nu, SWT.PUSH);
      aboutAboutItem. setText("Info") ;

      fileExitItem.ad dSelectionListe ner(new MenuItemListene r());
      aboutAboutItem. addSelectionLis tener(new MenuItemListene r());
      expertParseInIt em.addSelection Listener(new MenuItemListene r());

      Rectangle screen = display.getMoni tors()[0].getBounds();

      shell.setMenuBa r(menuBar);
      // Creazione ToolBar
      Image image = new Image(display," img/Parse.jpg");
      Image image2 = new Image(display," img/Help.jpg");
      //GC gc = new GC(image);
      //GC gc2 = new GC(image2);
      //gc.dispose();
      //gc2.dispose();
      ToolBar toolBar = new ToolBar(shell, SWT.NO);

      ToolItem itemParse = new ToolItem(toolBa r, SWT.PUSH);
      itemParse.setIm age(image);
      itemParse.setTe xt("Parse In");
      ToolItem itemHelp = new ToolItem(toolBa r, SWT.PUSH);
      itemHelp.setIma ge(image2);
      itemHelp.setTex t("Help");
      //itemHelp.addSel ectionListener( new ToolBarButtonLi stener());


      itemParse.addSe lectionListener (new SelectionAdapte r() {
      public void widgetSelected( SelectionEvent e2) {
      System.out.prin tln("E' stato premuto il tasto ParseIn");
      RWmidi midi = null;
      try {
      midi = new RWmidi("midi.mi d");
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTra ce();
      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTra ce();
      }
      Parser parser = new Parser(midi);
      String eventi = parser.getParsi ngTraccia(1);
      Shell dialog = new Shell(shell);
      dialog.setText( "Dialog");
      Text text4 = new Text(dialog, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
      text4.setBounds (0,0,800,500);
      text4.setEditab le(false);
      text4.setText(e venti);
      dialog.setSize( 800, 500);
      dialog.open();
      }
      });



      itemHelp.addSel ectionListener( new SelectionAdapte r() {
      public void widgetSelected( SelectionEvent e) {
      System.out.prin tln("E' stato premuto il tasto Help");
      MessageBox aboutBox = new MessageBox(shel l,SWT.ICON_INFO RMATION);
      aboutBox.setTex t("Informazioni ");
      StringBuffer messaggio = new StringBuffer();
      messaggio.appen d("Composer Midi\n");
      messaggio.appen d("Programma sviluppato da Fedullo Carmine\n");
      messaggio.appen d("\n");
      messaggio.appen d("Per ulteriori informazioni\n" );
      messaggio.appen d("carmine82@gm ail.com\n");
      aboutBox.setMes sage(messaggio. toString());
      int buttonOK = aboutBox.open() ;
      //shell.pack();
      shell.open();
      }
      });
      toolBar.pack();

      // Creazione Separatore
      Label separatore = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) ;
      separatore.setB ounds(0,83,1000 ,20);

      // Creazione Cavas
      Canvas canvas = new Canvas(shell, SWT.BORDER);
      canvas.setSize( 950, 500);
      canvas.setLocat ion(20, 120);

      Point size = shell.computeSi ze(-1,-1);
      shell.setBounds (0, 0, 0, 0);
      //shell.setSize(s creen.width,scr een.height-30);
      shell.setSize(1 000,700);
      shell.open();
      Color black = display.getSyst emColor(SWT.COL OR_BLACK);
      Color white = display.getSyst emColor(SWT.COL OR_WHITE);
      //canvas.setBackg round(white);
      //canvas.setBackg roundMode(2);
      gc = new GC(canvas);


      //canvas.setBackg round(white);

      gc.setBackgroun d(white);
      gc.setForegroun d(black);
      gc.fillRectangl e(0, 0, 1000, 900);

      //gc.setLineWidth (2);
      // Costruzione Pentagramma
      gc.drawLine(30, 50, 930, 50);
      //gc.drawLine(30, 60, 930, 60);
      //gc.drawLine(30, 70, 930, 70);
      //gc.drawLine(30, 80, 930, 80);
      //gc.drawLine(30, 90, 930, 90);
      costruisciPenta gramma(50,0);
      costruisciPenta gramma(120,0);
      costruisciPenta gramma(190,0);
      costruisciPenta gramma(260,0);
      costruisciPenta gramma(330,0);
      costruisciPenta gramma(400,0);
      gc.dispose();
      //canvas.redraw() ;
      // shell.pack();


      while(!shell.is Disposed())
      if(!display.rea dAndDispatch())
      display.sleep() ;
      display.dispose ();

      }
      /*
      class ToolBarButtonLi stener extends SelectionAdapte r {
      public void widgetSelected( SelectionEvent event) {
      if(event.widget .getData().)
      shell.close();
      System.exit(0);
      }
      }
      */

      public void costruisciPenta gramma(int altezza, int chiave) {
      // Chiave di Basso
      if(chiave==0) {
      Image img = new Image(display, "img/chiavebasso80.j pg");
      gc.drawImage(im g, 25, altezza-5);
      }
      gc.drawLine(30, altezza, 30, altezza+40);
      gc.drawLine(30, altezza, 930, altezza);
      gc.drawLine(30, altezza+10, 930, altezza+10);
      gc.drawLine(30, altezza+20, 930, altezza+20);
      gc.drawLine(30, altezza+30, 930, altezza+30);
      gc.drawLine(30, altezza+40, 930, altezza+40);
      gc.drawLine(300 , altezza,300 ,altezza+40);
      gc.drawLine(510 , altezza,510 ,altezza+40);
      gc.drawLine(720 , altezza,720 ,altezza+40);
      gc.drawLine(930 , altezza,930 ,altezza+40);

      }
      public void newWindow(Strin g titolo, String messaggio) {
      Shell dialog = new Shell(shell);
      dialog.setText( "Dialog");
      Text text4 = new Text(dialog, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
      text4.setBounds (0,0,800,500);
      text4.setEditab le(false);
      text4.setText(" ciao provola affumicata");
      dialog.setSize( 800, 500);
      dialog.open();
      }
      class MenuItemListene r extends SelectionAdapte r {
      public void widgetSelected( SelectionEvent event) {
      if (((MenuItem) event.widget).g etText().equals ("E&xit\tCTRL+C ")) {
      shell.close();
      System.exit(0);
      }
      if (((MenuItem) event.widget).g etText().equals ("Info")) {
      MessageBox aboutBox = new MessageBox(shel l,SWT.ICON_INFO RMATION);
      aboutBox.setTex t("Informazioni ");
      StringBuffer messaggio = new StringBuffer();
      messaggio.appen d("Composer Midi\n");
      messaggio.appen d("Programma sviluppato da Fedullo Carmine\n");
      messaggio.appen d("\n");
      messaggio.appen d("Per ulteriori informazioni\n" );
      messaggio.appen d("carmine82@gm ail.com\n");
      aboutBox.setMes sage(messaggio. toString());
      int buttonOK = aboutBox.open() ;
      //shell.pack();
      shell.open();
      System.out.prin tln("premuto help");
      }
      if (((MenuItem) event.widget).g etText().equals ("Parse Input\tCTRL+P") ) {
      RWmidi midi = null;
      try {
      midi = new RWmidi("midi.mi d");
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTra ce();
      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTra ce();
      }
      Parser parser = new Parser(midi);
      String eventi = parser.getParsi ngTraccia(1);
      Shell dialog = new Shell(shell);
      dialog.setText( "Dialog");
      Text text4 = new Text(dialog, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
      text4.setBounds (0,0,800,500);
      text4.setEditab le(false);
      text4.setText(e venti);
      dialog.setSize( 800, 500);
      dialog.open();


      }
      }
      }
      private GC gc;
      private Display display;
      }

      Comment

      • zildjian
        New Member
        • Oct 2007
        • 9

        #4
        This occur even when I move another application window over images as you can see by attached picture.
        Attached Files

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Either use Swing components or SWT components but not both.

          kind regards,

          Jos

          Comment

          • zildjian
            New Member
            • Oct 2007
            • 9

            #6
            Originally posted by JosAH
            Either use Swing components or SWT components but not both.

            kind regards,

            Jos
            Dear Jos,
            I use only SWT in my code.
            Where do you see Swing components ?

            Thank you,
            Carmine

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by zildjian
              Dear Jos,
              I use only SWT in my code.
              Where do you see Swing components ?

              Thank you,
              Carmine
              What does your class extend?

              Comment

              • zildjian
                New Member
                • Oct 2007
                • 9

                #8
                Originally posted by r035198x
                What does your class extend?

                mmm ... sorry I am distract :)

                and now I must change all code ?
                What is the class to extend about SWT ?

                I have deleted "extends javax.swing.JFr ame" but it is the same about my problem.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by zildjian
                  mmm ... sorry I am distract :)

                  and now I must change all code ?
                  What is the class to extend about SWT ?

                  I have deleted "extends javax.swing.JFr ame" but it is the same about my problem.
                  If you want to use AWT then you use Frame instead of JFrame. If you want to use swing then there's lots of things you want to change in your code. You have read something about AWT vs swing haven't you? You have to use the right tool for the right job.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    If you want to use AWT then you use Frame instead of JFrame. If you want to use swing then there's lots of things you want to change in your code. You have read something about AWT vs swing haven't you? You have to use the right tool for the right job.
                    It's a bit more complicated here: the OP wants to use SWT, not AWT; I guess
                    that some of the imports in the source are incorrect; we can't know because we
                    haven't seen the imports.

                    kind regards,

                    Jos

                    Comment

                    • zildjian
                      New Member
                      • Oct 2007
                      • 9

                      #11
                      Originally posted by JosAH
                      It's a bit more complicated here: the OP wants to use SWT, not AWT; I guess
                      that some of the imports in the source are incorrect; we can't know because we
                      haven't seen the imports.

                      kind regards,

                      Jos
                      Sorry if my question is stupid but I am learning SWT only since yesterday.

                      My imports are:

                      import java.io.IOExcep tion;
                      import org.eclipse.swt .events.Selecti onAdapter;
                      import org.eclipse.swt .events.Selecti onEvent;
                      import org.eclipse.swt .graphics.Color ;
                      import org.eclipse.swt .graphics.GC;
                      import org.eclipse.swt .graphics.Image ;
                      import org.eclipse.swt .graphics.Point ;
                      import org.eclipse.swt .graphics.Recta ngle;
                      import org.eclipse.swt .widgets.*;
                      import org.eclipse.swt .*;

                      I have deleted "extends java.awt.Frame" and I have the same previous fault.

                      Thank you
                      Carmine

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by JosAH
                        It's a bit more complicated here: the OP wants to use SWT, not AWT; I guess
                        that some of the imports in the source are incorrect; we can't know because we
                        haven't seen the imports.

                        kind regards,

                        Jos
                        Yep, I realized after posting that it's SWT not AWT that they want.

                        Comment

                        • zildjian
                          New Member
                          • Oct 2007
                          • 9

                          #13
                          Dear Sirs,
                          please can you confirm that I am using only SWT ?

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by zildjian
                            Dear Sirs,
                            please can you confirm that I am using only SWT ?
                            javax.swing.JFr ame is swing.

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by r035198x
                              javax.swing.JFr ame is swing.
                              Have a look at reply #11; all the Swing stuff is gone now; I gotta run now; I'll look
                              at this thread later.

                              kind regards,

                              Jos

                              Comment

                              Working...