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

2019/8/13

yin yang

ronald ronald

2019/8/13

#
import greenfoot.*;
import java.awt.Graphics;
import java.awt.Color;
 // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */

    private final int size = 0;
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1);
        
        prepare();
    }
             
    public void drawYinYang(final Graphics graphics)
    {
        
        final Color colorSave = graphics.getColor();
        
        graphics.setColor(Color.WHITE);
        // Use fillOval to draw a filled in circle
        graphics.fillOval(0, 0, size-1, size-1);

        graphics.setColor(Color.BLACK);
        // Use fillArc to draw part of a filled in circle
        graphics.fillArc(0, 0, size-1, size-1, 270, 180);
        graphics.fillOval(size/4, size/2, size/2, size/2);

        graphics.setColor(Color.WHITE);
        graphics.fillOval(size/4, 0, size/2, size/2);
        graphics.fillOval(7*size/16, 11*size/16, size/8, size/8);

        graphics.setColor(Color.BLACK);
        graphics.fillOval(7*size/16, 3*size/16, size/8, size/8);
        // Use drawOval to draw an empty circle for the outside border
        graphics.drawOval(0, 0, size-1, size-1);
        
        graphics.setColor(colorSave);

    }
    
     /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        YinYang yinYang = new YinYang();
        addObject(yinYang,200,200);
    }
}
I'm trying to draw a yin yang but I do not know how to finish this code that I found, Thank you for your help
danpost danpost

2019/8/13

#
To draw one, I would start by drawing a black circle on a square white image; then fill the right half of the image white (again). That will produce a left-sided semi-circle. This would be followed by filling half-sized circles of opposite colors at top and bottom center, and then the upper and lower dots. Finally, drawing a full-sized black circle (to define the right edge) will complete the image. Here is sample code:
import greenfoot.*;

public class MyWorld extends World
{
    public MyWorld()
    {
        super(600, 600, 1);
        int size = 600;
        GreenfootImage bg = getBackground(); // the white image
        bg.setColor(Color.BLACK);
        bg.fillOval(0, 0, size, size);
        bg.setColor(Color.WHITE);
        bg.fillRect(size/2, 0, size, size);
        bg.setColor(Color.BLACK);
        bg.fillOval(size/4, 0, size/2, size/2);
        bg.setColor(Color.WHITE);
        bg.fillOval(size/4, size/2, size/2, size/2);
        bg.fillOval(7*size/16, 3*size/16, size/8, size/8);
        bg.setColor(Color.BLACK);
        bg.fillOval(7*size/16, 11*size/16, size/8, size/8);
        bg.drawOval(0, 0, size-1, size-1);
    }
}
ronald ronald

2019/8/14

#
thank you
ronald ronald

3 days ago

#
how to center yin yang if i change size keeping super (600,600,1)
danpost danpost

3 days ago

#
ronald wrote...
how to center yin yang if i change size keeping super (600,600,1)
Use a method to create and return the yin yang image of the given size:
public static GreenfootImage getYinYang(int size)
{
    GreenfootImage img = new GreenfootImage(size, size);
    img.setColor(Color.BLACK);
    img.fillOval(0, 0, size, size);
    img.setColor(Color.WHITE);
    img.fillRect(0, 0, size/2, size);
    img.fillOval(size/4, 0, size/2, size/2);
    img.setColor(Color.BLACK);
    img.fillOval(size/4, size/2, size/2, size/2);
    img.fillOval(7*size/16, 3*size/16, size/8, size/8);
    img.setColor(Color.WHITE);
    img.fillOval(7*size/16, 11*size/16, size/8, size/8);
    img.setColor(Color.BLACK);
    img.drawOval(0, 0, size-1, size-1);
    return img;
}
Then you can draw it onto your background:
public MyWorld()
{
    super(600, 600, 1)
    int size = 300;
    getBackground().drawImage(getYinYang(size), (getWidth()-size)/2, (getHeight()-size)/2);
]
Of course, lines 4 and 5 could be replaced with:
getBackground().drawImage(getYinYang(300), 150, 150);
You need to login to post a reply.