CodedRaw是一個對初學者友好的繪圖庫,可用於創建圖片,動畫甚至交互式應用程序。它是為剛剛開始學習編程的人而設計的,使他們能夠創建圖形應用程序。
閱讀《編碼介紹》的介紹,以獲取編碼的初學者指南。它還概述了CodedRaw中可用的功能。
可以在此處找到編碼圖的Javadoc。
對於C#版本的CodedRaw,請訪問CodedRawProject存儲庫。
轉到發布並下載最新的Codedraw.jar。
打開Intellij,您想在其中添加CodedRaw。單擊文件>項目結構...。在項目設置下,選擇庫。在左上方,單擊Small Plus圖標,然後選擇Java選項。轉到下載的codedraw.jar,然後選擇它,然後按OK 。現在,您可以使用import codedraw.*;在Java文件的頂部。
要安裝帶有Eclipse , Maven或Gradle的CodedRaw,請參閱install.md。
這是一個示例,說明如何使用CodedRaw創建靜態圖像。
import codedraw .*;
public class Main {
public static void main ( String [] args ) {
// Creates a new CodeDraw window with a size of 400x400 pixel.
CodeDraw cd = new CodeDraw ( 400 , 400 );
// Sets the drawing color to red.
cd . setColor ( Palette . RED );
// Draws the outline of a rectangle.
cd . drawRectangle ( 100 , 100 , 200 , 100 );
// Draws a filled square.
cd . fillSquare ( 180 , 150 , 80 );
// Changes the color to light blue.
cd . setColor ( Palette . LIGHT_BLUE );
cd . fillCircle ( 300 , 200 , 50 );
// Finally, the method "show" must be called
// to display the drawn shapes in the CodeDraw window.
cd . show ();
}
}.show() ❗ 
動畫是通過繪製多個幀然後在這些框架之間暫停的。在CodedRaw中,這是通過創建一個循環來實現的,其中每次迭代繪製一個幀,然後使用方法.show(1000)在這種情況下等待一定的時間,1秒或1000毫秒。
import codedraw .*;
public class Main {
public static void main ( String [] args ) {
CodeDraw cd = new CodeDraw ( 400 , 400 );
for ( double sec = - Math . PI / 2 ; ! cd . isClosed (); sec += Math . PI / 30 ) {
// Clears the entire canvas.
cd . clear ();
// Draws the second hand of the clock.
cd . drawLine ( 200 , 200 , Math . cos ( sec ) * 100 + 200 , Math . sin ( sec ) * 100 + 200 );
// Draws the twelve dots.
for ( double j = 0 ; j < Math . PI * 2 ; j += Math . PI / 6 ) {
cd . fillCircle ( Math . cos ( j ) * 100 + 200 , Math . sin ( j ) * 100 + 200 , 4 );
}
// Displays the drawn objects and waits 1 second.
cd . show ( 1000 );
}
}
}可以通過讀取EventsCanner的事件並根據事件類型進行switch來創建交互式程序。在較舊的Java版本中,EventsCanner也可以與java.util.Scanner相同的方式使用has...和next...方法。在編碼事件簡介中的“處理事件”部分中可以找到更詳細的解釋。
您還可以使用CodedRaw事件代碼生成器自動為您生成事件代碼。
import codedraw .*;
public class Main {
public static void main ( String [] args ) {
CodeDraw cd = new CodeDraw ();
cd . drawText ( 200 , 200 , "Move your mouse over here." );
cd . show ();
cd . setColor ( Palette . RED );
// Creates an endless loop (until you close the window).
while (! cd . isClosed ()) {
// Creates a loop that consumes all the currently available events.
for ( var e : cd . getEventScanner ()) {
switch ( e ) {
// If the event is a mouse move event, a red square will be drawn at its location.
case MouseMoveEvent a ->
cd . fillSquare ( a . getX () - 5 , a . getY () - 5 , 10 );
default -> { }
}
}
// Display the red squares that have been drawn up to this point.
cd . show ( 16 );
}
}
}所有這些示例也可以使用Animation接口創建。 Animation接口的實例可以傳遞給codedraw,隨後調用您實現的方法。以下示例使您可以用wasd-keys控制一個圓圈。每次按下鑰匙時,都會觸發onKeyDown方法,並修改圓的位置。 draw方法稱為每秒60次,並在xy坐標處繪製圓。
import codedraw .*;
public class MyAnimation implements Animation {
public static void main ( String [] args ) {
CodeDraw . run ( new MyAnimation ());
}
private int x = 50 ;
private int y = 50 ;
@ Override
public void onKeyDown ( KeyDownEvent event ) {
if ( event . getKey () == Key . W ) {
y -= 20 ;
}
else if ( event . getKey () == Key . A ) {
x -= 20 ;
}
else if ( event . getKey () == Key . S ) {
y += 20 ;
}
else if ( event . getKey () == Key . D ) {
x += 20 ;
}
}
@ Override
public void draw ( Image canvas ) {
canvas . clear ();
canvas . fillCircle ( x , y , 10 );
}
}請隨時提出問題,建議功能或在問題部分中進行錯誤報告。 ?