如何在java/processing中显示移动对象内的文本

如何在java/processing中显示移动对象内的文本,java,processing,Java,Processing,我使用的是处理IDE,我发现了如何显示静态文本,而不是显示在内部并随移动磁盘移动的文本。有什么帮助吗 int xdirection = 1; // Left or Right int ydirection = 1; // Top to Bottom int value = 0; Disk disk1; Disk disk2; Disk disk3; Disk disk4; void setup() { size(400, 400); cursor(CROSS); noStrok

我使用的是处理IDE,我发现了如何显示静态文本,而不是显示在内部并随移动磁盘移动的文本。有什么帮助吗

int xdirection = 1;  // Left or Right
int ydirection = 1;  // Top to Bottom
int value = 0;
Disk disk1;
Disk disk2;
Disk disk3;
Disk disk4;

void setup() {
  size(400, 400);
  cursor(CROSS);
  noStroke();
  disk1 = new Disk(color(255, 0, 0), 10, 100, 2, 6);
  disk2 = new Disk(color(0, 0, 255), 10, 100, 1, 5);
  disk3 = new Disk(color(0, 255, 0), 10, 100, 3, 4);
  disk4 = new Disk(color(0, 255, 255), 10, 100, 4, 7);
  frameRate(60);
}
void mousePressed() {
  if (disk1.isOver() == true) {
    value += 25;
  }
  if (disk2.isOver() == true) {
    value += 50;
  }
  if (disk3.isOver() == true) {
    value += 75;
  }
  if (disk4.isOver() == true) {
    value += 100;
  }
}

void draw() {
  background(130, 140, 255);
  stroke(175);
  fill(0);
  textAlign(CENTER);
  text("Score: " + value, width/2, 60);
  disk1.bounce();
  disk1.display();
  disk2.bounce();
  disk2.display();
  disk3.bounce();
  disk3.display();
  disk4.bounce();
  disk4.display();
}

class Disk {
  color c;
  float xpos;
  float ypos;
  float xspeed;
  float yspeed;

  Disk(color tempC, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed) {
    c = tempC;
    xspeed = tempXspeed;
    yspeed = tempYspeed;
    if (xpos > width-200 || xpos < 0) {
      xdirection *= -1;
    }
    if (ypos > height-200 || ypos < 0) {
      ydirection *= -1;
    }
  }

  void display() {
    stroke(0);
    fill(c);
    ellipseMode(CENTER);
    ellipse(xpos, ypos, 20, 20);
  }

  boolean isOver() {
    // Is mouse position over the circle i.e. dist < radius
    if (dist(xpos, ypos, mouseX, mouseY) < 10)
      return true;
    else
      return false;
  }

  void bounce() {
    xpos = xpos + xspeed;
    xpos = xpos + ( xspeed * xdirection );
    ypos = ypos + ( yspeed * ydirection );
    if (xpos > width - 20) {
      xspeed = xspeed * -1;
    }
    if (xpos < 0) {
      xspeed = xspeed * -1;
    }
    if (ypos > height - 20) {
      yspeed = yspeed * -1;
    }
    if (ypos < 0) {
      yspeed = yspeed * -1;
    }
  }
}
int xdirection=1;//左还是右
int ydirection=1;//自上而下
int值=0;
磁盘disk1;
磁盘2;
磁盘3;
磁盘4;
无效设置(){
尺寸(400400);
光标(十字);
仰泳();
disk1=新磁盘(颜色(255,0,0),10100,2,6);
disk2=新磁盘(颜色(0,0,255),10100,1,5);
disk3=新磁盘(颜色(0,255,0),10,100,3,4);
disk4=新磁盘(颜色(0,255,255),10100,4,7);
帧率(60);
}
void mousePressed(){
if(disk1.isOver()==true){
数值+=25;
}
if(disk2.isOver()==true){
数值+=50;
}
if(disk3.isOver()==true){
数值+=75;
}
if(disk4.isOver()==true){
数值+=100;
}
}
作废提款(){
背景(130140255);
中风(175);
填充(0);
文本对齐(中心);
文本(“分数:”+值,宽度/2,60);
disk1.bounce();
disk1.display();
disk2.bounce();
disk2.display();
disk3.bounce();
disk3.display();
disk4.bounce();
disk4.display();
}
类磁盘{
颜色c;
浮动XPO;
浮动YPO;
浮动速度;
浮动速度;
磁盘(彩色tempC、浮点tempXpos、浮点tempYpos、浮点tempXspeed、浮点tempYspeed){
c=tempC;
xspeed=tempXspeed;
y速度=tempYspeed;
如果(xpos>width-200 | | xpos<0){
xdirection*=-1;
}
如果(ypos>height-200 | | ypos<0){
y方向*=-1;
}
}
无效显示(){
冲程(0);
填充(c);
ellipseMode(中心);
椭圆(XPO,YPO,20,20);
}
布尔isOver(){
//鼠标在圆上的位置,即距离<半径
if(距离(XPO、YPO、mouseX、mouseY)<10)
返回true;
其他的
返回false;
}
无效反弹(){
xpos=xpos+xspeed;
xpos=xpos+(xspeed*xdirection);
ypos=ypos+(Y速度*Y方向);
如果(xpos>宽度-20){
xspeed=xspeed*-1;
}
如果(xpos<0){
xspeed=xspeed*-1;
}
如果(ypos>高度-20){
yspeed=yspeed*-1;
}
if(ypos<0){
yspeed=yspeed*-1;
}
}
}

编辑:更新帖子以显示代码。我也在尝试如何使用
scale()
使磁盘变大,但还没有找到如何做到这一点。这也是我第一次在stack overflow上发帖,所以如果有什么我可以做得更好的,请告诉我。感谢磁盘
类封装了单个
磁盘
实例的状态。它包含检查鼠标点击和弹跳的代码。它还包含用于绘制磁盘的代码

该代码发生在
display()
函数中。通读该函数以了解它在做什么,并尝试更改它以查看发生了什么

具体来说,这行代码在磁盘位置绘制了一个圆圈:

ellipse(xpos, ypos, 20, 20);
请尝试更改在磁盘位置绘制矩形的选项。然后尝试更改它以在磁盘位置绘制一些文本


祝你好运

请出示一些代码。另外:文本的坐标必须跟随您希望它跟随的对象,因此您必须相应地计算它们。如果你感到困惑,我可能会帮助你,一旦我能看到你在做什么。@laancelot Postupdated@laancelot这是基于给我的模板。我还在学习过程中,所以如果你能给我一些指导,那就太好了。这是一个家庭作业,所以我不想直接得到答案凯文是第一个,他的答案很好,但如果你仍然感到困惑,你仍然可以给我留言(链接@或者我不知道)。谢谢!我再试试看