TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
I have problems with running this code to get the output please help.docx
1. I have problems with running this code to get the output please help
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Program {
public static void main(String[] args) {
JFrame frame = new JFrame("Data Entry");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
// create the timer panel
JPanel timerPanel = new JPanel();
timerPanel.setBackground(Color.WHITE);
JLabel timerLabel = new JLabel("Timer: 00:00", SwingConstants.CENTER);
timerLabel.setFont(new Font("Courier", Font.PLAIN, 20));
Timer timer = new Timer(1000, e -> {
int time = Integer.parseInt(timerLabel.getText().substring(7)) + 1;
timerLabel.setText("Timer: " + String.format("%02d", time / 60) + ":" + String.format("%02d",
time % 60));
});
timer.start();
timerPanel.add(timerLabel);
// create the name panel
JPanel namePanel = new JPanel();
2. namePanel.setBackground(Color.WHITE);
JTextField nameField = new JTextField(20);
nameField.setFont(new Font("Courier", Font.PLAIN, 20));
nameField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
nameField.setForeground(Color.BLUE);
}
@Override
public void focusLost(FocusEvent e) {}
});
JButton submitButton = new JButton("SUBMIT");
submitButton.setFont(new Font("Courier", Font.PLAIN, 20));
JTextArea textArea = new JTextArea(10, 40);
textArea.setFont(new Font("Courier", Font.PLAIN, 20));
textArea.setEditable(false);
textArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
submitButton.addActionListener(e -> {
String name = nameField.getText();
if (!name.equals("")) {
textArea.append(name + "n");
nameField.setText("");
}
});
namePanel.add(nameField);
namePanel.add(submitButton);
// create the font size panel
JPanel fontSizePanel = new JPanel();
fontSizePanel.setBackground(Color.WHITE);
JSlider fontSizeSlider = new JSlider(JSlider.HORIZONTAL, 10, 40, 20);
fontSizeSlider.setMajorTickSpacing(10);
fontSizeSlider.setMinorTickSpacing(5);
fontSizeSlider.setPaintTicks(true);
fontSizeSlider.setPaintLabels(true);
fontSizeSlider.setFont(new Font("Courier", Font.PLAIN, 20));
fontSizeSlider.addChangeListener(e -> {
int fontSize = fontSizeSlider.getValue();
Font font = new Font("Courier", Font.PLAIN, fontSize);
textArea.setFont(font);
});
fontSizePanel.add(fontSizeSlider);
// create the animation panel
JPanel animationPanel = new JPanel();
animationPanel.setBackground(Color.WHITE);
JLabel imageLabel = new JLabel();
ImageIcon horseIcon = new ImageIcon("horse.jpg");
3. ImageIcon birdIcon = new ImageIcon("bird.jpg");
ImageIcon aeroplanIcon = new ImageIcon("aeroplan.jpg");
imageLabel.setIcon(horseIcon);
JRadioButton horseButton = new JRadioButton("Horse", true);
horseButton.setFont(new Font("Courier", Font.PLAIN, 20));
JRadioButton birdButton = new JRadioButton("Bird"); birdButton.setFont(new Font("Courier",
Font.PLAIN, 20));
JRadioButton aeroplanButton = new JRadioButton("Aeroplan");
aeroplanButton.setFont(new Font("Courier", Font.PLAIN, 20));
horseButton.addActionListener(e -> imageLabel.setIcon(horseIcon));
birdButton.addActionListener(e -> imageLabel.setIcon(birdIcon));
aeroplanButton.addActionListener(e -> imageLabel.setIcon(aeroplanIcon));
animationPanel.add(horseButton);
animationPanel.add(birdButton);
animationPanel.add(aeroplanButton);
animationPanel.add(imageLabel);
// create the start and stop buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
JButton startButton = new JButton("Start");
startButton.setFont(new Font("Courier", Font.PLAIN, 20));
JButton stopButton = new JButton("Stop");
stopButton.setFont(new Font("Courier", Font.PLAIN, 20));
startButton.addActionListener(e -> {
Thread animationThread = new Thread(() -> {
while (true) {
ImageIcon currentIcon = (ImageIcon) imageLabel.getIcon();
if (currentIcon.equals(horseIcon)) {
imageLabel.setIcon(birdIcon);
} else if (currentIcon.equals(birdIcon)) {
imageLabel.setIcon(aeroplanIcon);
} else {
imageLabel.setIcon(horseIcon);
}
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
break;
}
}
});
animationThread.start();
});
stopButton.addActionListener(e -> {
for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (thread.getName().startsWith("Thread-")) {
4. thread.interrupt();
}
}
});
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
// create the main panel
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(timerPanel, BorderLayout.NORTH);
mainPanel.add(namePanel, BorderLayout.CENTER);
mainPanel.add(fontSizePanel, BorderLayout.SOUTH);
// add the sub-panels to the main panel
mainPanel.add(animationPanel, BorderLayout.EAST);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
// add the main panel to the frame and show the frame
frame.add(mainPanel);
frame.setVisible(true);
// add a window listener to the frame to show a message when the program exits
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
timer.stop();
String time = timerLabel.getText().substring(7);
JOptionPane.showMessageDialog(frame, "Thank you for using my program for " + time + "
time.");
}
});
}
}