This site requires JavaScript, please enable it in your browser!
Greenfoot back
Pointifix
Pointifix wrote ...

2014/3/13

Mistake at drawing?

1
2
Pointifix Pointifix

2014/3/13

#
Hi I cant unterstand why this is drawing a square by SIZE_RASTER * SIZE_RASTER onto my image, it should draw a "halfpipe", does anyone find something wrong with this?
1
2
3
4
image_item[6].setColor(Color.black);
image_item[6].fill();
image_item[6].setColor(new Color(0,0,0,0));
image_item[6].fillOval(-SIZE_RASTER,0,SIZE_RASTER * 2,SIZE_RASTER * 2);
Pointifix Pointifix

2014/3/13

#
or how to i paint transparent? cause setColor(new Color(0); and then image.fill(); dont work?
danpost danpost

2014/3/13

#
Unfortunately, there are not any methods called 'clearOval' and 'clearRect' (though, they would be very useful). A newly created image is made totally transparent and one that 'clear' is called on becomes totally transparent. Once a pixel becomes non-transparent, the only way to change it back to transparent (without 'clear'ing the entire image) is to use 'setColorAt' on each pixel you want to revert back to transparent.
Pointifix Pointifix

2014/3/14

#
setColorAt to what color should i set?
danpost danpost

2014/3/14

#
Pointifix wrote...
setColorAt to what color should i set?
java.awt.Color transparent = new java.awt.Color(0, 0, 0, 0);
Pointifix Pointifix

2014/3/14

#
i did this, still getting the square as i told on the top
danpost danpost

2014/3/14

#
Pointifix wrote...
i did this, still getting the square as i told on the top
It would help if you show the code you are trying to use. What shape is this 'halfpipe' that you want to draw (I presume the rest of the image is to be transparent)?
Pointifix Pointifix

2014/3/14

#
i want to get that: ***********X *********XX ******XXXX XXXXXXXX * is transparent X is a color (it should be a 1/4 circle shape
danpost danpost

2014/3/14

#
You could fill the image with the exeterior color and change all the pixels within the quarter circle back to transparent. On the other hand, you could start with the clear image and change all the exterior pixels to the exterior color (which would be a lot less pixel setting).
1
2
3
4
5
6
7
8
public GreenfootImage getClearQuarterCircle(int radius, java.awt.Color exteriorColor)
{
    GreenfootImage image = new GreenfootImage(radius, radius);
    for (int y=0; y<radius; y++)
        for (x=radius-1; x>= 0; x--)
            if (Math.hypot(x, y) > radius) image.setColorAt(x, y, exteriorColor); else break;
    return image;
}
You could probably cut down a little on CPU time by determining the first pixel on each line that is outside the circle and use the 'drawLine' method instead.
1
2
3
4
5
6
7
public GreenfootImage getClearQuarterCircle(int radius, java.awt.Color exteriorColor)
{
    GreenfootImage image = new GreenfootImage(radius, radius);
    image.setColor(exteriorColor);
    for (int y=0; y<radius; y++) image.drawLine((int)(Math.sqrt(radius*radius-y*y)+1), y, radius, y);
    return image;
}
Pointifix Pointifix

2014/3/14

#
mhhmm i thought filling and then drawing a circle with transparent does take less time than computing the hypot all time
danpost danpost

2014/3/14

#
Pointifix wrote...
mhhmm i thought filling and then drawing a circle with transparent does take less time than computing the hypot all time
The 'drawOval' and 'fillOval' methods need to compute the hypot (or at least somehow determine where the border between the interior and the exterior of the oval is) anyway. So, there is little difference in those methods calling it or you calling it directly and using a different method to draw. At any rate, if you fill the image, that is 100 percent of the pixels set; plus, then change the interior of the oval to transparent, for a regular oval, or a circle, that is 67 percent more pixels; you are setting 167 percent of the pixels to get your image. If you just do the exterior, you set only about 33 percent of the pixels. That is only 20 percent of the number of pixels you set otherwise. Just to show the code using transparent:
1
2
3
4
5
6
7
8
9
public GreenfootImage getClearQuarterCircle(int radius, java.awt.Color exteriorColor)
{
    GreenfootImage image = new GreenfootImage(radius, radius);
    image.setColor(exteriorColor);
    image.fill();
    java.awt.Color trans = new java.awt.Color(0, 0, 0, 0);
    for (int y=0; y<radius; y++) for (int x=0;  ; x++)
        if (Math.hypot(x, y) <= radius) image.setColorAt(x, y, trans); else break;
}
Pointifix Pointifix

2014/3/14

#
jeah that work but does not look the same way as a real halfpipe
danpost danpost

2014/3/14

#
Pointifix wrote...
jeah that work but does not look the same way as a real halfpipe
What do you mean?
Pointifix Pointifix

2014/3/14

#
still these lines wont let me get the same!
1
2
image_item[6].setColor(new Color(0,0,0,0));
image_item[6].fillOval(-SIZE_RASTER,0,SIZE_RASTER * 2,SIZE_RASTER * 2);
but why?
Pointifix Pointifix

2014/3/14

#
it looks a little bit different, the ends of the halfpipe or not fully drawn, so the look like these: *************** *************X **********XXX *****XXXXXX
There are more replies on the next page.
1
2