如何在其他所有内容前面绘制光标图像?

问题描述 投票:0回答:1

我正在尝试在Processing Java中创建一个MS Paint类型的应用程序,到目前为止,我有点卡在一个部分。在我的应用程序中,我将要绘制的形状显示为以鼠标为中心的红色透明圆圈。但是,它是在屏幕上显示的形状后面绘制的。

画画

int mSize = 25;
int mChoice = 0;
color dColor;

Cursors c;
void setup(){
  size(1000,1000);
  
  c = new Cursors(mSize, dColor);
}

void draw(){
  frameRate(120);
  noStroke();
  background(255);
  screenTop();
  c.cursorSet(mChoice);
}


void mouseWheel(MouseEvent event) {
  //gets which way mouse moves and changes brush size
  float e = event.getCount();
  c.update(e);
}

void screenTop(){
  //sets menu top
  fill(200);
  rectMode(CENTER);
  rect(width/2, height/20, width,height/10);
  fill(255);
  stroke(0);
  strokeWeight(5);
  rect(100,50,50,50);
  circle(200,50,50);
  triangle(275,75,300,25,325,75);
  fill(0);
  textSize(25);
  textAlign(CENTER);
  text("ERASE", 925,40);
  text("CLEAR", 925,80);
  fill(255,0.5);
  
  //highlight for items
  if((mouseX > 60 && mouseX < 140) && (mouseY > 10 && mouseY < 90)){
    rect(100,50,75,75);
  } else if((mouseX > 160 && mouseX < 240) && (mouseY > 10 && mouseY < 90)){
    rect(200,50,75,75);
  } else if((mouseX > 260 && mouseX < 340) && (mouseY > 10 && mouseY < 90)){
    rect(300,50,75,75);
  } else if((mouseX > 875 && mouseX<975) && (mouseY > 10 && mouseY<52.5)){
    rect(925,30,100,40);
  } else if((mouseX > 875 && mouseX<975) && (mouseY > 60 && mouseY<95)){
    rect(925,70,100,40);
  }
  
  //actually changes highlight info, because else/if stuff that makes barely any sense
  if(((mouseX > 60 && mouseX < 140) && (mouseY > 10 && mouseY < 90)) && mousePressed){
    mChoice = 1;
  } else if(((mouseX > 160 && mouseX < 240) && (mouseY > 10 && mouseY < 90)) && mousePressed){
    mChoice = 0;
  } else if(((mouseX > 260 && mouseX < 340) && (mouseY > 10 && mouseY < 90)) && mousePressed){
    mChoice = 2;
  }else if(((mouseX > 875 && mouseX<975) && (mouseY > 10 && mouseY<52.5)) && mousePressed && c.mouseClick){
    c.erase = !c.erase;
  }else if(((mouseX > 875 && mouseX<975) && (mouseY > 60 && mouseY<95)) && mousePressed && c.mouseClick){
    c.clear = !c.clear;
  }
  
  
  strokeWeight(1);
  noStroke();
}

void mouseClicked(){
  c.mouseClick = !c.mouseClick;
}

光标

class Cursors{
  ArrayList<Point> points = new ArrayList<Point>();
  
  float mSize;
  int mSizeCnt = 1;
  //hex code for coloring
  int dColor;
  boolean erase = false;
  boolean clear = false;
  boolean mouseClick = false;
  public Cursors(float mSize, color dColor){
    this.mSize = mSize;
    this.dColor = dColor;
  }
  
  void update(float e){
  
    //increases/deceases brush size
    if(e == -1){
      mSize+=12.5;
    } else {
      mSize-=12.5;
    }
  
  //locks size
    if(mSize <= 12.5){mSize=12.5;}
    if(mSize >= 250){mSize=250;}
  }
  
  void cursorSet(int s) {
    //sets shape to cursor
    fill(dColor,0);
    float shapeOffset =  mSize / 2;
    stroke(#FF0000);
    if(mouseY > 100+shapeOffset){
       switch(s){
      case 0:
        circle(mouseX,mouseY,mSize);
        break;
      case 1:
        rect(mouseX,mouseY,mSize,mSize);
        break;
      case 2:
        triangle(mouseX-mSize/2,mouseY+mSize/2,mouseX,mouseY -mSize/2, mouseX+mSize/2,mouseY+mSize/2);
        break;
      }
    } 
    
    
    if(clear){
      points.clear();
      //sets back to default if cleared
      clear = false;
      erase = false;
    } else if(erase){
      dColor = #FFFFFF;
    } else {
      dColor = #000000;
    }
    //adds new points to arraylist
    if(mousePressed && mouseY > 100+mSizeCnt + shapeOffset){
      points.add(new Point(mouseX, mouseY, mSize, s, dColor));
    }

    //draws shape
    for (Point point : points) {
      fill(point.c);
      if(point.c == #FFFFFF){
        noStroke();
      } else {
        stroke(0);
      }
      //switches shape for each drawn
      switch(point.brushType){
        case 0:
          circle(point.x, point.y, point.size);
          break;
        case 1:
          rect(point.x, point.y, point.size, point.size);
          break;
        case 2:
          triangle(point.x - point.size/2, point.y + point.size/2, point.x ,point.y - point.size/2, point.x + point.size/2, point.y + point.size/2);
          break;
      }
    }
  }
}  

积分

    class Point {
  float x, y, size;
  int brushType;
  color c;
  //gets all data for each drawn object
  Point(float x, float y, float size, int brushType, color c) {
    this.x = x;
    this.y = y;
    this.size = size;
    this.brushType = brushType;
    this.c = c;
     }
    }

到目前为止,我已经阅读了一些有关处理 IDE 工作原理的内容。我意识到先绘制的项目将显示在后面的对象后面。但是,无论我如何更改代码中首先调用的方法,我似乎都无法更改绘制顺序。我也尝试过调用它两次,一次在绘图方法之前和之后,但没有成功。

我也知道我的按钮现在坏了,我正在努力修复它。

java processing awt
1个回答
0
投票

尝试使用“光标(img)”。首先创建一个具有透明背景的圆形图像,并将其放入草图文件夹中。然后运行以下源代码:

PImage img;

void setup() {
  size(600, 600);
  img = loadImage("circle.png");
  cursor(img);
}

void draw() {
}

void mousePressed(){
 fill(0,0,255);
 rect(mouseX,mouseY,100,100); 
}

您应该在新创建的蓝色矩形顶部看到透明圆圈。

© www.soinside.com 2019 - 2024. All rights reserved.