0

In the following code changing fill to draw produces unexpected results. The attached image demonstrates the undesired but much appreciated postmodern effect caused by drawing the red and green rectangles.

The affine transform should not be part of the problem, but as Holmes said, once you rule out all other possibilities ... So, I will explain the transform. I solved a bunch of equations to figure out how to make the window show a cartesian coordinate system with (-2, -2) in the lower left and (+2, +2) in the upper right.

This is a self-contained example. Try changing any of the draws to a fill or vice versa to achieve your own custom art that you can frame in the Oracle office.

I am using Java SE 7, JDK 1.7.0_21!

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class G {

  public static void main (String [] args) {
    JFrame frame = new JFrame(G.class.getCanonicalName());
    JComponent component = new JComponent() {
      private static final long serialVersionUID = 1L;

      @Override
      protected void paintComponent (Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        AffineTransform xform = new AffineTransform(getWidth() / 4, 0, 0, - getHeight() / 4, getWidth() / 2, getHeight() / 2);
        g2.setTransform(xform);
        paint2D(g2);
        g2.dispose();
      }

      protected void paint2D (Graphics2D g2) {
        g2.setColor(Color.RED);
        g2.draw(new Rectangle2D.Double( 0,  0, 1, 1));
        g2.setColor(Color.GREEN);
        g2.draw(new Rectangle2D.Double(-1, -1, 1, 1));
        g2.setColor(Color.BLUE);
        g2.fill(new Rectangle2D.Double(-1,  0, 1, 1));
        g2.setColor(Color.YELLOW);
        g2.fill(new Rectangle2D.Double( 0, -1, 1, 1));
      }
    };
    frame.setLayout(new BorderLayout());
    frame.add(component, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setVisible(true);
  }
}

graphics2_postmodern.png

Makoto
  • 104,088
  • 27
  • 192
  • 230
necromancer
  • 23,916
  • 22
  • 68
  • 115
  • And ..what is your question? – Andrew Thompson May 14 '13 at 05:44
  • @AndrewThompson I thought `fill` and `draw` should produce the same rectangle...? – necromancer May 14 '13 at 05:45
  • @randomstring , Fill and Draw are both different , Fill will color the whole rectangle, and Draw just Outline the rectangle with color – akki0996 May 14 '13 at 05:51
  • @CRazyProgrammer yes it is the stroke width getting magnified like crazy! – necromancer May 14 '13 at 05:55
  • @randomstring , oke first tell me, what you exactly looking for? – akki0996 May 14 '13 at 06:01
  • oo I c, the fill and draw both have same results, that's makes you confused. let me check ...!! – akki0996 May 14 '13 at 06:04
  • *"I thought fill and draw should produce the same rectangle...?"* Putting a '?' on the end of a statement does not make it a question. Sheesh, it ain't rocket science.. – Andrew Thompson May 14 '13 at 06:08
  • @AndrewThompson the opening sentence in my original question says: "changing fill to draw produces UNEXPECTED results" the next sentence says: "the attached image demonstrates the UNDESIRED ... results". – necromancer May 14 '13 at 06:54
  • 1
    to help the frustrated readers empathize, this happened after a long slog to correctly scale the drawing area to a mathematical coordinate system ranging from -1 to +1 with the origin at lower left. while figuring out the scaling i took somebody's help who used fill in their code rather than draw (without explaining why they edited my code to use fill rather than draw). i could not recreate their solution in my code until i flipped draw to fill, and then this question arose. i have to say today has been a wtf day for java swing and graphics. – necromancer May 14 '13 at 06:59
  • for art purists, this piece came about organically. it was the actual test case i was using before knowing about the bug and before knowing it would show up this way. i started with 4 fills and started changing to a draw and then this happened without my knowing the cause until after i posted the question. it wasn't contrived for attention :) even the sequence of the fills changed to draws is original - with other sequences graphics2d would have interpreted it differently. – necromancer May 14 '13 at 20:51

1 Answers1

3

it is the 1 unit default stroke setting. i increased the coordinate space from -20 to +20 in each dimension and drew 10 unit wide and high rectangles which confirmed the problem as the image below shows.

less modern

necromancer
  • 23,916
  • 22
  • 68
  • 115