Java command line arguments:
Ever wondered what that weird String[] args thing is? Well, it’s an array of command line arguments, and it’s of an indeterminate size.
Here’s an example of printing out the command line arguments of a Java program:
public class Main{
public static void main(String[] args){
System.out.println(args[0]);
System.out.println(args[1]);
}
}
Then, when you run the program, you need to run it with 2 command line arguments.
Java GUI options:
Your options for making GUI applications in Java include Swing, JavaFX, OpenJFX, and a web frontend (if you’re doing back-end Java, such as with JSP (JavaServer Pages)). I personally use JavaFX with Java 8 to make graphical desktop programs.
Basic JavaFX GUI:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.scene.layout.VBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.ScrollPane;
public class MainFX extends Application {
BorderPane bPane = new BorderPane();
GridPane gPane = new GridPane();
VBox box = new VBox();
TextArea bottomOutput = new TextArea(“testing hello 123”);
Button clickMe;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
buildUI();
customizeUI();
registerEvents();
Scene scene = new Scene(bPane);
primaryStage.setScene(scene);
primaryStage.setTitle(“Here’s a GUI program in Java!”);
primaryStage.show();
}
public void buildUI() {
clickMe = new Button(“Click me”);
clickMe.setMinWidth(130);
gPane.add(clickMe, 0, 0);
bPane.setCenter(gPane);
box.getChildren().add(bottomOutput);
bPane.setBottom(box);
}
public void customizeUI() {
gPane.setHgap(10);
gPane.setVgap(10);
box.setSpacing(10);
bPane.setPadding(new Insets(10, 10, 10, 10));
gPane.setPadding(new Insets(0, 0, 10, 0));
bottomOutput.appendText(“\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n”);
bottomOutput.appendText(“I converted this from a different app I made.”);
}
public void registerEvents() {
clickMe.setOnAction(e -> {
bottomOutput.appendText(“\nYou clicked the button! Yay!”);
});
}
}
The above code isn’t the cleanest, and I’m not really explaining how it works in-depth, but that’s because this book doesn’t focus on GUI stuff. I had a different app I coded a long time ago and I kind of just cut out a lot of features to show a very simple GUI program with a button you can click on, and it does stuff when it’s clicked.
Viewing mouse position and automating keyboard and mouse input
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class Main {
public static void main(String[] args) {
try {
//print info about the mouse location
System.out.print(MouseInfo.getPointerInfo().getLocation().x);
System.out.print(“, “);
System.out.println(MouseInfo.getPointerInfo().getLocation().y);
//move the mouse
Robot machine = new Robot();
machine.mouseMove(123,123); //x,y coords
//click the mouse
machine.mousePress(InputEvent.BUTTON1_DOWN_MASK);
machine.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
a
//press the down arrow
machine.keyPress(KeyEvent.VK_DOWN);
machine.keyRelease(KeyEvent.VK_DOWN);
//press the “A” key
machine.keyPress(KeyEvent.VK_A);
machine.keyRelease(KeyEvent.VK_A);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
Java sleep (pausing program in terms of milliseconds):
public class Main{
public static void main(String[] args) throws InterruptedException{
String wordsToSay = “Hello there. How are you today?”;
String[] wordArray = wordsToSay.split(” “);
for (int i = 0; i < wordArray.length; i++) {
aSystem.out.println(wordArray[i]);
Tahread.sleep(1000);
}
}
}
Java’s indexOf() String method:
String longString = “here is a long string”;
String searchTerm = “str”;
System.out.println(longString.indexOf(searchTerm));
String notInString = “this is not in the string”;
System.out.println(longString.indexOf(notInString));
Java charAt():
public class Main{
public static void main(String[] args){
String letters = “abcdefghijkl”;
System.out.println(letters.charAt(3));
}
}
Java stringVar.length() method:
public class Main{
public static void main(String[] args) throws InterruptedException{
String someString = “here’s a string”;
System.out.println(someString.length());
}
}
Java arrayVar.length property:
public class Main{
public static void main(String[] args) throws InterruptedException{
int[] intArray = {1, 2, 143, 6, 4, 2, 72};
System.out.println(intArray.length);
}
}
Java instanceof keyword:
Used to see if something is a particular type/class.
if (student1 instanceof Student ){
System.out.println(“student1 is an instance of the Student class”);
} else {
System.out.println(“student1 is not an instance of the Student clas”);
}
Congratulations on completing section 4!
You are now a Java developer!