Persian rug regression

I am new to Java and I am learning recursion. I found some interesting things like this link that implements recursion to make a Persian rug. I want to implement this code in java

float a = 1.0; int left = 0; int right = 340; int top = 0; int bot = 340; int r,g,b; color firstColor; color backC; //color backC = color(35,95,28); void setup() { //size(700, 700); size(341, 341); r = floor(random(1,255)); g = floor(random(1,255)); b = floor(random(1,255)); firstColor = color(g); backC = color(r,g,b); background(backC); stroke(firstColor); line(left,top,right,top); line(left,bot,right,bot); line(left,top,left,bot); line(right,top,right,bot); a = random(1,6); chooseColor(left, right, top, bot, a); } void draw() {} void chooseColor(int left, int right, int top, int bot, float a) { int midcol, midrow; color col; if (left < (right-1)) { col = floor((get(left,top) + get(right,top) + get(left,bot) + get(right,bot))/a); midcol = (left + right) / 2; midrow = (top + bot) / 2; stroke(col); line(left+1, midrow, right-1, midrow); line(midcol, top+1, midcol, bot-1); chooseColor(left, midcol, top, midrow, a); chooseColor(midcol, right, top, midrow, a); chooseColor(left, midcol, midrow, bot, a); chooseColor(midcol, right, midrow, bot, a); } } void keyPressed() { setup(); } 

I use JFrame for my window and JPanel for drawing, but the code is really different from java, can someone give me advice on where to start? I know that the code uses colors, but what I'm trying to do is make the carpet only in black, so I can later implement some color.

Thanks for the tips.

+5
source share
1 answer

Since you are using Swing, consider using the Oracle Java2D tutorial to learn how to draw lines and fill colors.

0
source

Source: https://habr.com/ru/post/1204141/


All Articles