gpt4 book ai didi

java - 单击按钮后弹出异常(java fx)

转载 作者:行者123 更新时间:2023-12-02 01:53:21 28 4
gpt4 key购买 nike

我正在制作一个应用程序,您可以选择绘制矩形或直线,并且还可以选择清除 Canvas 。清除 Canvas 按钮可以工作,但每次单击清除 Canvas 按钮时都会显示一堆异常。我只是想通过尝试确定它的含义来消除异常

我已经尝试仔细检查代码中的错误(任何语法或逻辑错误),但我仍然没有指出问题所在。我是一个相当新的程序员,所以我可能没有足够的知识来尝试找到它,我也要求其他人看一下,他们说一切似乎都很好。

以下是单击一键后的错误总数 ( /image/JYkNt.jpg )。

这是 GitHub 上的代码 ( https://github.com/DWER-afk/DWERR.git )

感谢所有帮助,并提前感谢您查看它。(刚刚添加的代码块和 Line、Main 和矩形都是单独的类)

`import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import org.w3c.dom.css.Rect;

import java.util.ArrayList;



public class Main extends Application {

// Layout sizes
int a = 0;
final double SCREEN_WIDTH = 1000;
final double SCREEN_HEIGHT = 800;
final double CONTROL_HEIGHT = -175;
private GraphicsContext gc, transgc;
private double sx, sy;
private double ex, ey;
private Color lineColor = Color.BLUE;
private ColorPicker lineColorPicker;
private TextField tf;
public double lines123 = 1.0;

// Reference to the Model
private ArrayList<Rectangle> rectangle1;
private ArrayList<Line> lines;
// TODO: Private Event Handlers and Helper Methods

private void pressHandler(MouseEvent me) {
sx = me.getX();
sy = me.getY();
}

private void releaseHandler(MouseEvent me) {
ex = me.getX();
ey = me.getY();
if(a == 0){Line l = new Line(sx,sy,ex,ey,lineColorPicker.getValue(),lines123);
lines.add(l);
for (Line l1 : lines)
l1.draw(gc);
transgc.clearRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);}
if(a == 1){Rectangle rl = new Rectangle(sx,sy,ex,ey,lineColorPicker.getValue(),lines123);
rectangle1.add(rl);

for (Rectangle r1 : rectangle1)
r1.draw(gc);
transgc.clearRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);}



}


/**
* This will draw a "Rubberband" line on the transparent surface above the drawing.
*
* @param me The mouse drag event - not used in method
*/
private void dragHandler(MouseEvent me) {
transgc.clearRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
ex = me.getX();
ey = me.getY();
if(a == 0){
Line tempLine = new Line(sx,sy,ex,ey,lineColorPicker.getValue(),lines123);
tempLine.draw(transgc);
}
if (a == 1){
Rectangle temprect = new Rectangle(sx,sy,ex,ey,lineColorPicker.getValue(),lines123);
temprect.draw(transgc);
}

}

/**
* This is where you create your components and the model and add event
* handlers.
*
* @param stage The main stage
* @throws Exception
*/
@Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, SCREEN_WIDTH, SCREEN_HEIGHT, Color.DARKGRAY); // set the size here
stage.setTitle("Java Shape Drawer by Alexei Ougriniouk"); // set the window title here
stage.setScene(scene);
// TODO: Add your GUI-building code here

// 1. Create the model - No model yet

lines = new ArrayList<>();
rectangle1 = new ArrayList<>();
// 2. Create the GUI components - Just a CANVAS for now

Canvas c = new Canvas(SCREEN_WIDTH, SCREEN_HEIGHT - CONTROL_HEIGHT);
c.relocate(0, CONTROL_HEIGHT);
Canvas transc = new Canvas(SCREEN_WIDTH, SCREEN_HEIGHT);
transc.relocate(0, CONTROL_HEIGHT);

// Add JavaFX controls to top pane ...

Label colorLabel = new Label("Change line Colours");
colorLabel.relocate(100, 625);

lineColorPicker = new ColorPicker(lineColor);
lineColorPicker.relocate(100, 650);

Button resetButton = new Button("Reset Shape Colors");
resetButton.relocate(100,750);
resetButton.setOnAction(this::resetHandler);

Button clearstuff = new Button("Clear Canvas");
clearstuff.relocate(450,675);
clearstuff.setOnAction(this::clearHandler);

Button linedraw = new Button("line");
linedraw.relocate(350,635);
linedraw.setDefaultButton(true);
linedraw.setOnAction(this::lineHandler);

Button rectdraw = new Button("rectangle");
rectdraw.relocate(450,635);
rectdraw.setOnAction(this::rectHandler);

tf = new TextField("1");
tf.relocate(250,750);

// 3. Add components to the root
root.getChildren().addAll(c, transc, colorLabel, lineColorPicker, resetButton, tf,clearstuff,linedraw,rectdraw);

// Create the two graphics contexts

gc = c.getGraphicsContext2D();
transgc = transc.getGraphicsContext2D();

gc.setFill(Color.WHITE);
gc.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// 5. Add Event Handlers and do final setup
transc.addEventHandler(MouseEvent.MOUSE_PRESSED, this::pressHandler);
transc.addEventHandler(MouseEvent.MOUSE_RELEASED, this::releaseHandler);
transc.addEventHandler(MouseEvent.MOUSE_DRAGGED, this::dragHandler);
// 6. Show the stage
stage.show();
}
private void clearHandler(ActionEvent actionEvent){
gc.clearRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
for(Line l : lines) {
l.setLineColor(lineColorPicker.getValue());
lines.clear();
}
for(Rectangle rl : rectangle1 ) {
rl.setLineColor(lineColorPicker.getValue());
rectangle1.clear();
}

}


private void resetHandler(ActionEvent actionEvent) {
for(Line l : lines) {
l.setLineColor(lineColorPicker.getValue());
l.draw(gc);
}
for(Rectangle l : rectangle1) {
l.setLineColor(lineColorPicker.getValue());
l.draw(gc);
}



try {
int lineWidth = Integer.parseInt(tf.getText());
if(lineWidth > 0){
new Alert(Alert.AlertType.INFORMATION,
"VALUE APPROVED : Value in TextBox = " + String.valueOf(lineWidth)).showAndWait() ;
lines123 = (double) lineWidth;
}
if(lineWidth <= 0){
new Alert(Alert.AlertType.ERROR,
"Could not convert " + tf.getText() + " to an integer due to it being negative").showAndWait();
}

}
catch (NumberFormatException ex)
{
new Alert(Alert.AlertType.ERROR,
"Could not convert " + tf.getText() + " to an integer").showAndWait();

}

}
private void lineHandler(ActionEvent actionEvent) {
a = 0;
if(a == 0){for(Line l : lines) {
l.setLineColor(lineColorPicker.getValue());
l.draw(gc);
}}


}
private void rectHandler(ActionEvent actionEvent) {
a = 1;
if(a == 1){for(Rectangle l : rectangle1) {
l.setLineColor(lineColorPicker.getValue());
l.draw(gc);
}}
}

/**
* Make no changes here.
* @param args unused
*/
public static void main(String[] args) {
launch(args);
}
}
'

这是 Line 类

public class Line {
Main sc = new Main();
private double sx, sy, ex, ey;
private Color lineColor;
double lineswidth = sc.lines123;
public Line(double sx, double sy, double ex, double ey, Color lineColor, double lineswidth)
{
this.sx = sx;
this.sy = sy;
this.ex = ex;
this.ey = ey;
this.lineColor = lineColor;
this.lineswidth = lineswidth;
}

/**
* Draw a rectangle to the Graphics context
* @param gc the graphics context of the FX component
*/
public void draw(GraphicsContext gc) {
gc.setStroke(lineColor);
gc.setLineWidth(lineswidth);
// For circle drawing must get the smaller of the two X values, same for Y
gc.strokeLine (sx,sy,ex,ey);
}

public void setLineColor(Color newColor)
{
lineColor = newColor;
}


@Override
public String toString() {
return "Line{}" + super.toString();
}

}

这是矩形类

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Rectangle {
Main sc = new Main();
private double sx, sy, ex, ey;
private Color lineColor;
double lineswidth = sc.lines123;
public Rectangle(double sx, double sy, double ex, double ey, Color lineColor, double lineswidth)
{
this.sx = sx;
this.sy = sy;
this.ex = ex;
this.ey = ey;
this.lineColor = lineColor;
this.lineswidth = lineswidth;
}

/**
* Draw a rectangle to the Graphics context
* @param gc the graphics context of the FX component
*/
public void draw(GraphicsContext gc) {
{
gc.setStroke(Color.BLACK);
gc.strokeRect(sx,sy,ex,ey);
gc.setLineWidth(lineswidth);
// For circle drawing must get the smaller of the two X values, same for Y
gc.setFill(lineColor);
gc.fillRect(sx,sy,ex,ey);
}

}

public void setLineColor(Color newColor)
{
lineColor = newColor;
}


@Override
public String toString() {
return "Line{}" + super.toString();
}

}

最佳答案

在两个绘图函数中,在开始时执行以下操作:double sx = this.sx, sy = this.sy, ex = this.ex, ey = this.ey;

现在,绘制时变量无法更改。

关于java - 单击按钮后弹出异常(java fx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57413428/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com