Re: Graphics help please
Knute Johnson <nospam@rabbitbrush.frazmtn.com> writes:
The JPanel is buffered so that it doesn't actually clear the
screen just the offscreen buffer. You can't draw a circle
pixel by pixel with the technique you used. It will produce
gaps because it only draws a pixel and every degree of angle
and not the points in between.
Alan Kay was teaching five-year old children how to
program a circle: They were asked to walk in a circle
and to report what they did. The children would answer
"walk a little and turn a little." After that cognition
they could write a program to draw a circle.
Ten-year old children already knew what a circle is:
"The set of all point, having the same distance to a
center." So they startet to program individual points
starting at a center, which was more complicated; and
the result was non a connected circle but only single
dots.
Fifteen-year old children already knew the formula ?r = x + y?.
They tried to draw a circle using that formula, but
failed. (This formula is not a good starting point for such a
program.) Just because of their additional knowledge, it was
actually more difficult or impossible for them to write such a
program. At least that is what Alan Kay said in a video.
This made me want to write a little Java program.
It draws a part of something like a circle without AWT or Swing.
The start of the program implements a little framework,
then the contents of the method ?main? solely consists of
?Turtle commands?.
public class Turtle
{
static double x = 0;
static double y = 0;
static double stepsize = 1;
static double direction = 0;
static char[][] screen = new char[24][78];
static void turnALittle(){ direction += 10; }
static void walkALittle()
{ final double angle = direction / 360 * 2 * java.lang.Math.PI;
y = y + stepsize * java.lang.Math.sin( angle );
x = x + stepsize * java.lang.Math.cos( angle );
set( y, x ); }
static
{ for( int i = 0; i < 24; ++i )for( int j = 0; j < 78; ++j )
screen[ i ][ j ]= ' ';
java.lang.Runtime.getRuntime().addShutdownHook
( new Thread
( new java.lang.Runnable()
{ public void run()
{ for( int i = 0; i < 24; ++i )
{ for( int j = 0; j < 78; ++j )
java.lang.System.out.print( screen[ i ][ j ] );
java.lang.System.out.println(); }}})); }
static void set( final double y, final double x )
{ try{ screen[( int )( y + 11 )][( int )( x + 38 )]= '*'; }
catch( final Exception e ){} }
public static void main( final java.lang.String[] args )
{ walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle();
walkALittle(); turnALittle(); }}
**
**
*
*
*
*
*
*
*