Code Snippets
Get name of current method in Java
Get name of current method in Java
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
Convert String to Date in Java
Convert String to Date in Java
java.util.Date = java.text.DateFormat.getDateInstance().parse(date String);
Capture screen shots in Java
Capture screen shots in Java
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
...
public void captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(fileName));
}
POKE'BALL
POKE'BALL
public class Program
{
public static void main(String[] args) {
System.out.println(" @@@@@@@@@@ ");
System.out.println(" @@@@@@@@@@@@ ");
System.out.println(" @@@@@@@@@@@@@@ ");
System.out.println(" @@@@@@@@@@@@@@@@ ");
System.out.println(" |||||[\"\"\"\"]|||||");
System.out.println(" |||||[....]|||||");
System.out.println(" OOOOOOOOOOOOOOOO");
System.out.println(" OOOOOOOOOOOOOO");
System.out.println(" OOOOOOOOOOOO");
System.out.println(" OOOOOOOOOO");
}
}
Build for Scanner
Build for Scanner
import java.util.Scanner;
public class bucky1 {
public static void main(String[] args) {
Scanner bucky = new Scanner(System.in);
/*
To use scanner, I would use code like this
Scanner bucky = new Scanner(System.in);
int i = bucky.nextInt();
To retrieve a username I would probably use:
bucky.nextLine()
.
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("Your username is " + username);
*/
//int can be replaced with other Types like doubles etc.
Simple GUI Login Interface
Simple GUI Login Interface
package localGroup;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class UrsaMinorDwarf {
private JFrame frmLoginWindowAndromeda;
private JTextField usernameField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UrsaMinorDwarf window = new UrsaMinorDwarf();
window.frmLoginWindowAndromeda.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UrsaMinorDwarf() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmLoginWindowAndromeda = new JFrame();
frmLoginWindowAndromeda.setTitle("Login Window Andromeda");
frmLoginWindowAndromeda.setForeground(Color.LIGHT_GRAY);
frmLoginWindowAndromeda.setBackground(Color.RED);
frmLoginWindowAndromeda.setBounds(100, 100, 450, 300);
frmLoginWindowAndromeda.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmLoginWindowAndromeda.getContentPane().setLayout(null);
JLabel lblUsername = new JLabel("UserName");
lblUsername.setBounds(35, 66, 140, 35);
frmLoginWindowAndromeda.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(35, 113, 140, 35);
frmLoginWindowAndromeda.getContentPane().add(lblPassword);
usernameField = new JTextField();
usernameField.setBounds(113, 73, 86, 20);
frmLoginWindowAndromeda.getContentPane().add(usernameField);
usernameField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(113, 120, 86, 20);
frmLoginWindowAndromeda.getContentPane().add(passwordField);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
private Component frame;
public void actionPerformed(ActionEvent args0) {
String uname = usernameField.getText();
@SuppressWarnings("deprecation")
String pswd = passwordField.getText();
if (uname.equals("Helios") && pswd.equals("Sun")) {
JOptionPane.showMessageDialog(frame, "You are a man of the knights watch! May the gods be with you");
}else {
JOptionPane.showMessageDialog(frame, "Gandalf: YoU sHalL nOt PaSs!!!!!");
}
}
});
btnLogin.setBounds(262, 227, 89, 23);
frmLoginWindowAndromeda.getContentPane().add(btnLogin);
}
}
Build for Java Ascii art (Less tediousness)
Build for Java Ascii art (Less tediousness)
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class ASCIIArtService {
public static void main(String[] args) throws IOException {
int width = 100;
int height = 30;
//BufferedImage image = ImageIO.read(new File("/Users/Lordjulius/Desktop/logo.jpg"));
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setFont(new Font("SansSerif", Font.BOLD, 24));
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.drawString("Bleep Bloop", 10, 20);
//save this image
//ImageIO.write(image, "png", new File("/users/Lordjulius/ascii-art.png"));
for (int y = 0; y < height; y++) {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < width; x++) {
sb.append(image.getRGB(x, y) == -16777216 ? " " : "$");
}
if (sb.toString().trim().isEmpty()) {
continue;
}
System.out.println(sb);
}
}
}
Last updated: November 15, 2017