Could I enquire, how do I check for the time complexity for the program below? (:
Thank you!
import javax.swing.*;
import java.awt.*;
import java.math.*;
/**
*
* @author Jia
*/
public class tilePanel extends JPanel
{
int numSquares;
int increment;
private frame app;
Integer buff;
String x=" ";
public tilePanel(frame app)
{
this.app=app;
}
public void paintComponent(Graphics g)
{
//super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
if(app.p1.n1>0&&app.p1.n1<4)
{
g.setColor(Color.BLACK);
numSquares = (int)Math.pow(2, app.p1.n1);
increment=(this.getWidth()-40)/numSquares;
if(app.p1.x1>numSquares||app.p1.x1<1||
app.p1.y1>numSquares||app.p1.y1<1)
{
app.activity.setText("Error!! Invalid value keyed!");
}
else
{
for(int i=0;i<numSquares;i++)
{
buff=i+1;
g.drawString(buff.toString(), (increment*i)+22, 14);
g.drawRect((increment*i)+20, 15, increment, increment*numSquares);
g.drawString(buff.toString(), 5, (increment*i)+29);
g.drawRect(20, (increment*i)+15, increment*numSquares, increment);
}
g.setColor(Color.BLACK);
g.fillRect((increment*(app.p1.x1-1))+21, (increment*(app.p1.y1-1))+16,increment-1, increment-1);
g.setColor(Color.red);
Tile(app.p1.n1,app.p1.x1,app.p1.y1,increment,g,numSquares,x);
}
}
else
{
if(app.p1.n1>=4)
app.activity.setText("Error!! The program only supports up to n=3");
}
}
public void Tile(int n,int x,int y,int increment, Graphics g,int numSquares,String orientation)
{
int buf;
int bufx=0;
int bufy=0;
int x2=0;
int x3=0;
int x4=0;
int y2=0;
int y3=0;
int y4=0;
buf=(int)((Math.pow(2, n))/2);
if(n==1)
{
if(x%2==0&&y%2==0)
{
g.fillRect((increment*(x-2))+21, (increment*(y-2))+16, increment-1, (increment*2)-1);
g.fillRect((increment*(x-2))+21, increment*(y-2)+16, (increment*2)-1, increment-1);
}
if(x%2==0&&y%2!=0)
{
g.fillRect((increment*(x-2))+21, (increment*(y-1))+16, increment-1, (increment*2)-1);
g.fillRect((increment*(x-2))+21, increment*(y)+16, (increment*2)-1, increment-1);
}
if(x%2!=0&&y%2!=0)
{
g.fillRect((increment*(x))+21, (increment*(y-1))+16, increment-1, (increment*2)-1);
g.fillRect((increment*(x-1))+21, increment*(y)+16, (increment*2)-1, increment-1);
}
if(x%2!=0&&y%2==0)
{
g.fillRect((increment*(x))+21, (increment*(y-2))+16, increment-1, (increment*2)-1);
g.fillRect((increment*(x-1))+21, increment*(y-2)+16, (increment*2)-1, increment-1);
}
return;
}
if(orientation.compareTo(" ")==0)
{
bufx=buf;
bufy=buf;
}
else if(orientation.compareTo("downleft") == 0)
{
bufx=buf;
bufy=numSquares-buf;
}
else if(orientation.compareTo("downright")==0)
{
bufx=numSquares-buf;
bufy=numSquares-buf;
}
else if(orientation.compareTo("upright")==0)
{
bufx=numSquares-buf;
bufy=buf;
}
else if(orientation.compareTo("upleft")==0)
{
bufx=buf;
bufy=buf;
}
if(x<=bufx&&y<=bufy)
{
orientation="upleft";
g.fillRect((increment*(bufx))+21, (increment*(bufy-1))+16, increment-1, (increment*2)-1);
g.fillRect((increment*(bufx-1))+21, increment*(bufy)+16, (increment*2)-1, increment-1);
x2=bufx;y2=bufy+1;
x3=bufx+1;y3=bufy+1;
x4=bufx+1;y4=bufy;
}
else if(x <= bufx && y > bufy)
{
orientation="downleft";
g.fillRect((increment*(bufx))+21, (increment*(bufy-1))+16, increment-1, (increment*2)-1);
g.fillRect((increment*(bufx-1))+21, increment*(bufy-1)+16, (increment*2)-1, increment-1);
x2=bufx+1;y2=bufy+1;
x3=bufx+1;y3=bufy;
x4=bufx;y4=bufy;
}
else if(x > bufx && y > bufy)
{
orientation="downright";
g.fillRect((increment*(bufx-1))+21, (increment*(bufy-1))+16, increment-1, (increment*2)-1);
g.fillRect((increment*(bufx-1))+21, increment*(bufy-1)+16, (increment*2)-1, increment-1);
x2=bufx+1;y2=bufy;
x3=bufx;y3=bufy;
x4=bufx;y4=bufy+1;
}
else if(x > bufx && y <= bufy)
{
orientation="upright";
g.fillRect((increment*(bufx-1))+21, (increment*(bufy-1))+16, increment-1, (increment*2)-1);
g.fillRect((increment*(bufx-1))+21, increment*(bufy)+16, (increment*2)-1, increment-1);
x2=bufx;y2=bufy;
x3=bufx;y3=bufy+1;
x4=bufx+1;y4=bufy+1;
}
Tile(n-1,x,y,increment,g,numSquares,orientation);
orientation=checkOrientation(orientation);
Tile(n-1,x2,y2,increment,g,numSquares,orientation);
orientation=checkOrientation(orientation);
Tile(n-1,x3,y3,increment,g,numSquares,orientation);
orientation=checkOrientation(orientation);
Tile(n-1,x4,y4,increment,g,numSquares,orientation);
}
public String checkOrientation(String orientation)
{
if(orientation.compareTo("upright")==0)
orientation="upleft";
else if(orientation.compareTo("upleft")==0)
orientation="downleft";
else if(orientation.compareTo("downleft")==0)
orientation="downright";
else if(orientation.compareTo("downright")==0)
orientation="upright";
return orientation;
}
}