Download dan Belajar Software, Download, Script all Programming Dunia IT.
Saturday, May 31, 2025
[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(); } } }
[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();
}
}
[JAVA] LINE DRAG MOUSE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
public class DrawLineDrag extends JPanel {
private List<Line2D> lines = new ArrayList<>();
private Point prevPoint;
public DrawLineDrag() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
prevPoint = e.getPoint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (prevPoint != null) {
Point currentPoint = e.getPoint();
lines.add(new Line2D.Double(prevPoint, currentPoint));
prevPoint = currentPoint;
repaint();
}
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK); // Set line color
for (Line2D line : lines) {
g2d.draw(line);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Lines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawLineDrag());
frame.setSize(400, 300);
frame.setVisible(true);
}
}
Subscribe to:
Posts (Atom)