3 Replies Latest reply on Jul 27, 2004 7:27 AM by Shigeru Chiba

    Modifying jcomponent

    Gordon Hollenbeck Newbie

       

      ClassPool pool = ClassPool.getDefault();
      CtClass cc = pool.get("javax.swing.JComponent");
      CtMethod m = cc.getDeclaredMethod("paint");
      m.insertBefore("((java.awt.Graphics2D)$1).setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON);");
      


      I am trying to modify all jcomponents so that they have antialiasing enabled when they are painted. However, this doesnt quite seem to work. I want this to be a runtime, change. I do not want to write the class to disk and then use it (as it would breach the sun license).

      What am I doing wrong?

        • 1. Re: Modifying jcomponent
          Shigeru Chiba Expert

          This problem should have been fixed in CVS_HEAD.
          Please download the source files from CVS and try again.
          At least, I could run the following code without any problem:

          ClassPool pool = ClassPool.getDefault();
          CtClass cc = pool.get("javax.swing.JComponent");
          CtMethod m = cc.getDeclaredMethod("paint");
          m.insertBefore("((java.awt.Graphics2D)$1).setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON);");
          Class c = cc.toClass(); // I added.
          


          • 2. Re: Modifying jcomponent
            Gordon Hollenbeck Newbie


            Well, now I get errors:

            javassist.CannotCompileException: by java.lang.LinkageError: duplicate class definition: javax/swing/JLabel
             at javassist.CtClass.toClass(CtClass.java:900)
             at javassist.CtClass.toClass(CtClass.java:860)
             at AASwing.main(AASwing.java:40)
            Exception in thread "main"


            The exception is thrown at the Class c = cc.toClass(); line.
            the code is as follows:

            ClassPool pool = ClassPool.getDefault();
             CtClass cc = pool.get("javax.swing.JLabel");
             CtMethod m = cc.getSuperclass().getDeclaredMethod("paint");
             m.insertBefore("((java.awt.Graphics2D)$1).setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON); super.paint($1);");
             Class c = cc.toClass();
            
             JLabel b = (JLabel)c.newInstance();


            How do I replace the JLabel component?

            • 3. Re: Modifying jcomponent
              Shigeru Chiba Expert

              That's because the JVM had loaded JLabel
              before toClass() was called. To avoid this,
              maybe you should write your own class
              loader to run your whole application.
              For details, please take a look at the tutorial.