Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4757223

some JNI-warning reported by -Xcheck:jni option (2D)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.2
    • 1.4.1, 1.4.2
    • client-libs
    • None
    • 2d
    • mantis
    • generic
    • generic



      Name: osR10079 Date: 10/02/2002

      there are a lot JNI-warnings ("Calling other JNI
      functions in the scope of Get/ReleasePrimitiveArrayCritical or
      Get/ReleaseStringCritical") reported when the test is executed with
      -Xcheck:jni option.
      These warnings could be cause of the bug 4726821 (Graphics2DTest.java
      catch null pointer exception). We need check our usage of
      Get/ReleasePrimitiveArrayCritical.

      --- the test ---
      import java.applet.*;

      import java.awt.*;
      import java.awt.event.*;
      import java.awt.font.*;
      import java.awt.geom.*;

      import java.util.*;

      import javax.swing.*;


      public class Graphics2DTest extends Frame {
          _Graphics2DTest frame = null;
          Properties pro = null;
          public static String base;
          public static String str1;
          public static String str2;

          public static void main(String[] args) {
              new Graphics2DTest();
          }

          public Graphics2DTest() {

              base = "0x3000";
              str1 = "0123456789 \u4e24\u5f3a\u4e89\u548c\u4e4b\u4e89+\u3437\u34d4\ua1ad\ua44c\u0f67ue791\ue78d\u4b3a";
              str2 = "Graphics2D:
      \u4f46\u5929\u59cb\u7537\u5341\u8df3\u53f0\u6bd4\u8d5b+\u4b3e\u40e2\ue793\u1816\u0676\u0f37\ua365";

              frame = new _Graphics2DTest();

              add(frame);
              setSize(600, 600);
              setVisible(true);

              /* Toolkit toolkit = Toolkit.getDefaultToolkit();
                        Component root = SwingUtilities.getRoot(this);
                        Dimension sd = toolkit.getScreenSize();
                        root.setSize(400, sd.height - 100);
                        root.setLocation(sd.width - 400, 0); */
          }
      }

      class _Graphics2DTest extends Panel {
          private TextView textView;

          public _Graphics2DTest() {
              setLayout(new BorderLayout());
              textView = new TextView();
              add(new FontSelPanel(textView), BorderLayout.NORTH);
              add(textView, BorderLayout.CENTER);
              add(new ControlPanel(textView), BorderLayout.SOUTH);
          }
      }

      class FontSelPanel extends Panel implements ItemListener {
          static FontStyle[] fontStyles = {
              new FontStyle("Plain", Font.PLAIN), new FontStyle("Bold", Font.BOLD),
              new FontStyle("Italic", Font.ITALIC),
              new FontStyle("BoldItalic", Font.BOLD + Font.ITALIC)
                  };
          static final int[] fontSizes = {
              8, 9, 10, 12, 14, 16, 18, 20, 24, 28, 32, 36, 40, 48, 54, 60, 72
          };
          static final String[] javaFonts = {
              "serif", "sansserif", "monospaced", "dialog", "dialoginput"
          };
          TextView target;
          Choice cFontName;
          Choice cFontStyle;
          Choice cFontSize;

          public FontSelPanel(TextView target) {
              super();
              setBackground(Color.lightGray);
              this.target = target;

              // FontName Choice
              cFontName = new Choice();

              /* old style */

              //String[] flist = getToolkit().getFontList();
              //for (int i=0; i<flist.length; i++) {
              // cFontName.addItem(flist[i]);
              //}
              for (int i = 0; i < javaFonts.length; i++) {
                  cFontName.addItem(javaFonts[i]);
              }

              GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
              Font[] fonts = ge.getAllFonts();

              for (int i = 0; i < fonts.length; i++) {
                  if (fonts[i].canDisplay('\u3042')) {
                      cFontName.addItem(fonts[i].getName());
                      System.out.println("can display: " + '\u3042');
                  }
              }

              cFontName.addItemListener(this);
              add(cFontName);

              // FontStyle Choice
              cFontStyle = new Choice();

              for (int i = 0; i < fontStyles.length; i++) {
                  cFontStyle.addItem(fontStyles[i].name);
              }

              cFontStyle.addItemListener(this);
              add(cFontStyle);

              // FontSize Choice
              cFontSize = new Choice();

              for (int i = 0; i < fontSizes.length; i++) {
                  cFontSize.addItem(Integer.toString(fontSizes[i]));
              }

              cFontSize.addItemListener(this);
              add(cFontSize);
              target.setFont(getSelFont());
          }

          public Font getSelFont() {
              return new Font(cFontName.getSelectedItem(),
                              fontStyles[cFontStyle.getSelectedIndex()].style,
                              fontSizes[cFontSize.getSelectedIndex()]);
          }

          public void itemStateChanged(ItemEvent e) {
              target.setFont(getSelFont());
          }
      }

      class ControlPanel extends Panel implements ItemListener, ActionListener {
          TextView target;
          Choice cAntiAlias;
          Choice cRendering;
          Choice cTextAntiAlias;

          public ControlPanel(TextView target) {
              super();
              setBackground(Color.lightGray);
              this.target = target;

              // ANTIALIASING Choice
              add(new Label("Antialiasing:"));
              cAntiAlias = new Choice();
              cAntiAlias.addItem("On");
              cAntiAlias.addItem("Off");
              cAntiAlias.addItemListener(this);
              add(cAntiAlias);

              // RENDERING Choice
              add(new Label("Rendering:"));
              cRendering = new Choice();
              cRendering.addItem("Quality");
              cRendering.addItem("Speed");
              cRendering.addItemListener(this);
              add(cRendering);

              // TEXT_ANTIALIASING Choice
              add(new Label("TextAntialiasing:"));
              cTextAntiAlias = new Choice();
              cTextAntiAlias.addItem("On");
              cTextAntiAlias.addItem("Off");
              cTextAntiAlias.addItemListener(this);
              add(cTextAntiAlias);
              target.setOption(RenderingHints.VALUE_ANTIALIAS_ON,
                               RenderingHints.VALUE_RENDER_QUALITY,
                               RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

              Button btn = new Button("Prev");
              add(btn);
              btn.addActionListener(this);
              btn = new Button("Next");
              add(btn);
              btn.addActionListener(this);
          }

          public void itemStateChanged(ItemEvent e) {
              Object antialias;
              Object rendering;
              Object textAntialias;

              if (cAntiAlias.getSelectedIndex() == 0)
                  antialias = RenderingHints.VALUE_ANTIALIAS_ON;
              else
                  antialias = RenderingHints.VALUE_ANTIALIAS_OFF;

              if (cRendering.getSelectedIndex() == 0)
                  rendering = RenderingHints.VALUE_RENDER_QUALITY;
              else
                  rendering = RenderingHints.VALUE_RENDER_SPEED;

              if (cTextAntiAlias.getSelectedIndex() == 0)
                  textAntialias = RenderingHints.VALUE_TEXT_ANTIALIAS_ON;
              else
                  textAntialias = RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;

              target.setOption(antialias, rendering, textAntialias);
          }

          public void actionPerformed(ActionEvent e) {
              String label = e.getActionCommand();

              if (label.equals("Prev")) {
                  target.prevChar();
              } else {
                  target.nextChar();
              }
          }
      }

      class TextView extends Canvas {
          private String text;
          private Font font;
          private Object AntiAlias = RenderingHints.VALUE_ANTIALIAS_ON;
          private Object Rendering = RenderingHints.VALUE_RENDER_QUALITY;
          private Object TextAntiAlias = RenderingHints.VALUE_TEXT_ANTIALIAS_ON;
          private char cBase = 0x3000;

          public TextView() {
              super();
              text = Graphics2DTest.str1;
              font = new Font("Dialog", Font.PLAIN, 12);

              String bstr = Graphics2DTest.base;

              try {
                  int index = Graphics2DTest.base.indexOf("x");

                  if (index != -1) {
                      bstr = bstr.substring(index + 1);
                  } else {
                      index = Graphics2DTest.base.indexOf("X");

                      if (index != -1) {
                          bstr = bstr.substring(index + 1);
                      }
                  }

                  int tmp = Integer.valueOf(bstr, 16).intValue();
                  cBase = (char)tmp;
              } catch (NumberFormatException e) {
                  e.printStackTrace();
              }
          }

          public void setOption(Object antialias, Object rendering,
                                Object textAntialias) {
              this.AntiAlias = antialias;
              this.Rendering = rendering;
              this.TextAntiAlias = textAntialias;
              repaint();
          }

          public void setText(String newtext) {
              this.text = newtext;
              repaint();
          }

          public void prevChar() {
              cBase = (char)(cBase - 256);
              repaint();
          }

          public void nextChar() {
              cBase = (char)(cBase + 256);
              repaint();
          }

          public void setFont(Font newfont) {
              this.font = newfont;
              repaint();
          }

          public void paint(Graphics g) {
              Graphics2D g2d = (Graphics2D)g;
              int w = getSize().width;
              int h = getSize().height;
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
              g2d.setRenderingHint(RenderingHints.KEY_RENDERING, Rendering);
              g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                                   TextAntiAlias);
              g2d.setFont(font);

              FontMetrics fm = g2d.getFontMetrics();
              int ch = fm.getHeight();

              // drawString
              g.drawString("drawString of Graphics : " + font.toString(), 5, (int)ch);
              g.drawString(text, 10, (int)ch * 2);

              // drawString(2D)
              g2d.setColor(Color.red);
              g2d.drawString("drawString of Graphics2D : " + font.toString(), 5.0f,
                             ch * 3);
              g2d.drawString(text, 10.0f, ch * 4);
              g2d.setColor(Color.black);

              char[] cText = new char[32]; // 16*2

              for (int i = 0; i < 16; i++) {
                  for (int j = 0; j < 16; j++) {
                      cText[j * 2] = (char)(cBase + i * 16 + j);
                      cText[j * 2 + 1] = ' ';
                  }

                  g2d.drawString(new String(cText), 10.0f, ch * (6 + i));
              }

              AffineTransform at = new AffineTransform();
              at.setToTranslation(w / 2, h / 2);
              g2d.setTransform(at);
              g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                                                          0.3f));
              g2d.setColor(Color.yellow);

              Rectangle rect = new Rectangle(10, 10, 200, 50);
              g2d.fill(rect);

              //
              at.setToRotation(Math.PI / 4.0); // 45
              g2d.transform(at);
              g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                                                          0.5f));
              g2d.setColor(Color.green);
              g2d.fill(rect);
              g2d.setComposite(AlphaComposite.SrcOver);
              g2d.setColor(Color.blue);
              g2d.setFont(font.deriveFont(20.0f));

              for (int i = 0; i < 8; i++) {
                  g2d.drawString(Graphics2DTest.str2, 10.0f, 10.0f);
                  g2d.transform(at);
              }
          }
      }

      class FontStyle {
          public String name;
          public int style;

          public FontStyle(String name, int style) {
              this.name = name;
              this.style = style;
          }
      }
      --- end the test ---
      ======================================================================

            dougfelt Doug Felt
            son Oleg Sukhodolsky (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: