Difference between revisions of "Ruby:Programming Tutorial"

From Progzoo
Jump to: navigation, search
(New page: An introduction to computer programming using Java. Comprising * Tutorials which present problems and their solutions. * How to... guides to completing common task...)
 
Line 1: Line 1:
An introduction to computer programming using Java. Comprising
+
An introduction to computer programming using Ruby. Comprising
 
*[[Tutorials| Tutorials]] which present problems and their solutions.
 
*[[Tutorials| Tutorials]] which present problems and their solutions.
 
*[[Guides| How to...]] guides to completing common tasks.
 
*[[Guides| How to...]] guides to completing common tasks.
 
*[[Assessments| Assessments]] graded problems with solutions.
 
*[[Assessments| Assessments]] graded problems with solutions.
==Drawing the French tricolour with Java==  
+
==An Example: drawing the French tricolour ==
You can create images using Java. The first tutorial [[Flags]] looks at drawing flags of the world, from simple flags such as Libya to more complicated flags such as the Stars and Stripes of the USA. In Java you can use [[methods]] such as  <code>[[setColor]]</code> and <code>[[fillRect]]</code> to create simple images.        
+
You can create images using the GD2 library. The first tutorial [[Flags]] looks at drawing flags of the world, from simple flags such as Libya to more complicated flags such as the Stars and Stripes of the USA.
<question height="100" width="150" imgOut="flag.png" className="Raster" title="France" rows="9" cols='28'>  
+
 
[[Image:Flag_guide_france.png|border|right]]
+
With GD2 you can set the canvas color and draw a rectangle to create simple images.      
Notice that the blue rectangle is at (0,0), it is 50 wide and 100 deep so the commands to draw the blue rectangle are:   
+
<question height="100" width="150" imgOut="flag.png" className="Raster" title="France" rows="9" cols="40" lang='rb'>  
  g.setColor(Color.blue);
+
[[Image:Flag_guide_france.png|border|right]]  
  g.fillRect(0,0,50,100);
+
Notice that the blue rectangle has corners at (0,0) and (50,100) and has color [1,0,0]
<shell lang="java" className="Flag" import="shells.xml#raster"/>
+
  g.color = image.palette.resolve Color[1.0, 0, 0]
<prog lang="java">static void drawFlag(Graphics2D g){
+
  g.rectangle(0,0,50,100,filled=true)
   g.setColor(Color.blue);
+
<shell lang="rb" className="Flag" import="shells.xml#rasterrb"/>
   g.fillRect(0,0,50,100);
+
<prog>
   g.setColor(Color.white);
+
image.draw do |g|
   g.fillRect(50,0,50,100);
+
   g.color = image.palette.resolve Color[0,0,1.0]
   g.setColor(Color.red);
+
   g.rectangle(0,0,50,100,filled=true)
   g.fillRect(100,0,50,100);
+
   g.color = image.palette.resolve Color[1.0,1.0,1.0]
}
+
   g.rectangle(50,0,100,100,filled=true)
 +
   g.color = image.palette.resolve Color[1.0,0,0]
 +
   g.rectangle(100,0,150,100,filled=true)
 +
end
 
</prog>
 
</prog>
<answer lang="java">static void drawFlag(Graphics2D g){
+
<answer>
   g.setColor(Color.blue);
+
image.draw do |g|
   g.fillRect(0,0,50,100);
+
   g.color = image.palette.resolve Color[0,0,1.0]
   g.setColor(Color.white);
+
   g.rectangle(0,0,50,100,filled=true)
   g.fillRect(50,0,50,100);
+
   g.color = image.palette.resolve Color[1.0,1.0,1.0]
   g.setColor(Color.red);
+
   g.rectangle(50,0,100,100,filled=true)
   g.fillRect(100,0,50,100);
+
   g.color = image.palette.resolve Color[1.0,0,0]
}
+
   g.rectangle(100,0,150,100,filled=true)
 +
end
 
</answer>
 
</answer>
 
</question>
 
</question>

Revision as of 22:55, 30 December 2007

An introduction to computer programming using Ruby. Comprising

An Example: drawing the French tricolour

You can create images using the GD2 library. The first tutorial Flags looks at drawing flags of the world, from simple flags such as Libya to more complicated flags such as the Stars and Stripes of the USA.

With GD2 you can set the canvas color and draw a rectangle to create simple images.

Flag guide france.png

Notice that the blue rectangle has corners at (0,0) and (50,100) and has color [1,0,0]

g.color = image.palette.resolve Color[1.0, 0, 0]
g.rectangle(0,0,50,100,filled=true)



[Font] [Default] [Show] [Resize] [History] [Profile]

Consult the User's Guide for information on changing material in progzoo.

You can mess about in the Playpen.