ferdysetia_aan

TP 3 No.3 - Java GUI ChangeTextColor - Kelompok 1

Jan 10th, 2024 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.08 KB | None | 0 0
  1. // ChangeTextColor.java
  2.  
  3. package form;
  4.  
  5. import java.awt.Color;
  6. import javax.swing.JFrame;
  7.  
  8. public class ChangeTextColor extends javax.swing.JFrame {
  9.     private int currentPosition = 0;
  10.     private final String text = "Programming Is Fun";
  11.     private final int maxLeftOffset = 0;
  12.     private final int maxRightOffset = 200;
  13.  
  14.     public ChangeTextColor() {
  15.         initComponents();
  16.         updateText();
  17.        
  18.         setExtendedState(JFrame.MAXIMIZED_HORIZ);
  19.         setVisible(true);
  20.         setResizable(false);
  21.     }
  22.  
  23.     private void updateText() {
  24.         teks.setText(text);
  25.         teks.setForeground(getSelectedColor());
  26.  
  27.         int labelWidth = teks.getPreferredSize().width;
  28.         int panelWidth = panelTeks.getWidth();
  29.    
  30.         if (currentPosition < maxLeftOffset) {
  31.         currentPosition = maxLeftOffset;
  32.         } else if (currentPosition + labelWidth > panelWidth - maxLeftOffset) {
  33.         currentPosition = panelWidth - labelWidth - maxLeftOffset;
  34.         }
  35.         teks.setBounds(currentPosition, 46, labelWidth, 20);
  36.     }
  37.  
  38.     private Color getSelectedColor() {
  39.         if (red.isSelected()) {
  40.             return Color.RED;
  41.         } else if (yellow.isSelected()) {
  42.             return Color.YELLOW;
  43.         } else if (black.isSelected()) {
  44.             return Color.BLACK;
  45.         } else if (orange.isSelected()) {
  46.             return Color.ORANGE;
  47.         } else if (green.isSelected()) {
  48.             return Color.GREEN;
  49.         } else {
  50.             return Color.BLACK; // Default color
  51.         }
  52.     }
  53.  
  54.     @SuppressWarnings("unchecked")
  55.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  56.     private void initComponents() {
  57.  
  58.         colors = new javax.swing.ButtonGroup();
  59.         jPanel1 = new javax.swing.JPanel();
  60.         panelTeks = new javax.swing.JPanel();
  61.         teks = new javax.swing.JLabel();
  62.         red = new javax.swing.JRadioButton();
  63.         yellow = new javax.swing.JRadioButton();
  64.         black = new javax.swing.JRadioButton();
  65.         orange = new javax.swing.JRadioButton();
  66.         green = new javax.swing.JRadioButton();
  67.         left = new javax.swing.JButton();
  68.         right = new javax.swing.JButton();
  69.  
  70.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  71.  
  72.         jPanel1.setBackground(new java.awt.Color(255, 255, 255));
  73.  
  74.         panelTeks.setBackground(new java.awt.Color(255, 255, 255));
  75.         panelTeks.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  76.  
  77.         teks.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
  78.         teks.setText("Programming Is Fun");
  79.  
  80.         javax.swing.GroupLayout panelTeksLayout = new javax.swing.GroupLayout(panelTeks);
  81.         panelTeks.setLayout(panelTeksLayout);
  82.         panelTeksLayout.setHorizontalGroup(
  83.             panelTeksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  84.             .addGroup(panelTeksLayout.createSequentialGroup()
  85.                 .addGap(88, 88, 88)
  86.                 .addComponent(teks)
  87.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  88.         );
  89.         panelTeksLayout.setVerticalGroup(
  90.             panelTeksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  91.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTeksLayout.createSequentialGroup()
  92.                 .addContainerGap(46, Short.MAX_VALUE)
  93.                 .addComponent(teks)
  94.                 .addGap(32, 32, 32))
  95.         );
  96.  
  97.         colors.add(red);
  98.         red.setText("Red");
  99.         red.addActionListener(new java.awt.event.ActionListener() {
  100.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  101.                 redActionPerformed(evt);
  102.             }
  103.         });
  104.  
  105.         colors.add(yellow);
  106.         yellow.setText("Yellow");
  107.         yellow.addActionListener(new java.awt.event.ActionListener() {
  108.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  109.                 yellowActionPerformed(evt);
  110.             }
  111.         });
  112.  
  113.         colors.add(black);
  114.         black.setText("Black");
  115.         black.addActionListener(new java.awt.event.ActionListener() {
  116.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  117.                 blackActionPerformed(evt);
  118.             }
  119.         });
  120.  
  121.         colors.add(orange);
  122.         orange.setText("Orange");
  123.         orange.addActionListener(new java.awt.event.ActionListener() {
  124.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  125.                 orangeActionPerformed(evt);
  126.             }
  127.         });
  128.  
  129.         colors.add(green);
  130.         green.setText("Green");
  131.         green.addActionListener(new java.awt.event.ActionListener() {
  132.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  133.                 greenActionPerformed(evt);
  134.             }
  135.         });
  136.  
  137.         left.setText("<=");
  138.         left.addActionListener(new java.awt.event.ActionListener() {
  139.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  140.                 leftActionPerformed(evt);
  141.             }
  142.         });
  143.  
  144.         right.setText("=>");
  145.         right.addActionListener(new java.awt.event.ActionListener() {
  146.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  147.                 rightActionPerformed(evt);
  148.             }
  149.         });
  150.  
  151.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  152.         jPanel1.setLayout(jPanel1Layout);
  153.         jPanel1Layout.setHorizontalGroup(
  154.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  155.             .addGroup(jPanel1Layout.createSequentialGroup()
  156.                 .addGap(7, 7, 7)
  157.                 .addComponent(red)
  158.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  159.                 .addComponent(yellow)
  160.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  161.                 .addComponent(black)
  162.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  163.                 .addComponent(orange)
  164.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  165.                 .addComponent(green)
  166.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  167.             .addComponent(panelTeks, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  168.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  169.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  170.                 .addComponent(left, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  171.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  172.                 .addComponent(right, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  173.                 .addGap(97, 97, 97))
  174.         );
  175.         jPanel1Layout.setVerticalGroup(
  176.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  177.             .addGroup(jPanel1Layout.createSequentialGroup()
  178.                 .addContainerGap()
  179.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  180.                     .addComponent(black, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
  181.                     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  182.                         .addComponent(red)
  183.                         .addComponent(yellow)
  184.                         .addComponent(orange)
  185.                         .addComponent(green)))
  186.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  187.                 .addComponent(panelTeks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  188.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  189.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  190.                     .addComponent(right)
  191.                     .addComponent(left))
  192.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  193.         );
  194.  
  195.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  196.         getContentPane().setLayout(layout);
  197.         layout.setHorizontalGroup(
  198.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  199.             .addGroup(layout.createSequentialGroup()
  200.                 .addContainerGap()
  201.                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  202.                 .addContainerGap())
  203.         );
  204.         layout.setVerticalGroup(
  205.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  206.             .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  207.         );
  208.  
  209.         pack();
  210.     }// </editor-fold>                        
  211.  
  212.     private void yellowActionPerformed(java.awt.event.ActionEvent evt) {                                      
  213.         updateText();
  214.     }                                      
  215.  
  216.     private void redActionPerformed(java.awt.event.ActionEvent evt) {                                    
  217.         updateText();
  218.     }                                  
  219.  
  220.     private void blackActionPerformed(java.awt.event.ActionEvent evt) {                                      
  221.         updateText();
  222.     }                                    
  223.  
  224.     private void orangeActionPerformed(java.awt.event.ActionEvent evt) {                                      
  225.         updateText();
  226.     }                                      
  227.  
  228.     private void greenActionPerformed(java.awt.event.ActionEvent evt) {                                      
  229.         updateText();
  230.     }                                    
  231.  
  232.     private void leftActionPerformed(java.awt.event.ActionEvent evt) {                                    
  233.         if (currentPosition > maxLeftOffset) {
  234.             currentPosition -= 10; // Adjust this value for the desired movement
  235.             updateText();
  236.         }
  237.     }                                    
  238.  
  239.     private void rightActionPerformed(java.awt.event.ActionEvent evt) {                                      
  240.         if (currentPosition < maxRightOffset) {
  241.             currentPosition += 10; // Adjust this value for the desired movement
  242.             updateText();
  243.         }
  244.     }                                    
  245.  
  246.     public static void main(String args[]) {
  247.         /* Set the Nimbus look and feel */
  248.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  249.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  250.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  251.          */
  252.         try {
  253.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  254.                 if ("Nimbus".equals(info.getName())) {
  255.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  256.                     break;
  257.                 }
  258.             }
  259.         } catch (ClassNotFoundException ex) {
  260.             java.util.logging.Logger.getLogger(ChangeTextColor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  261.         } catch (InstantiationException ex) {
  262.             java.util.logging.Logger.getLogger(ChangeTextColor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  263.         } catch (IllegalAccessException ex) {
  264.             java.util.logging.Logger.getLogger(ChangeTextColor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  265.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  266.             java.util.logging.Logger.getLogger(ChangeTextColor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  267.         }
  268.         //</editor-fold>
  269.         //</editor-fold>
  270.  
  271.         /* Create and display the form */
  272.         java.awt.EventQueue.invokeLater(new Runnable() {
  273.             public void run() {
  274.                 new ChangeTextColor().setVisible(true);
  275.             }
  276.         });
  277.     }
  278.  
  279.     // Variables declaration - do not modify                    
  280.     private javax.swing.JRadioButton black;
  281.     private javax.swing.ButtonGroup colors;
  282.     private javax.swing.JRadioButton green;
  283.     private javax.swing.JPanel jPanel1;
  284.     private javax.swing.JButton left;
  285.     private javax.swing.JRadioButton orange;
  286.     private javax.swing.JPanel panelTeks;
  287.     private javax.swing.JRadioButton red;
  288.     private javax.swing.JButton right;
  289.     private javax.swing.JLabel teks;
  290.     private javax.swing.JRadioButton yellow;
  291.     // End of variables declaration                  
  292. }
  293.  
Advertisement
Add Comment
Please, Sign In to add comment