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

drawLine method on graphics not working properly

XMLWordPrintable

    • 2d
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Windows, Ubuntu at least
      OpenJDK at least

      A DESCRIPTION OF THE PROBLEM :
      When calling the methods drawLine(x1,y1,x2,y2) on a Buffered image of type byte binary, for graphics 2d not being draw correctly,

      @Override
      public void paint(Graphics g1) {
      BufferedImage colored = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_BYTE_BINARY);

      Graphics2D g = colored.createGraphics();
      g.setColor(Color.white);
      g.fillRect(0, 0, getWidth(), getHeight());
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
      g.setColor(Color.black);

      g.drawLine(40, 10, 0, 10);
      g.drawLine(80, 21, 10, 20);
      g.drawLine(120, 32, 20, 30);
      g.drawLine(160, 43, 30, 40);
      g.drawLine(50, 50, 200, 54);


      g1.drawImage(colored, 0, 0, 300, 300, 0, 0, 300, 300, null);
      }

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create e JPanel inside a JFrame overriding paint method. Code bellow

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      complete lines
      ACTUAL -
      dashed lines

      ---------- BEGIN SOURCE ----------
      import java.awt.Color;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.RenderingHints;
      import java.awt.image.BufferedImage;

      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.WindowConstants;

      public class JTestFrame extends JFrame {

      public JTestFrame() {
      super();
      JTestPanel a = new JTestPanel();
      a.setSize(300, 300);
      a.setBackground(Color.BLACK);
      add(a);
      }


      public static void main(String[] args) {
      JTestFrame t = new JTestFrame();
      t.setSize(300, 300);
      t.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      t.setVisible(true);
      }

      private class JTestPanel extends JPanel {

      @Override
      public void paint(Graphics g1) {
      BufferedImage colored = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_BYTE_BINARY);

      Graphics2D g = colored.createGraphics();
      g.setColor(Color.white);
      g.fillRect(0, 0, getWidth(), getHeight());
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
      g.setColor(Color.black);

      g.drawLine(40, 10, 0, 10);
      g.drawLine(80, 21, 10, 20);
      g.drawLine(120, 32, 20, 30);
      g.drawLine(160, 43, 30, 40);
      g.drawLine(50, 50, 200, 54);


      g1.drawImage(colored, 0, 0, 300, 300, 0, 0, 300, 300, null);
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      this only happens when y1>y2, the workaround it to switch y2 for y1 and the respective x1 and x2

        1. Capture.PNG
          4 kB
          Praveen Narayanaswamy
        2. Capture_JDK20ea20.PNG
          6 kB
          Praveen Narayanaswamy
        3. JTestFrame.java
          1 kB
          Praveen Narayanaswamy

            prr Philip Race
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: