Processing - How to resize shapes - java

Using the mouse, I would like to change the size of the shapes that I have in my code (which is circle and rectangle) by clicking on the corners and dragging it to change the shape's width and height.
Here's my main code:
circleShape [] objs;
int numObjs = 1;
int pickedItem = -1;
rectShape [] objss;
int numObjss = 1;
int itemPicked = -1;
void setup() {
size(600, 600);
objs = new circleShape[numObjs];
for (int i = 0; i < numObjs; i++) {
int radius = (int)(random(20, 70));
color c = (int)(random(0, 255));
objs[i] = new circleShape(radius, c);
}
objss = new rectShape[numObjss];
for (int i = 0; i < numObjss; i++) {
int Radius = (int)(random(20, 70));
color C = (int)(random(0, 255));
objss[i] = new rectShape(Radius, C);
}
}
void draw() {
background(255);
for (int i = 0; i < numObjs; i++) {
objs[i].drawShape();
}
for (int i =0; i < numObjss; i++) {
objss[i].shapeDraw();
}
}
// return the index number of the picked item from the array
// return -1 otherwise
int getPickedObj(int x, int y) {
for (int i = 0; i < numObjs; i++) {
if (objs[i].isOnShape(x, y)) {
return i;
}
}
return -1;
}
int pickedObj(int X, int Y) {
for (int i = 0; i < numObjss; i++) {
if (objss[i].onShape(X, Y)) {
return i;
}
}
return -1;
}
void mousePressed() {
pickedItem = getPickedObj(mouseX, mouseY);
itemPicked = pickedObj(mouseX, mouseY);
}
void mouseDragged() {
if (pickedItem >= 0) {
objs[pickedItem].move(mouseX, mouseY);
objs[pickedItem].drawShape();
}
if (itemPicked >= 0) {
objss[itemPicked].move(mouseX, mouseY);
objss[itemPicked].shapeDraw();
}
}
And here's my shape code:
// a shape is simply as a circle
class circleShape {
int posX, posY, radius;
color c;
circleShape(int radius, color c) {
this.radius = radius;
this.c = c;
posX = (int)random(0, 200);
posY = (int)random(0, 200);
}
// return true if the position x, y is on the shape
boolean isOnShape(int x, int y) {
float d = (posX - x)*(posX - x) + (posY - y)*(posY - y);
d = sqrt(d);
if (d < radius) return true;
else return false;
}
void move (int newPosX, int newPosY) {
this.posX = newPosX;
this.posY = newPosY;
}
void drawShape() {
noStroke();
fill(c);
ellipse(posX, posY, 2*radius, 2*radius);
}
}
//Rectangle Shape
class rectShape {
int xPos, yPos, Radius;
color C;
rectShape(int Radius, color C) {
this.Radius = Radius;
this.C = C;
xPos = (int)random(0, 200);
yPos = (int)random(0, 200);
}
boolean onShape(int X, int Y) {
float D = (xPos - X)*(xPos - X) + (yPos - Y)*(yPos - Y);
D = sqrt(D);
if (D < Radius) return true;
else return false;
}
void move (int posXnew, int posYnew) {
this.xPos = posXnew;
this.yPos = posYnew;
}
void shapeDraw() {
noStroke();
fill(C);
rect(xPos, yPos, 2*Radius+60, 2*Radius);
}
}
My code allows the mouse to move the shapes from one place to another. It just needs to be able to change the size of them.

I'll give you an example for a rectangular shape.
A rectangle is defined by a position, the width and the height. Change the attributes and the constructor of the rectangle:
class rectShape {
int xPos, yPos, Width, Height;
color C;
rectShape(int X, int Y, int Width, int Height, color C) {
this.xPos = X;
this.yPos = Y;
this.Width = Width;
this.Height = Height;
this.C = C;
}
// [...]
void shapeDraw() {
noStroke();
fill(C);
rect(xPos, yPos, this.Width, this.Height);
}
}
Create a random rectangle:
void setup() {
size(600, 600);
// [...]
objss = new rectShape[numObjss];
for (int i = 0; i < numObjss; i++) {
int xPos = (int)random(0, 200);
int yPos = (int)random(0, 200);
int Width = (int)(random(20, 70));
int Height = (int)(random(20, 70));
color C = (int)(random(0, 255));
objss[i] = new rectShape(xPos, yPos, Width, Height, C);
}
}
Create a method in the class rectShape, which determines if the mouse is on the rectangle and if it is on the left, right, top, bottom or center:
String onShape(int X, int Y) {
String where = "";
Boolean isOn = X > xPos && X < xPos + Width && Y > yPos && Y < yPos + Height;
if (isOn) {
int left = abs(X - xPos);
int right = abs(X - (xPos + Width));
int top = abs(Y - yPos);
int bottom = abs(Y - (yPos + Height));
if (min (left, right) < min (top, bottom)) {
if (left < 10)
where = "left";
else if (right < 10)
where = "right";
else
where = "center";
}
else {
if (top < 10)
where = "top";
else if (bottom < 10)
where = "bottom";
else
where = "center";
}
}
return where;
}
Create a method which can change the rectangle dependent on an action command:
void update(String what, int posXnew, int posYnew) {
if (what == "left") {
if (posXnew < this.xPos + this.Width) {
this.Width = this.xPos + this.Width - posXnew;
this.xPos = posXnew;
}
}
else if (what == "right") {
if (posXnew > this.xPos) {
this.Width = posXnew - this.xPos;
}
}
else if (what == "top") {
if (posYnew < this.yPos + this.Height) {
this.Height = this.yPos + this.Height - posYnew;
this.yPos = posYnew;
}
}
else if (what == "bottom") {
if (posYnew > this.yPos) {
this.Height = posYnew - this.yPos;
}
}
else {
this.xPos = posXnew - this.Width / 2;
this.yPos = posYnew - this.Height / 2;
}
}
Determine the action when the rectangle is clicked on:
String itemAction = "";
int pickedObj(int X, int Y) {
for (int i = 0; i < numObjss; i++) {
itemAction = objss[i].onShape(X, Y);
if (itemAction != "") {
return i;
}
}
return -1;
}
void mousePressed() {
// [...]
itemPicked = pickedObj(mouseX, mouseY);
}
Pass the action to the update method when the mouse is dragged:
void mouseDragged() {
if (itemPicked >= 0) {
objss[itemPicked].update(itemAction, mouseX, mouseY);
}
}
Complete class rectShape:
//Rectangle Shape
class rectShape {
int xPos, yPos, Width, Height;
color C;
rectShape(int X, int Y, int Width, int Height, color C) {
this.xPos = X;
this.yPos = Y;
this.Width = Width;
this.Height = Height;
this.C = C;
}
String onShape(int X, int Y) {
String where = "";
Boolean isOn = X > xPos && X < xPos + Width && Y > yPos && Y < yPos + Height;
if (isOn) {
int left = abs(X - xPos);
int right = abs(X - (xPos + Width));
int top = abs(Y - yPos);
int bottom = abs(Y - (yPos + Height));
if (min (left, right) < min (top, bottom)) {
if (left < 10)
where = "left";
else if (right < 10)
where = "right";
else
where = "center";
}
else {
if (top < 10)
where = "top";
else if (bottom < 10)
where = "bottom";
else
where = "center";
}
}
return where;
}
void update(String what, int posXnew, int posYnew) {
if (what == "left") {
if (posXnew < this.xPos + this.Width) {
this.Width = this.xPos + this.Width - posXnew;
this.xPos = posXnew;
}
}
else if (what == "right") {
if (posXnew > this.xPos) {
this.Width = posXnew - this.xPos;
}
}
else if (what == "top") {
if (posYnew < this.yPos + this.Height) {
this.Height = this.yPos + this.Height - posYnew;
this.yPos = posYnew;
}
}
else if (what == "bottom") {
if (posYnew > this.yPos) {
this.Height = posYnew - this.yPos;
}
}
else {
this.xPos = posXnew - this.Width / 2;
this.yPos = posYnew - this.Height / 2;
}
}
void shapeDraw() {
noStroke();
fill(C);
rect(xPos, yPos, this.Width, this.Height);
}
}

Related

Processing: Where would I initialize a counter if it is changed in draw?

Table t1;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
int stickColR=0;
int stickColG=0;
int stickColB=0;
int counter=0;
//--------------
void setup(){
size(1000,600);
smooth();
t1 = new Table(1000,600);
t1.startGame();
//sound player setup
minim= new Minim(this);
player=minim.loadFile("ballsound.mp3");
}
//--------------
void draw(){
strokeWeight(10);
stroke(255,0,0);
fill(26, 218, 35);
rect(0, 0, 1000, 600);
fill(0);
noStroke();
ellipse(0, 0, 100, 100);
ellipse(1000, 0, 100, 100);
ellipse(0, 600, 100, 100);
ellipse(1000, 600, 100, 100);
t1.updatePos(); //calling the function to update the position of the balls
t1.visualize(); // Function responsible for drawing the balls
}
//--------------
void mousePressed(){
t1.mousePressed(mouseX-t1.x,mouseY-t1.y);
}
//--------------
void mouseReleased(){
t1.mouseReleased(mouseX-t1.x,mouseY-t1.y);
}
//=============================
class Table{
boolean GameIsOver=false;
float drag = 0.99;
float bounce = 0.4;
float wallBounce = 0.3;
float pushFactor = 0.4;
float maxPush = 20;
color[] ballColors = new color[]{color (255), color (216,7,17),color(17,242,225), color ( #45B4CE) , color ( #6A6347) , color (#E80909) ,color (#CEA9A9)};
Ball[] balls;
Ball selectedBall;
int x,y,width,height;
//--------------
Table(int w, int h){
width = w;
height = h;
}
//--------------
void startGame(){
buildBalls(5);
}
//--------------
void buildBalls(int count){
balls = new Ball[2*count+1];
int x_coor_red=600;
int y_coor_red=300;
int x_coor_green=626;
int y_coor_green=313;
for(int i=0;i<count;i++)
{
balls[i] = new Ball(x_coor_red, y_coor_red,i+1, this);
x_coor_red+=26;
y_coor_red-=13;
if(i>=3)
{
x_coor_red-=26;
y_coor_red+=26;
}
}
for(int i=0;i<count;i++)
{
balls[count+i] = new Ball( x_coor_green, y_coor_green,i+2, this);
x_coor_green+=26;
y_coor_green+=13;
if(i==1)
{
x_coor_green-=26;
y_coor_green-=20;
}
if(i==2)
{
y_coor_green-=20;
}
if(i==3)
{
x_coor_green-=45;
}
}
balls[2*count] = new Ball( 0.5*(width), 0.5*(height),0, this);
}
//--------------
void updatePos(){
//simulation
for(int i=0;i<balls.length;i++)
balls[i].update();
//collision detection
for(int i=0;i<balls.length;i++)
for(int j=i+1;j<balls.length;j++)
balls[i].collisionDetect(balls[j]);
}
//--------------
void visualize(){
translate(x,y);
noStroke();
//draw stick
if(mousePressed && selectedBall!= null && (mouseX<=26+selectedBall.x || mouseX>26-selectedBall.x) && selectedBall.ballColor==color(255)){
stickColR+=2;
stickColB+=2;
strokeWeight(4);
stroke(stickColR, stickColG, stickColB);
line ( mouseX , mouseY , mouseX + cos (atan2 ( mouseY -selectedBall.y , mouseX - selectedBall.x) )*300 , mouseY + sin( atan2 ( mouseY - selectedBall.y , mouseX -selectedBall.x ))*300);
}
//drawing
for(int i=0;i<balls.length;i++){
balls[i].visualize();
//Balls"disappearing" in corners
if(balls[i].x<50 && (balls[i].y<50 || balls[i].y>550) || balls[i].x>950 &&(balls[i].y<50 || balls[i].y>550)){
player.rewind();
player.play();
if(balls[i].ballColor==color(255))
{
textSize(25);
text("Cue Ball Sunk. GAME OVER",350,560);
}
fill(0);
ellipse(0,0,100,100);
ellipse(1000, 0, 100, 100);
ellipse(0, 600, 100, 100);
ellipse(1000, 600, 100, 100);
balls[i].x=1200;
balls[i].y=0;
counter++;
if (balls[i].ballColor != 255 && counter>=3)
{
textSize(25);
text("YOU WIN", 350,560);
}
}
}
}
//--------------
float kineticEnergy(){
float energy=0;
for(int i=0;i<balls.length;i++)
energy += mag( balls[i].vx, balls[i].vy );
return energy;
}
//--------------
void mousePressed(int mx, int my){
for(int i=0;i<balls.length;i++)
if( dist(balls[i].x,balls[i].y,mx,my) < balls[i].radius) {
selectedBall = balls[i];
break;
}
}
//--------------
void mouseReleased(int mx, int my){
if(selectedBall != null){
float px = (selectedBall.x-mx) * pushFactor;
float py = (selectedBall.y-my) * pushFactor;
float push = mag(px,py);
stickColR=0;
stickColB=0;
if( push > maxPush ){
px = maxPush*px/push;
py = maxPush*py/push;
}
selectedBall.push(px,py);
}
selectedBall = null;
}
}
class Ball{
float x,y,vx,vy,radius,diameter;
int type;
Table table;
color ballColor;
//--------------
Ball(float x, float y, int type, Table t){
this.x = x;
this.y = y;
this.type = type;
diameter = 26;
radius = diameter/2;
table = t;
ballColor = table.ballColors[type];
}
//--------------
void update(){
vx *= table.drag;
vy *= table.drag;
x += vx;
y += vy;
wallBounce();
}
//--------------
void wallBounce(){
if(x<=radius){
vx = -table.wallBounce*vx;
x = radius;
player.rewind();
player.play();
}
if(x>=t1.width-radius){
vx = -table.wallBounce*vx;
x = table.width-radius;
player.rewind();
player.play();
}
if(y<=radius){
vy = -table.wallBounce*vy;
y = radius;
player.rewind();
player.play();
}
if(y>=t1.height-radius){
vy = -table.wallBounce*vy;
y = table.height-radius;
player.rewind();
player.play();
}
}
//--------------
void visualize(){
fill(ballColor);
stroke(0);
strokeWeight(2);
stroke(0);
ellipse(x,y,diameter,diameter);
}
//--------------
void push(float dx, float dy){
vx += dx;
vy += dy;
}
//--------------
void collisionDetect(Ball b){
float distance = dist(x,y,b.x,b.y);
if( distance < diameter && (b.x>50 && (b.y>50 || b.y<550) || b.x<950 &&(b.y>50 || b.y<550))){
float vxSum = 0.5*(vx + b.vx);
float vySum = 0.5*(vy + b.vy);
float forceMagnitude = ((b.x-x)*(vx-vxSum) + (b.y-y)*(vy-vySum));
float xForce = 0.25*(x-b.x)*forceMagnitude/distance;
float yForce = 0.25*(y-b.y)*forceMagnitude/distance;
vx = vx + table.bounce * xForce;
vy = vy + table.bounce * yForce;
b.vx = b.vx - table.bounce * xForce;
b.vy = b.vy - table.bounce * yForce;
b.x = x + (diameter+1)*(b.x-x)/distance;
b.y = y + (diameter+1)*(b.y-y)/distance;
player.rewind();
player.play();
}
}
}
Im making a pool game in processing and I'm almost done but I'm stuck here. I want to add a counter that counts when a ball has been in the corner, then once the counter equals ten, the number of balls, it will display the winning message. I think something is going wrong in that the counter is being reset each time since it's in draw? But I'm not sure where else to initialize it. Btw, right now I just have it >= 3 so that I can quickly see if it's working. Thanks all
due to my low reputation, I have to show you my solution even though I think I haven't fixed all the bugs in your code.
I think your bug stated in the question has little relationship with the counter variable. I ran your code and found that you tried to hide the "disappeared" ball by changing coordinates. It means that "disappeared" balls can still be qualified to the if statement below, and as the result, value of counter will keep increasing.
//Balls"disappearing" in corners
if(balls[i].x<50 && (balls[i].y<50 || balls[i].y>550) || balls[i].x>950 &&(balls[i].y<50 || balls[i].y>550))
So you can add a boolean field to the Ball class
class Ball{
float x,y,vx,vy,radius,diameter;
int type;
Table table;
color ballColor;
// add this variable.
boolean disappeared = false;
// your constructor, wallBounce(), push(), collisionDetect()
void visualize(){
// only draw the ball when it hasn't disappeared.
if(!this.disappeared) {
fill(ballColor);
stroke(0);
strokeWeight(2);
stroke(0);
ellipse(x,y,diameter,diameter);
}
}
}
and then use it to avoid double counting.
// other code in visualize()
if(!balls[i].disappeared) {
counter++;
balls[i].disappeared = true;
}
// other code
Table t1;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
int stickColR=0;
int stickColG=0;
int stickColB=0;
float counter;
boolean isAllIn = false;
//--------------
void setup() {
counter=0;
size(1000, 600);
smooth();
t1 = new Table(1000, 600);
t1.startGame();
//sound player setup
minim= new Minim(this);
player=minim.loadFile("ballsound.mp3");
}
//--------------
void draw() {
strokeWeight(10);
stroke(255, 0, 0);
fill(26, 218, 35);
rect(0, 0, 1000, 600);
fill(0);
noStroke();
ellipse(0, 0, 100, 100);
ellipse(1000, 0, 100, 100);
ellipse(0, 600, 100, 100);
ellipse(1000, 600, 100, 100);
t1.updatePos(); //calling the function to update the position of the balls
t1.visualize(); // Function responsible for drawing the balls
}
//--------------
void mousePressed() {
t1.mousePressed(mouseX-t1.x, mouseY-t1.y);
}
//--------------
void mouseReleased() {
t1.mouseReleased(mouseX-t1.x, mouseY-t1.y);
}
//=============================
class Table {
float drag = 0.99;
float bounce = 0.4;
float wallBounce = 0.3;
float pushFactor = 0.4;
float maxPush = 20;
color[] ballColors = new color[]{color (255), color (216, 7, 17), color(17, 242, 225), color ( #45B4CE), color ( #6A6347), color (#E80909), color (#CEA9A9)};
Ball[] balls;
Ball selectedBall;
int x, y, width, height;
//--------------
Table(int w, int h) {
width = w;
height = h;
}
//--------------
void startGame() {
buildBalls(5);
}
void restart() {
t1 = new Table(1000, 600);
t1.startGame();
}
//--------------
void buildBalls(int count) {
balls = new Ball[2*count+1];
int x_coor_red=600;
int y_coor_red=300;
int x_coor_green=626;
int y_coor_green=313;
for (int i=0; i<count; i++)
{
balls[i] = new Ball(x_coor_red, y_coor_red, i+1, this);
x_coor_red+=26;
y_coor_red-=13;
if (i>=3)
{
x_coor_red-=26;
y_coor_red+=26;
}
}
for (int i=0; i<count; i++)
{
balls[count+i] = new Ball( x_coor_green, y_coor_green, i+2, this);
x_coor_green+=26;
y_coor_green+=13;
if (i==1)
{
x_coor_green-=26;
y_coor_green-=20;
}
if (i==2)
{
y_coor_green-=20;
}
if (i==3)
{
x_coor_green-=45;
}
}
balls[2*count] = new Ball( 0.5*(width), 0.5*(height), 0, this);
}
//--------------
void updatePos() {
//simulation
for (int i=0; i<balls.length; i++)
balls[i].update();
//collision detection
for (int i=0; i<balls.length; i++)
for (int j=i+1; j<balls.length; j++)
balls[i].collisionDetect(balls[j]);
}
//--------------
void visualize() {
translate(x, y);
noStroke();
//draw stick
if (mousePressed && selectedBall!= null && (mouseX<=26+selectedBall.x || mouseX>26-selectedBall.x) && selectedBall.ballColor==color(255)) {
stickColR+=2;
stickColB+=2;
strokeWeight(4);
stroke(stickColR, stickColG, stickColB);
line ( mouseX, mouseY, mouseX + cos (atan2 ( mouseY -selectedBall.y, mouseX - selectedBall.x) )*300, mouseY + sin( atan2 ( mouseY - selectedBall.y, mouseX -selectedBall.x ))*300);
}
//drawing
for (int i=0; i<balls.length; i++) {
balls[i].visualize();
//Balls"disappearing" in corners
if (balls[i].x<50 && (balls[i].y<50 || balls[i].y>550) || balls[i].x>950 &&(balls[i].y<50 || balls[i].y>550)) {
player.rewind();
player.play();
if (balls[i].ballColor==color(255))
{
textSize(25);
text("Cue Ball Sunk. GAME OVER. Press any key to restart.", 170, 560);
if (keyPressed == true) {
t1.restart();
}
}
/*
fill(0);
ellipse(0, 0, 100, 100);
ellipse(1000, 0, 100, 100);
ellipse(0, 600, 100, 100);
ellipse(1000, 600, 100, 100);
balls[i].x=1200;
balls[i].y=0;
*/
if (!balls[i].disappeared) {
counter++;
balls[i].disappeared=true;
}
if ( counter>=3) {
textSize(25);
text("YOU WIN. Press any key to restart.", 300, 560);
if (keyPressed == true) {
t1.restart();
}
}
}
}
}
//--------------
float kineticEnergy() {
float energy=0;
for (int i=0; i<balls.length; i++)
energy += mag( balls[i].vx, balls[i].vy );
return energy;
}
//--------------
void mousePressed(int mx, int my) {
for (int i=0; i<balls.length; i++)
if ( dist(balls[i].x, balls[i].y, mx, my) < balls[i].radius) {
selectedBall = balls[i];
break;
}
}
//--------------
void mouseReleased(int mx, int my) {
if (selectedBall != null) {
float px = (selectedBall.x-mx) * pushFactor;
float py = (selectedBall.y-my) * pushFactor;
float push = mag(px, py);
stickColR=0;
stickColB=0;
if ( push > maxPush ) {
px = maxPush*px/push;
py = maxPush*py/push;
}
selectedBall.push(px, py);
}
selectedBall = null;
}
}
class Ball {
boolean disappeared = false;
float x, y, vx, vy, radius, diameter;
int type;
Table table;
color ballColor;
//--------------
Ball(float x, float y, int type, Table t) {
this.x = x;
this.y = y;
this.type = type;
diameter = 26;
radius = diameter/2;
table = t;
ballColor = table.ballColors[type];
}
//--------------
void update() {
vx *= table.drag;
vy *= table.drag;
x += vx;
y += vy;
wallBounce();
}
//--------------
void wallBounce() {
if (x<=radius) {
vx = -table.wallBounce*vx;
x = radius;
player.rewind();
player.play();
}
if (x>=t1.width-radius) {
vx = -table.wallBounce*vx;
x = table.width-radius;
player.rewind();
player.play();
}
if (y<=radius) {
vy = -table.wallBounce*vy;
y = radius;
player.rewind();
player.play();
}
if (y>=t1.height-radius) {
vy = -table.wallBounce*vy;
y = table.height-radius;
player.rewind();
player.play();
}
}
//--------------
void visualize() {
if (!this.disappeared) {
fill(ballColor);
stroke(0);
strokeWeight(2);
stroke(0);
ellipse(x, y, diameter, diameter);
}
}
//--------------
void push(float dx, float dy) {
vx += dx;
vy += dy;
}
//--------------
void collisionDetect(Ball b) {
float distance = dist(x, y, b.x, b.y);
if ( distance < diameter && (b.x>50 && (b.y>50 || b.y<550) || b.x<950 &&(b.y>50 || b.y<550))) {
float vxSum = 0.5*(vx + b.vx);
float vySum = 0.5*(vy + b.vy);
float forceMagnitude = ((b.x-x)*(vx-vxSum) + (b.y-y)*(vy-vySum));
float xForce = 0.25*(x-b.x)*forceMagnitude/distance;
float yForce = 0.25*(y-b.y)*forceMagnitude/distance;
vx = vx + table.bounce * xForce;
vy = vy + table.bounce * yForce;
b.vx = b.vx - table.bounce * xForce;
b.vy = b.vy - table.bounce * yForce;
b.x = x + (diameter+1)*(b.x-x)/distance;
b.y = y + (diameter+1)*(b.y-y)/distance;
player.rewind();
player.play();
}
}
}

Draw lines between points, each moving in random direction

I'm working on a Java Game using LWJGL. I made a particle system and would like to obtain same result as found on this website: http://play.cubedpixels.de
This is what I got so far:
I tried my best and the problem is it's stopping and they are only moving in one direction. Also the lines are not like on this website. On the website it's smoother and better. They are using OpenGL too.
How to solve these problems and make my implementation better, more like it is one the website?
The code below shows two classes: ParticleGalaxy and Particle. Latter one is inner class of the former, but for clarity I've split them into separate snippets.
ParticleGalaxy:
public class ParticleGalaxy
{
private int count;
private int width;
private int height;
private int mousex;
private int mousey;
public ArrayList<Particle> particles = new ArrayList();
private Random random = new Random();
private TimerUtil timer = new TimerUtil();
int state = 0;
int a = 255;
int r = 255;
int g = 0;
int b = 0;
public ParticleGalaxy(int count, int width, int height)
{
this.count = count;
this.width = width;
this.height = height;
for(int i = 0; i < count; i++)
{
this.particles.add(
new Particle(this.random.nextInt(width),
this.random.nextInt(height)));
}
}
public void drawParticles(int mousex, int mousey)
{
this.mousex = mousex;
this.mousey = mousey;
for(Particle p : this.particles)
{
if(p.reset)
{
p.resetPosSize();
p.reset = false;
}
int x = Math.abs(p.getX() - mousex);
int y = Math.abs(p.getY() - mousey);
if((x < 40 && x > -40) && (y<35 && y>-40))
{
p.setConnect(true);
}
else
{
p.setConnect(false);
}
p.draw();
}
}
}
Particle:
public class Particle
{
private int x;
private int y;
private int k;
private int movey;
private int movex;
private int starty;
private int startx;
private int locationy;
private int locationx;
private float size;
private boolean reset;
private boolean connect;
private Random random = new Random();
private TimerUtil timer = new TimerUtil();
private boolean moving = false;
private boolean start = false;
public Particle(int x, int y)
{
this.x = x;
this.y = y;
this.startx = x;
this.starty = y;
this.size = genRandom(0.57F, 0.71F);
}
public void setConnect(boolean bool)
{
connect = bool;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
private void drawLine(int xto, int yto, int xfrom, int yfrom)
{
GL11.glLineWidth(1.2f);
GL11.glColor3f(1.0f, 1.0f, 1.0f);
GL11.glBegin(GL11.GL_LINE_STRIP);
GL11.glVertex2d(xto, yto);
GL11.glVertex2d(xfrom, yfrom);
GL11.glEnd();
}
public void draw()
{
if(!start)
{
start = true;
movex = height - random.nextInt(height);
movey = width - random.nextInt(width);
move(startx, starty, movex, movey);
}
float speed = 1;
float elapsed = 0.01f;
// On starting movement
float distance = (float)Math.sqrt(Math.pow(movex - startx, 2) +
Math.pow(movey - starty, 2));
float directionX = (movex - startx) / distance;
float directionY = (movey - starty) / distance;
if(moving == true)
{
x += directionX * speed * elapsed;
y += directionY * speed * elapsed;
if(Math.sqrt(Math.pow(x - startx, 2) + Math.pow(y - starty, 2)) >= distance)
{
x = (int)movex;
y = (int)movey;
resetPosSize();
this.reset = true;
moving = false;
}
}
this.k += 1;
int xx = 0;
int yy = 0;
this.locationx = this.x + xx;
this.locationy = this.y + yy;
if(locationx < 0 || locationy < 0)
this.reset = true;
if(locationx > width || locationy > height)
this.reset = true;
GuiUtils.drawCircle(this.x + xx, this.y + yy, this.size, -1);
if(connect)
{
for(Particle p : particles)
{
if(p.connect)
drawLine(locationx, locationy, p.locationx, p.locationy);
}
}
}
public void move(int startX, int startY, int endX, int endY)
{
x = (int)startX;
y = (int)startY;
moving = true;
}
public void resetPosSize()
{
this.x = this.random.nextInt(ParticleGalaxy.this.width);
this.y = this.random.nextInt(ParticleGalaxy.this.height);
startx = x;
starty = y;
movex = height - random.nextInt(height);
movey = width - random.nextInt(width);
move(startx, starty, movex, movey);
}
public float genRandom(float min, float max)
{
return (float)(min + Math.random() * (max - min + 1.0F));
}
}

How to rotate BufferedImage as object itself

I have trouble with performance when I am rotating every image, when drawing it FPS has dropped from 1400 to 6. Can I rotate BufferedImage object just after creating it, so the calculation happens only once?
There is my Grid Control class: (Drawing class, problem method is drawRotated())
package cs.meowxiik.universes.graphics;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.Observer;
import cs.meowxiik.universes.map.Map;
import cs.meowxiik.universes.map.Tile;
public class GridControl {
int width;
int height;
int xcells;
int ycells;
int cellWidth;
int cellHeight;
int maxZoom = 80;
int minZoom = 30;
private Map activeMap;
public GridControl(int width, int height, int xcells, int ycells) {
this.width = width;
this.height = height;
this.xcells = xcells;
this.ycells = ycells;
cellWidth = width / xcells;
cellHeight= height/ ycells;
}
public GridControl(int width, int height, int cellWidth, int cellHeight, int thisDoesntMatterJustForOverloading) {
this.width = width;
this.height = height;
this.cellWidth = cellWidth;
this.cellHeight = cellHeight;
xcells = width / cellWidth;
ycells = height / cellHeight;
}
public void drawGrid(Graphics g){
for(int i = 0; i < xcells; i++){
for(int ii = 0; ii <ycells; ii++){
g.drawRect(i * cellWidth, ii * cellHeight, cellWidth, cellHeight);
}
}
}
public int getCellSize() {
return cellWidth;
}
public void editCellSize(int i) {
if(i == 0)return;
System.out.println("Resizing: " + i);
//Pokud je i menší jak 0 -> člověk se pokouší přibližovat
if(i < 0 && xcells > maxZoom)return;
if(i > 0 && xcells < minZoom)return;
this.cellWidth += i;
this.cellHeight += i;
xcells = width / cellWidth;
ycells = height / cellHeight;
}
public void setActiveMap(Map map){
this.activeMap = map;
}
public void drawMap(Graphics g, Canvas canvas){
if(activeMap != null){
for(int i = 0; i < xcells; i++){
for(int ii = 0; ii <ycells; ii++){
drawRotated(activeMap.getTiles()[i][ii].getTexture(),activeMap.getTiles()[i][ii].getDegrees(), i*cellWidth, ii*cellWidth,canvas, g, null);
//ImageObserver observer = null;
//g.drawImage(activeMap.getTiles()[i][ii].getTexture(), i*cellWidth, ii*cellWidth, observer);
}
}
}
}
public Tile getTileByCoordinate(int x, int y){
int cellx = x / this.cellWidth;
int celly = y / this.cellHeight;
return activeMap.getTiles()[cellx][celly];
}
public boolean isWalkable(int x, int y){
return getTileByCoordinate(x,y).getWalkable();
}
public void drawRotated(BufferedImage img,double rotationRequired,int x,int y,Canvas canvas, Graphics g, Observer observer){
//double rotationRequired = Math.toRadians(degrees);
double locationX = img.getWidth() / 2;
double locationY = img.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
// Drawing the rotated image at the required drawing locations
g.drawImage(op.filter(img, null), x, y, null);
}}
My Tile method:
public class Tile {
boolean walkable;
BufferedImage image;
double radians;
int ID; //0 - floor, 1 - wall
public Tile(boolean walkable, BufferedImage image, int ID) {
this.walkable = walkable;
this.image = image;
this.ID = ID;
}
public BufferedImage getTexture() {
return image;
}
public boolean getWalkable() {
return walkable;
}
public int getDegrees() {
return (int) Math.toDegrees(radians);
}
public int getID() {
return ID;
}
public void setTexture(BufferedImage image, int degrees) {
this.image = image;
this.radians = Math.toRadians(degrees);
}
}
And my Map creator: (the rotating part is broken, I'll fix that later, now I need to solve my 6-FPS problem)
package cs.meowxiik.universes.map;
import cs.meowxiik.universes.graphics.GraphicsManager;
public class MapCreator {
GraphicsManager graphics;
public MapCreator(GraphicsManager graphics) {
this.graphics = graphics;
}
public Map getSpaceStationMap(){
Map map;
int x = 32;
int y = 18;
Tile[][] tiles = new Tile[x][y];
for(int i = 0; i < x; i++){
for(int ii = 0; ii < y; ii++){
tiles[i][ii] = new Tile(true, graphics.floor, 0);
}
}
for(int i = 0; i < x; i++){
for(int ii = 0; ii < y; ii++){
if(i == 0 || ii == 0 || i == x-1 || ii == y-1)tiles[i][ii] = new Tile(false,graphics.stWallZero, 1); //tiles.get(i).set(ii, new Tile(false,graphics.wall));
}
}
for(int i = 0; i < x; i++){
for(int ii = 0; ii < y; ii++){
if(tiles[i][ii].getID() == 0)break;
boolean connectedUp = false;
boolean connectedLeft = false;
boolean connectedRight = false;
boolean connectedBottom = false;
if(i + 1 < tiles.length)
if(tiles[i + 1][ii + 0].getID() == 1)
connectedRight = true;
if(i != 0)
if(tiles[i - 1][ii + 0].getID() == 1)
connectedLeft = true;
if(ii + 1 < tiles[i].length)
if(tiles[i + 0][ii + 1].getID() == 1)
connectedBottom = true;
if(ii != 0)
if(tiles[i + 0][ii - 1].getID() == 1)
connectedUp = true;
if(connectedUp && !(connectedLeft || connectedRight || connectedBottom))tiles[i][ii].setTexture(graphics.stWallOne, 0);
if(connectedLeft && !(connectedUp || connectedRight || connectedBottom))tiles[i][ii].setTexture(graphics.stWallOne, 90);
if(connectedRight && !(connectedUp || connectedLeft || connectedBottom))tiles[i][ii].setTexture(graphics.stWallOne, 180);
if(connectedBottom && !(connectedUp || connectedRight || connectedLeft))tiles[i][ii].setTexture(graphics.stWallOne, 270);
if((connectedUp && connectedBottom) && !(connectedLeft || connectedRight))tiles[i][ii].setTexture(graphics.stWallTwo, 90);
if(!(connectedUp && connectedBottom) && !(connectedLeft || connectedRight))tiles[i][ii].setTexture(graphics.stWallTwo, 0);
}
}
map = new Map(x,y,tiles);
return map;
}
}
Given this code
public void drawRotated(BufferedImage img,double rotationRequired,int x,int y,Canvas canvas, Graphics g, Observer observer) {
//double rotationRequired = Math.toRadians(degrees);
double locationX = img.getWidth() / 2;
double locationY = img.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage rotatedImage = op.filter(img, null); // my edit here
// Drawing the rotated image at the required drawing locations
g.drawImage(rotatedImage , x, y, null);
}
The rotation of the image to get rotatedImage is only dependant upon it's size (width/height) and the required rotation.
From my reading of your code the required rotation is largely fixed. The rotation is known when the setTexture method of Title is called.
Sine this is the case, rotatedImage can be computed in the setTexture method. Just create a new attribute that stores the computed rotated image. Don't destroy the original.
If setTexture is called as often as drawRotated then this won't deliver the performance increase you desire.

How to make circle clickable

I am developing game using canvas. I have drawn grid of circles using loop. I want to get x,y coordinates of circles. For that I have to make those circles clickable so that whenever player click on a circle it should return its coordinates. I will pass those coordinates to lineDraw() method for drawing line between those circles.
This loop is defining grid of circles:
for (int y=0;y<rows;y++)
{
for (int x=0;x<cols;x++)
{
canvas.drawCircle((x + 1) * dw, (y + 1) *(3* dh), 20, pDot);
}
}
Here dw is (displayWidth) and dh is (displayHeight). Please suggest how I should make these circles clickable?
I had played with this project a little after your previous questions. I'm sure this isn't the most optimized way to do this, but without knowing how you plan to implement the game logic, I think this is flexible enough to adapt. The following is your custom View class, which I've renamed Board, in keeping with Java naming conventions.
public class Board extends View
{
Paint pBack = new Paint();
Paint pDot = new Paint();
Paint pLine = new Paint();
int cols = 5;
int rows = 6;
// Default initialization = false
boolean[][] dots = new boolean[cols][rows];
int canWidth = 0;
int canHeight = 0;
float xStep = 0;
float yStep = 0;
float[] xCoords = new float[cols];
float[] yCoords = new float[rows];
public Board(Context context)
{
super(context);
pBack.setARGB(255, 255, 102, 0);
pDot.setARGB(255, 255, 255, 255);
pLine.setStrokeWidth(5);
pLine.setARGB(255, 90, 10, 0);
}
public void setDots(boolean[][] dotSelected)
{
this.dots = dotSelected;
}
public boolean[][] getDots()
{
return dots;
}
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
canWidth = w;
canHeight = h;
xStep = canWidth / (cols + 1);
yStep = canHeight / (rows + 1);
for (int y = 0; y < rows; y++)
{
yCoords[y] = (y + 1) * yStep;
}
for (int x = 0; x < cols; x++)
{
xCoords[x] = (x + 1) * xStep;
}
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawPaint(pBack);
// Grid, lines and box markings
for (int y = 0; y < rows; y++)
{
canvas.drawLine(xStep, yCoords[y], cols * xStep, yCoords[y], pDot);
for (int x = 0; x < cols; x++)
{
if (y == 0)
{
canvas.drawLine(xCoords[x], yStep, xCoords[x], rows * yStep, pDot);
}
if (dots[x][y])
{
boolean left = x > 0 && dots[x - 1][y];
boolean up = y > 0 && dots[x][y - 1];
if (left)
{
canvas.drawLine(xCoords[x], yCoords[y], xCoords[x - 1], yCoords[y], pLine);
}
if (up)
{
canvas.drawLine(xCoords[x], yCoords[y], xCoords[x], yCoords[y - 1], pLine);
}
if (left && up && dots[x - 1][y - 1])
{
canvas.drawCircle(xCoords[x] - xStep / 2, yCoords[y] - yStep / 2, 10, pLine);
}
}
}
}
// Dots
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
canvas.drawCircle(xCoords[x], yCoords[y], 20, pDot);
if (dots[x][y])
{
canvas.drawCircle(xCoords[x], yCoords[y], 15, pBack);
}
}
}
}
public boolean onTouchEvent(MotionEvent event)
{
super.onTouchEvent(event);
if (event.getAction() != MotionEvent.ACTION_DOWN)
return true;
int xNear = 0;
int yNear = 0;
float xMin = canWidth;
float yMin = canHeight;
for (int x = 0; x < cols; x++)
{
if (Math.abs(xCoords[x] - event.getX()) < xMin)
{
xMin = Math.abs(xCoords[x] - event.getX());
xNear = x;
}
}
for (int y = 0; y < rows; y++)
{
if (Math.abs(yCoords[y] - event.getY()) < yMin)
{
yMin = Math.abs(yCoords[y] - event.getY());
yNear = y;
}
}
dots[xNear][yNear] = !dots[xNear][yNear];
invalidate();
return true;
}
}
Store the data of your circles (for instance the position and the radius) in a list and check each of them for mouse collision(register a mouseeventlistener).
public class Circle
{
private int radius;
private int positionX;
private int positionY;
public Circle(int positionX, int positionY, int radius)
{
this.radius = radius;
this.positionX = positionX;
this.positionY = positionY;
}
public int getPositionX()
{
return this.positionX;
}
public int getPositionY()
{
return this.positionY;
}
public boolean contains(int posX, int posY)
{
int distanceX = this.positionX - posX;
int distanceY = this.positionY - posY;
return Math.sqrt((distanceX * distanceX) + (distanceY * distanceY)) <= this.radius;
}

How to make the ball bounce off the circle Graphic2D? - Java

I'm working on Air Hockey game. Until now, I've moved both handles and bounce the ball in my Air Hockey table actually ball can bounce off the walls, but my problem is I can't hit the ball with my handles and I don't have any idea for that. I searched in the net but I couldn't find any thing. I'll appreciate if you help me as soon as you can!!
This is all my code that you need:
public class StartGamePanel extends JPanel implements ActionListener, KeyListener, Runnable {
private static final long serialVersionUID = 1L;
double xCircle1=200;
double yCircle1 =100 ;
double xCircle2=200;
double yCircle2 =700 ;
double xBall=200;
double yBall=400;
double rBall=20;
double ballSpeedX=-3;
double ballSpeedY=0;
private Ball m_ball = new Ball(0, 0, 7, 7);
private int m_interval = 35;
private Timer m_timer;
double dx = 0, dy = 0, dx2=0, dy2=0;
private Graphics2D circle1;
private Graphics2D circle2;
private Graphics2D circle3;
public static void main(String[] args) {
JFrame f=new JFrame();
f.setSize(new Dimension(512,837));
StartGamePanel p=new StartGamePanel();
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public StartGamePanel(){
m_timer = new Timer(m_interval, new TimerAction());
m_timer.start(); // start animation by starting the timer.
Timer t = new Timer(5, this);
t.start();
addKeyListener(this);
setFocusable(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
addKeyListener(this);
ContainerTable table=new ContainerTable(2, 0, 493, 800, new Color(51,153,255), Color.WHITE);
table.draw(g);
circle1 = (Graphics2D) g;
circle1.setColor(new Color(255,51,51));
Shape theCircle = new Ellipse2D.Double(xCircle1 - 40, yCircle1 - 40, 2.0 * 40, 2.0 * 40);
circle1.fill(theCircle);
circle2 = (Graphics2D) g;
circle2.setColor(new Color(255,102,102));
Shape theCircle2 = new Ellipse2D.Double(xCircle1 - 35, yCircle1 - 35, 2.0 * 35, 2.0 * 35);
circle2.fill(theCircle2);
circle3 = (Graphics2D) g;
circle3.setColor(new Color(255,51,51));
Shape theCircle3 = new Ellipse2D.Double(xCircle1 - 20, yCircle1 - 20, 2.0 * 20, 2.0 * 20);
circle3.fill(theCircle3);
Graphics2D circleprim = (Graphics2D) g;
circleprim.setColor(new Color(0,51,102));
Shape theCircleprim = new Ellipse2D.Double(xCircle2 - 40, yCircle2 - 40, 2.0 * 40, 2.0 * 40);
circleprim.fill(theCircleprim);
Graphics2D circle2prim = (Graphics2D) g;
circle2prim.setColor(new Color(0,102,204));
Shape theCircle2prim = new Ellipse2D.Double(xCircle2 - 35, yCircle2 - 35, 2.0 * 35, 2.0 * 35);
circle2prim.fill(theCircle2prim);
Graphics2D circle3prim = (Graphics2D) g;
circle3prim.setColor(new Color(0,51,102));
Shape theCircle3prim = new Ellipse2D.Double(xCircle2 - 20, yCircle2 - 20, 2.0 * 20, 2.0 * 20);
circle3prim.fill(theCircle3prim);
g.setColor(Color.gray);
m_ball.draw(g);
Graphics2D goal = (Graphics2D) g;
goal.setColor(Color.BLACK);
goal.fill3DRect(100, 0, 300, 10, true);
Graphics2D goal2 = (Graphics2D) g;
goal2.setColor(Color.BLACK);
goal2.fill3DRect(100, 790, 300, 10, true);
}
class TimerAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
m_ball.setBounds(getWidth(), getHeight());
m_ball.move();
repaint();
}
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
xCircle1 += dx;
yCircle1 += dy;
yCircle2 += dy2;
xCircle2+= dx2;
}
#Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
if(yCircle1>40){
dy = -2;
dx = 0;
}else{
yCircle1=40;
dy *= -1;
dx=0;
}
}
if (code == KeyEvent.VK_DOWN) {
if(yCircle1<360){
dy = 2;
dx = 0;
}else{
yCircle1=360;
dx =0;
dy *= -1;
}
}
if (code == KeyEvent.VK_LEFT) {
if (xCircle1 < 30) {
xCircle1=30;
dy = 0;
dx *= -1;
}else {
dy = 0;
dx = -2;
}
}
if (code == KeyEvent.VK_RIGHT) {
if (xCircle1 > 460) {
xCircle1= 460;
dx *= -1;
dy = 0;
}else{
dy = 0;
dx = 2;
}
}
if (code == KeyEvent.VK_F) {
if (xCircle2 > 460) {
xCircle2= 460;
dy2 = 0;
dx2 *= -1;
}else{
dy2 = 0;
dx2 = 2;
}
}
if (code == KeyEvent.VK_S) {
if (xCircle2 < 30) {
xCircle2=30;
dy2 = 0;
dx2 *= -1;
}else {
dy2 = 0;
dx2 = -2;
}
}
if (code == KeyEvent.VK_E) {
if(yCircle2>430){
dy2 = -2;
dx2 = 0;
}else{
yCircle2=430;
dy*= -1;
dx=0;
}}
if (code == KeyEvent.VK_D) {
if(yCircle2>750){
yCircle2=750;
dy2 *= -1;
dx2=0;
}else{
dy2 = 2;
dx2 = 0;
}}
}
#Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
dy = 0;
dx = 0;
}
if (code == KeyEvent.VK_DOWN) {
dy = 0;
dx = 0;
}
if (code == KeyEvent.VK_LEFT) {
dy = 0;
dx = 0;
}
if (code == KeyEvent.VK_RIGHT) {
dy = 0;
dx = 0;
}
if (code == KeyEvent.VK_E) {
dy2 = 0;
dx2 = 0;
}
if (code == KeyEvent.VK_D) {
dy2 = 0;
dx2 = 0;
}
if (code == KeyEvent.VK_S) {
dy2 = 0;
dx2 = 0;
}
if (code == KeyEvent.VK_F) {
dy2 = 0;
dx2 = 0;
}
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
xBall += ballSpeedX;
yBall += ballSpeedX;
repaint();
if (xBall < 30) {
ballSpeedX = -ballSpeedX;
xBall = 30;
}
if (xBall > 470) {
ballSpeedX = -ballSpeedX;
xBall = 470;
}
try { Thread.sleep (20); }
catch (InterruptedException ex) {}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY); }}}
class Ball {
final static int DIAMETER = 50;
private int m_x;
private int m_y;
private int m_velocityX;
private int m_velocityY;
private int m_rightBound;
private int m_bottomBound;
public Ball(int x, int y, int velocityX, int velocityY) {
m_x = x;
m_y = y;
m_velocityX = velocityX;
m_velocityY = velocityY;
}
public void setBounds(int width, int height) {
m_rightBound = width - DIAMETER;
m_bottomBound = height - DIAMETER;
}
public void move() {
m_x += m_velocityX;
m_y += m_velocityY;
if (m_x < 0) {
m_x = 0;
m_velocityX = -m_velocityX;
} else if (m_x > m_rightBound) {
m_x = m_rightBound;
m_velocityX = -m_velocityX;
}
if (m_y < 0) {
m_y = 0;
m_velocityY = -m_velocityY;
} else if (m_y > m_bottomBound) {
m_y = m_bottomBound;
m_velocityY = -m_velocityY;
}
}
public void draw(Graphics g) {
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
public int getDiameter() { return DIAMETER;}
public int getX() { return m_x;}
public int getY() { return m_y;}
public void setPosition(int x, int y) {
m_x = x;
m_y = y;
}
}
class ContainerTable {
public int minX;
public int maxX;
public int minY;
public int maxY;
private Color colorFilled;
private Color colorBorder;
public ContainerTable(int x, int y, int width, int height, Color colorFilled, Color colorBorder) {
minX = x;
minY = y;
maxX = x + width - 1;
maxY = y + height - 1;
this.colorFilled = colorFilled;
this.colorBorder = colorBorder;
}
public void set(int x, int y, int width, int height) {
minX = x;
minY = y;
maxX = x + width - 1;
maxY = y + height - 1;
}
public void draw(Graphics g) {
g.setColor(colorFilled);
g.fillRect(minX, minY, maxX - minX - 1, maxY - minY - 1);
g.setColor(colorBorder);
g.drawRect(minX, minY, maxX - minX - 1, maxY - minY - 1);
Graphics2D southArc = ( Graphics2D ) g;
southArc.setColor ( Color.WHITE );
southArc.setStroke(new BasicStroke(3));
southArc.drawArc ( 98, 640 , 300, 300 , 0, 180 );
Graphics2D northArc = ( Graphics2D ) g;
northArc.setColor ( Color.WHITE );
northArc.setStroke(new BasicStroke(3));
northArc.drawArc ( 98, -143 , 300, 300 , 180, 180 );
Graphics2D line = ( Graphics2D ) g;
line.setStroke(new BasicStroke(3));
line.setColor(Color.white);
line.drawLine(4, 395, 491, 395);
Graphics2D dot=(Graphics2D) g;
dot.setColor(Color.black);
for(int j=10;j<800;j+=20){
for(int i=6;i<502;i+=20){
dot.drawLine(i, j, i, j);
}
}
}
public int getMinX() {
return minX;
}
public void setMinX(int minX) {
this.minX = minX;
}
public int getMaxX() {
return maxX;
}
public void setMaxX(int maxX) {
this.maxX = maxX;
}
public int getMinY() {
return minY;
}
public void setMinY(int minY) {
this.minY = minY;
}
public int getMaxY() {
return maxY;
}
public void setMaxY(int maxY) {
this.maxY = maxY;
}}

Categories

Resources