Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Hello World Programs/Java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Getting Started

Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.

## Folder Structure

The workspace contains two folders by default, where:

- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies

## Dependency Management

The `JAVA DEPENDENCIES` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-pack/blob/master/release-notes/v0.9.0.md#work-with-jar-files-directly).

## This 2 program is to demonstrate hello world but in GUI form
43 changes: 43 additions & 0 deletions Hello World Programs/Java/src/HelloWorld/HelloWorldGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package HelloWorld;

import java.awt.Image;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class HelloWorldGUI{

public static void main(final String[] args) {
new HelloWorldGUI();
}

private HelloWorldGUI() {
this.displayImage();
}

private ImageIcon resizeIcon(final String path, final int resizedWidth, final int resizedHeight) {
final Image img = new ImageIcon(new File(path).getAbsolutePath()).getImage();
final Image resizedImage = img.getScaledInstance(resizedWidth, resizedHeight, java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(resizedImage);
}

private void displayImage() {

final JFrame jf = new JFrame("Hello World");

final JLabel label = new JLabel("Hello World", this.resizeIcon("src/globe.png", 100, 100), SwingConstants.CENTER);
label.setVisible(true);

label.setSize(120, 40);
jf.add(label);

jf.setSize(250, 140);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
70 changes: 70 additions & 0 deletions Hello World Programs/Java/src/HelloWorld/HelloWorldGUI2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package HelloWorld;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serial;

import javax.imageio.ImageIO;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class HelloWorldGUI2 extends JPanel{

@Serial
private static final long serialVersionUID = 1L;
private final BufferedImage image;


public static void main(final String[] args) {
try {
new HelloWorldGUI2();
} catch (final IOException e) {
e.printStackTrace();
}
}

private HelloWorldGUI2() throws IOException {
final String absolutePath = new File("src/globe.png").getAbsolutePath();
this.image = resizeBufferedImage(ImageIO.read(new File(absolutePath)), 100, 100);
this.displayImage();
}

private BufferedImage resizeBufferedImage(final BufferedImage img, final int newW, final int newH) {
final Image image = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
final BufferedImage bfImage = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = bfImage.createGraphics();
g2d.drawImage(image, 0, 0, this);
g2d.dispose();
return bfImage;
}

private void displayImage() {

final JFrame jf = new JFrame("Hello World");

final JLabel label = new JLabel("Hello World");
label.setVisible(true);

label.setSize(120, 40);
jf.add(label);

jf.setSize(250, 140);
jf.getContentPane().add(this);
jf.setLocationRelativeTo(this);
jf.setVisible(true);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
g.drawImage(this.image, 0, 0, this);
}
}
Binary file added Hello World Programs/Java/src/globe.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.