Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Graphics2D.fill vs setClip - different results  (Read 2871 times)
0 Members and 1 Guest are viewing this topic.
Offline CommanderKeith
« Posted 2009-11-10 13:16:21 »

Hi,

I'm finding that Graphics2D.fill(Shape) gives different results to Graphics2D.setClip(Shape) then filling with Graphics2D.fillRect(0 ,0, width, height).

Setting the clip then filling results in a bigger filled area, or sometimes the same size filled area, depending on the floating point dimensions of the shape and the AffineTransform applied to the Graphics2D object.

It's a slight annoyance since I'm getting stray pixels that aren't coloured properly in my game. Just wondering if this is an intentional feature or a strange bug?

Thanks  Smiley
Keith

Here's a little test case illustrationg my point:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import java.awt.image.*;

/**
 *
 * @author Keith
 */

public class FillVsClipTest extends JFrame{
   final ViewPane v;
   volatile boolean keepRunning = true;
   long lastUpdateNanos;

   public FillVsClipTest(){
      setTitle("Graphics2D Fill vs Clip Test");
      setSize(400, 400);
      setLocationRelativeTo(null);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            keepRunning = false;
         }
      });
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      v = new ViewPane(this);
      this.setLayout(new BorderLayout());
      add(v);

      setVisible(true);

      Thread renderThread = new Thread(){
         public void run(){
            lastUpdateNanos = System.nanoTime();
            while(keepRunning){
               long currentNanos = System.nanoTime();
               double secondsSinceLastUpdate = (currentNanos - lastUpdateNanos)/1000000000.0;
               lastUpdateNanos = currentNanos;
               v.update(secondsSinceLastUpdate);
               v.render();
               try{Thread.sleep(1);}catch(InterruptedException e){}
            }
         }
      };
      renderThread.setDaemon(true);
      renderThread.start();
   }

   public class ViewPane extends JComponent{
      FillVsClipTest frame;
      VolatileImage backImage;
      Graphics2D g;
      double x = 0;
      boolean xIncreasing = true;
      double y = 0;
      boolean yIncreasing = true;

      public ViewPane(FillVsClipTest frame){
         this.frame = frame;
      }

      protected VolatileImage createVolatileImage() {
         return createVolatileImage(getWidth(), getHeight(), Transparency.OPAQUE);
      }

      protected VolatileImage createVolatileImage(int width, int height, int transparency) {
         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
         GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
         VolatileImage image = null;

         image = gc.createCompatibleVolatileImage(width, height, transparency);

         int valid = image.validate(gc);

         if (valid == VolatileImage.IMAGE_INCOMPATIBLE) {
            image = this.createVolatileImage(width, height, transparency);
         }
         //System.out.println(this.getClass().getSimpleName() + ": initiated VolatileImage backImage for quick rendering");
        return image;
      }

      public void render() {
         if (getWidth() <= 0 || getHeight() <= 0) {
            System.out.println(this.getClass().getSimpleName() + ": width &/or height <= 0!!!");
            return;
         }
         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
         GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
         if (backImage == null || getWidth() != backImage.getWidth() || getHeight() != backImage.getHeight() || backImage.validate(gc) != VolatileImage.IMAGE_OK) {
            backImage = createVolatileImage();
         }

         do {
            int valid = backImage.validate(gc);
            if (valid == VolatileImage.IMAGE_INCOMPATIBLE) {
               backImage = createVolatileImage();
            }
            g = backImage.createGraphics();
            renderWorld();
            // It's always best to dispose of your Graphics objects.
           g.dispose();
         } while (backImage.contentsLost());
         if (getGraphics() != null) {
            getGraphics().drawImage(backImage, 0, 0, null);
            Toolkit.getDefaultToolkit().sync(); // to flush the graphics commands to the graphics card.  see http://www.java-gaming.org/forums/index.php?topic=15000.msg119601;topicseen#msg119601
        }
      }

      public void update(double seconds){
         double speed = 1;   // pixels per second

         double minY = 0;
         double maxY = 20;
         double yIncrement = speed*seconds;
         if (yIncreasing){
            y += yIncrement;
         }else{
            y -= yIncrement;
         }
         if (y >= maxY){
            yIncreasing = false;
         }else if (y <= minY){
            yIncreasing = true;
         }

         double minX = 0;
         double maxX = 20;
         double xIncrement = speed*seconds;
         if (xIncreasing){
            x += xIncrement;
         }else{
            x -= xIncrement;
         }
         if (x >= maxX){
            xIncreasing = false;
         }else if (x <= minX){
            xIncreasing = true;
         }
      }

      public void renderWorld(){
         g.setColor(Color.BLACK);
         g.fillRect(0, 0, getWidth(), getHeight());
         g.setColor(Color.RED);
         Shape oldClip = g.getClip();
         g.drawString("Graphics2D.fill(rect) is in red", 40, 30);
         g.setColor(Color.WHITE);
         g.drawString("Graphics2D.setClip(rect) is in white", 40, 50);
         //g.scale(4, 4);
        g.translate(x, y);
         {
            float w = 80f;
            float h = 80f;
            Rectangle2D.Float rect = new Rectangle2D.Float(10, 70, w, h);
            Rectangle2D.Float rect2 = new Rectangle2D.Float(10, 180, w, h);

            g.setColor(Color.WHITE);
            g.setClip(rect);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setClip(oldClip);

            g.setColor(Color.RED);
            g.fill(rect);

           
           
            g.setColor(Color.RED);
            g.fill(rect2);

            g.setColor(Color.WHITE);
            g.setClip(rect2);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setClip(oldClip);
         }
         {
            float w = 80.3f;
            float h = 80.3f;
            Rectangle2D.Float rect = new Rectangle2D.Float(150, 70, w, h);
            Rectangle2D.Float rect2 = new Rectangle2D.Float(150, 180, w, h);

            g.setColor(Color.WHITE);
            g.setClip(rect);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setClip(oldClip);

            g.setColor(Color.RED);
            g.fill(rect);



            g.setColor(Color.RED);
            g.fill(rect2);

            g.setColor(Color.WHITE);
            g.setClip(rect2);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setClip(oldClip);
         }
      }
   }

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

}

Offline CommanderKeith
« Reply #1 - Posted 2009-11-12 06:59:58 »

I started learning openGL for the first time through LWJGL and Slick and found this in the red book:

Polygons are drawn in such a way that if adjacent polygons share an edge or vertex, the pixels making up the edge or vertex are drawn exactly once - they're included in only one of the polygons. This is done so that partially transparent polygons don't have their edges drawn twice, which would make those edges appear darker (or brighter, depending on what color you're drawing with).

Maybe java2D uses the same logic for Graphics2D.fill, but not for clipping.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (33 views)
2014-07-18 06:55:21

Zero Volt (29 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (26 views)
2014-07-16 23:30:00

Cero (41 views)
2014-07-16 00:42:17

Riven (43 views)
2014-07-14 18:02:53

OpenGLShaders (31 views)
2014-07-14 16:23:47

Riven (30 views)
2014-07-14 11:51:35

quew8 (29 views)
2014-07-13 13:57:52

SHC (65 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!