Download dan Belajar Software, Download, Script all Programming Dunia IT.
Saturday, May 31, 2025
[JAVA] IMAGE RESIZE SCALE
import javax.imageio.ImageIO; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ImageScaler { public static void main(String[] args) { try { File imageFile = new File("path/to/your/image.jpg"); BufferedImage image = ImageIO.read(imageFile); if (image != null) { int newWidth = image.getWidth() / 2; int newHeight = image.getHeight() / 2; BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); AffineTransform at = AffineTransform.getScaleInstance(0.5, 0.5); AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); scaledImage = scaleOp.filter(image, scaledImage); File output = new File("scaled_image.jpg"); ImageIO.write(scaledImage, "jpg", output); System.out.println("Image scaled and saved."); } else { System.out.println("Failed to load the image."); } } catch (IOException e) { e.printStackTrace(); } } }
[JAVA] IMAGE INFO RESOLUTION
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageLoader {
public static void main(String[] args) {
try {
File imageFile = new File("path/to/your/image.jpg");
BufferedImage image = ImageIO.read(imageFile);
if (image != null) {
int width = image.getWidth();
int height = image.getHeight();
System.out.println("Image dimensions: " + width + "x" + height);
} else {
System.out.println("Failed to load the image.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageLoader {
public static void main(String[] args) {
try {
File imageFile = new File("path/to/your/image.jpg");
BufferedImage image = ImageIO.read(imageFile);
if (image != null) {
int width = image.getWidth();
int height = image.getHeight();
System.out.println("Image dimensions: " + width + "x" + height);
} else {
System.out.println("Failed to load the image.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
[JAVA] PLAY VIDEO
import javax.sound.sampled.*;
import java.io.File;
public class AudioPlayer {
public static void main(String[] args) {
try {
File audioFile = new File("path/to/your/audio.wav"); // Replace with your file path
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
// Keep the program running so the audio can play
Thread.sleep(clip.getMicrosecondLength() / 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.File;
public class AudioPlayer {
public static void main(String[] args) {
try {
File audioFile = new File("path/to/your/audio.wav"); // Replace with your file path
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
// Keep the program running so the audio can play
Thread.sleep(clip.getMicrosecondLength() / 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
[JAVA] PLAY MUSIC
import javax.sound.sampled.*;
import java.io.File;
public class AudioPlayer {
public static void main(String[] args) {
try {
File audioFile = new File("path/to/your/audio.wav"); // Replace with your file path
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
// Keep the program running so the audio can play
Thread.sleep(clip.getMicrosecondLength() / 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.File;
public class AudioPlayer {
public static void main(String[] args) {
try {
File audioFile = new File("path/to/your/audio.wav"); // Replace with your file path
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
// Keep the program running so the audio can play
Thread.sleep(clip.getMicrosecondLength() / 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
[JAVA] MOVE OBJECT FROM MOUSE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MovableObject extends JPanel {
private int objectX = 50;
private int objectY = 50;
private int objectSize = 30;
public MovableObject() {
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
objectX = e.getX() - objectSize / 2;
objectY = e.getY() - objectSize / 2;
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(objectX, objectY, objectSize, objectSize);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Movable Object");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MovableObject());
frame.setSize(400, 300);
frame.setVisible(true);
}
}
[JAVA] MOVE OBJECT FROM KEYBOARD
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MoveObject extends JPanel implements KeyListener {
private int x = 50;
private int y = 50;
private final int step = 10;
public MoveObject() {
addKeyListener(this);
setFocusable(true);
requestFocusInWindow();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(x, y, 30, 30);
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_LEFT:
x -= step;
break;
case KeyEvent.VK_RIGHT:
x += step;
break;
case KeyEvent.VK_UP:
y -= step;
break;
case KeyEvent.VK_DOWN:
y += step;
break;
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Move Object");
MoveObject panel = new MoveObject();
frame.add(panel);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
[JAVA] USE COLOUR FROM MOUSE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class ColorClick extends JFrame {
private Color currentColor = Color.BLACK;
private Random random = new Random();
private JPanel panel;
public ColorClick() {
panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(currentColor);
g.fillRect(50, 50, 100, 100); // Example rectangle
}
};
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
currentColor = getRandomColor();
panel.repaint();
}
});
add(panel);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private Color getRandomColor() {
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
return new Color(red, green, blue);
}
public static void main(String[] args) {
new ColorClick();
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class ColorClick extends JFrame {
private Color currentColor = Color.BLACK;
private Random random = new Random();
private JPanel panel;
public ColorClick() {
panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(currentColor);
g.fillRect(50, 50, 100, 100); // Example rectangle
}
};
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
currentColor = getRandomColor();
panel.repaint();
}
});
add(panel);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private Color getRandomColor() {
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
return new Color(red, green, blue);
}
public static void main(String[] args) {
new ColorClick();
}
}
Subscribe to:
Posts (Atom)