简体   繁体   English

用Java绘制矩形

[英]Drawing rectangles in java

I have this code where I have random rectangles generating over the entire canvas. 我有这段代码,其中我在整个画布上生成随机矩形。 I also have 1 rectangle at the center. 我在中央也有1个矩形。

I need to get the 1 rectangle at the center to move around when the user hovers the mouse over the canvas. 当用户将鼠标悬停在画布上时,我需要在中心获得1个矩形来移动。

Here is my code 这是我的代码

package sample;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.Random;


public class Main extends Application {

private static final int DRAW_WIDTH  = 800;
private static final int DRAW_HEIGHT = 500;

private Animation myAnimation;
private Canvas canvas;
private GraphicsContext gtx;

@Override
public void start(Stage stage) throws Exception
{

    stage.setTitle("tRIPPy BoXXX");

    canvas = new Canvas(DRAW_WIDTH, DRAW_HEIGHT);
    gtx = canvas.getGraphicsContext2D();
    gtx.setLineWidth(3);
    gtx.setFill(Color.BLACK);
    gtx.fillRect(0, 0, DRAW_WIDTH, DRAW_HEIGHT);


    VBox vBox = new VBox();
    vBox.getChildren().addAll(canvas);
    Scene scene = new Scene(vBox, DRAW_WIDTH, DRAW_HEIGHT);
    stage.setScene(scene);
    stage.show();
    myAnimation = new Animation();
    myAnimation.start();
}


class Animation extends AnimationTimer
{
    @Override

    public void handle(long now)
    {

        Random rand = new Random();
        double red   = rand.nextDouble();
        double green = rand.nextDouble();
        double blue  = rand.nextDouble();
        Color boxColor = Color.color(red,green,blue);
        gtx.setStroke(boxColor);
....

This is the box that I want to have moving around with the users mouse. 这是我要用用户鼠标移动的框。 I have tried some things on my own, however I can't get the rect to stay the way it is in the code. 我已经尝试了一些事情,但是我无法保持代码的原样。

      .....
      int rectX = 800 / 2;
      int rectY = 500 / 2;
      for (int side = 10; side <= 100; side += 10) {
        gtx.strokeRect(rectX - side / 2, rectY - side / 2, side, side);
      }


      int centerX = (int)(Math.random()*800);
      int centerY = (int)(Math.random()*800);
      for (int side = (int)(Math.random()*100); side <= Math.random()* 100; side += Math.random()*100)

      {
        gtx.strokeRect(centerX - side / 2, centerY - side / 2, side, side);

      }
    }
}


public static void main(String[] args)
{
    launch(args);
}

If you plan to move some graphics arround, why do you then start with a canvas? 如果您计划移动一些图形,为什么要从画布开始呢? On a canvas you cannot move anything arround unless you constantly want to redraw everything again and again. 在画布上,除非一直想一次又一次地重画所有内容,否则您不能移动任何东西。 Putting your rectangles into the scene graph is much better suited for this task. 将矩形放入场景图更适合此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM