forked from Galexyin/canvas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas.java
92 lines (90 loc) · 2.66 KB
/
canvas.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.LinkedList;
class canvas{
private int h = 240,w = 320;
private char border_char='*';
private char canv[][];
LinkedList<objectX> objects = new LinkedList<objectX>();
canvas(int x,int y,char border_char){
this.h=y+2; this.w=x+2;
this.border_char=border_char;
this.canv=new char[this.h][this.w];
for(int i = 0;i < this.h;i++){
for(int j = 0;j < this.w;j++){
if((i == 0||i == this.h-1||j == 0||j == this.w-1))
this.canv[i][j] = this.border_char;
else
this.canv[i][j] = ' ';
}
}
}
void display(){
for (int i=h-1;i>=0;i--) {
System.out.println(String.valueOf(canv[i]));
}
}
void clear(){
for(int i = 0;i < this.h;i++){
for(int j = 0;j < this.w;j++){
if((i == 0||i == this.h-1||j == 0||j == this.w-1))
this.canv[i][j] = this.border_char;
else
this.canv[i][j] = ' ';
}
}
}
boolean addPoint(int x,int y,char p){
try{
if(x!=w-2&&y!=h-2)
canv[y+1][x+1]=p;
return true;
}
catch (Exception e){
return false;
}
}
boolean delPoint(int x,int y){
try{
if(x!=w-2&&y!=h-2&&x!=-1&&y!=-1){
canv[y+1][x+1]=' ';
}
return true;
}
catch(Exception e){
return false;
}
}
boolean write(String s,int x,int y){
try{
if(s.length()<((w-1)-x+1)&&y!=-1&&y!=h-2){
for(int i=x+1;i<x+s.length();i++)
canv[y+1][i]=s.charAt(i-x-1);
}
return true;
}
catch (Exception e){
return false;
}
}
boolean insideCanvas(int x,int y){
if(-1<x&&x<w-1&&-1<y&&y<h-1)
return true;
else
return false;
}
boolean addObject(objectX o,String objectName){
o.objectName=objectName;
objects.add(o);
return true;
}
void drawObject(String objname,int frame,int x,int y,char p){
objectX o=null;
for(objectX i : objects){
if(i.objectName==objname)
o=i;
}
o.selectFrame(frame);
for(int i=1;i<o.points.size();i++){
addPoint(o.points.get(i).x+x-o.points.get(0).x, o.points.get(i).y+y-o.points.get(0).y, p);
}
}
}