Error in code by classes
-
Good. I have now begun studying java programming which includes several classes in which one calls another. But I'm coming across a mistake I can't understand. I've searched similar-effect codes and I can't figure out a clear difference of error. The goal is to create a class that makes a tablet-shaped drawing (Cumprido), and another that draws a grid of these tablets.
In the drawing code I have this:
import javafx.application.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; import javafx.scene.shape.*; import javafx.embed.swing.*; import javafx.application.*; import javafx.scene.text.*; import java.util.*; import javafx.scene.paint.Color;
public class Pill {
private Pane pane;
public void pill(int x, int y, int width, int height, Color leftColor, Color rightColor)
{
Rectangle leftRect= new Rectangle(x,y,width/4,height);
leftRect.setStrokeWidth(0);
leftRect.setFill(leftColor);
pane.getChildren().add(leftRect);Rectangle rightRect = new Rectangle(x+width/4,y,width/4,height); rightRect.setStrokeWidth(0); rightRect.setFill(rightColor); pane.getChildren().add(rightRect); Ellipse leftEllipse = new Ellipse(x,y+height/2,width/4,height/2); leftEllipse.setStrokeWidth(0); leftEllipse.setFill(leftColor); pane.getChildren().add(leftEllipse); Ellipse rightEllipse = new Ellipse(x+width/2,y+height/2,width/4,height/2); rightEllipse.setStrokeWidth(0); rightEllipse.setFill(rightColor); pane.getChildren().add(rightEllipse);
}
} // END class Pill
Which compiles and has already tried to put in a class apart and performs without problem.
Where is the mistake is in this:import javafx.application.;
import javafx.event.;
import javafx.scene.;
import javafx.scene.control.;
import javafx.scene.layout.;
import javafx.stage.;
import javafx.scene.shape.;
import javafx.embed.swing.;
import javafx.application.;
import javafx.scene.text.;
import java.util.*;
import javafx.scene.paint.Color;public class Drawing {
private Pane pane;
private void start(Stage primaryStage)
{
primaryStage.setOnCloseRequest(
e -> Platform.runLater( () -> {Platform.exit(); System.exit(0);} ));// WRITE YOUR CODE HERE // TODO //
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Pane.html
this.pane = new Pane();
this.pane.setPrefSize(900, 600);
primaryStage.setScene(new Scene(this.pane, Color.WHITE));
primaryStage.show();Pill pill = new Pill (150,100,300,200,Color.BLACK,Color.GRAY); //Aqui dá um erro(Constructor Pill cannot be applied to class Pill cannot be applied to given types). this.pane.getChildren().add(pill);//Aqui da outro erro(No suitable method for add(pill). this.drawGrid();
} // END start
public void addShape(Shape shape)
{
Platform.runLater(() -> this.pane.getChildren().add(shape));
}/** execute this method to start the program
- executing the code in method start(Stage primaryStage)
*/
public static void start()
{
Drawing drawingApp = new Drawing();
drawingApp.launch();
}
public void launch()
{
// Initialises JavaFX:
new JFXPanel();
// Makes sure JavaFX doesn't exit when first window is closed:
Platform.setImplicitExit(false);
// Runs initialisation on the JavaFX thread:
Platform.runLater(() -> start(new Stage()));
}public Drawing()
{
super();
}public void drawGrid(int x, int y, int nLines, int nColumns, int width, int height, Color leftColor, Color rightColor)
{
nLines=3;
nColumns = 4;
for(int i = 300;i<=nLines350;i+=350)
{
for(int j = 200; j <=nColumns250;i+=250)
{
Pill p = new Pill (i,j,width,height,Color.BLACK,Color.GRAY); // Dá um erro igual ao primeiro.
}
}
}} // END class Drawing
- executing the code in method start(Stage primaryStage)
-
The problem of your code is in the class builder call
Pill
, since there is no constructor that meets what was called in classDrawing
. Since a constructor has not been defined with the parameters of the call that is made in this last one, the code cannot compileA possible solution would be to rename the method
public void pill(...)
Forpublic Pill(...)
, staying as follows:public class Pill { private Pane pane; public Pill(int x, int y, int width, int height, Color leftColor, Color rightColor){ ... } }
Note that there are still two conceptual problems in your code:
1 - Builder has no return type;
2 - Builder needs to have the same class name (do not forget that Java is case sensitive!).