Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.88 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.HashMap;
  5. import java.util.Scanner;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.swing.UIManager;
  9. public class Chegg264 extends javax.swing.JFrame {
  10.  private Color CURRENT_RGB = new Color(255, 255, 255);
  11.  private final Color DEFAULT_RGB = new Color(255, 255, 255);
  12.  private Color SAVED_RGB = new Color(255, 255, 255);
  13.  private final HashMap < String, Color > map;
  14.  public Chegg264() {
  15.   initComponents();
  16.   map = new HashMap < > ();
  17.   readFromFile();
  18.  }
  19.  private void readFromFile() {
  20.   try {
  21.    try (Scanner in = new Scanner(new File("colors.txt"))) {
  22.     while ( in .hasNext()) {
  23.      map.put( in .next(), new Color( in .nextInt(), in .nextInt(), in .nextInt()));
  24.     }
  25.    }
  26.   } catch (FileNotFoundException ex) {
  27.    Logger.getLogger(Chegg264.class.getName()).log(Level.SEVERE, null, ex);
  28.   }
  29.  }
  30.  
  31.  private void initComponents() {
  32.   panelSampleColor = new javax.swing.JPanel();
  33.   jScrollPane1 = new javax.swing.JScrollPane();
  34.   listColors = new javax.swing.JList < > ();
  35.   jPanel1 = new javax.swing.JPanel();
  36.   javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
  37.   tfRed = new javax.swing.JTextField();
  38.   btnRMinus = new javax.swing.JButton();
  39.   btnRPlus = new javax.swing.JButton();
  40.   javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
  41.   tfBlue = new javax.swing.JTextField();
  42.   btnBMinus = new javax.swing.JButton();
  43.   btnBPlus = new javax.swing.JButton();
  44.   javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
  45.   tfGreen = new javax.swing.JTextField();
  46.   btnGMinus = new javax.swing.JButton();
  47.   btnGPlus = new javax.swing.JButton();
  48.   btnSave = new javax.swing.JButton();
  49.   btnReset = new javax.swing.JButton();
  50.   setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  51.   setTitle("Color Sampler");
  52.   panelSampleColor.setBackground(DEFAULT_RGB);
  53.   javax.swing.GroupLayout panelSampleColorLayout = new javax.swing.GroupLayout(panelSampleColor);
  54.   panelSampleColor.setLayout(panelSampleColorLayout);
  55.   panelSampleColorLayout.setHorizontalGroup(panelSampleColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
  56.   panelSampleColorLayout.setVerticalGroup(panelSampleColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 140, Short.MAX_VALUE));
  57.   listColors.setModel(new javax.swing.AbstractListModel < String > () {
  58.    String[] strings = {
  59.     "Red",
  60.     "Green",
  61.     "Blue",
  62.     "Yellow",
  63.     "Cyan",
  64.     "Magenta",
  65.     "Orange",
  66.     "Pink",
  67.     "Gray",
  68.     "Black",
  69.     "White"
  70.    };
  71.    public int getSize() {
  72.     return strings.length;
  73.    }
  74.    public String getElementAt(int i) {
  75.     return strings[i];
  76.    }
  77.   });
  78.   listColors.addMouseListener(new java.awt.event.MouseAdapter() {
  79.    public void mouseClicked(java.awt.event.MouseEvent evt) {
  80.     setListColor(evt);
  81.    }
  82.   });
  83.   jScrollPane1.setViewportView(listColors);
  84.   jLabel1.setText("Red:");
  85.   tfRed.setText(DEFAULT_RGB.getRed() + "");
  86.   btnRMinus.setText("-");
  87.   btnRMinus.addActionListener(new java.awt.event.ActionListener() {
  88.    public void actionPerformed(java.awt.event.ActionEvent evt) {
  89.     redMinus(evt);
  90.    }
  91.   });
  92.   btnRPlus.setText("+");
  93.   btnRPlus.addActionListener(new java.awt.event.ActionListener() {
  94.    public void actionPerformed(java.awt.event.ActionEvent evt) {
  95.     redPlus(evt);
  96.    }
  97.   });
  98.   jLabel2.setText("Blue:");
  99.   tfBlue.setText(DEFAULT_RGB.getBlue() + "");
  100.   btnBMinus.setText("-");
  101.   btnBMinus.addActionListener(new java.awt.event.ActionListener() {
  102.    public void actionPerformed(java.awt.event.ActionEvent evt) {
  103.     blueMinus(evt);
  104.    }
  105.   });
  106.   btnBPlus.setText("+");
  107.   btnBPlus.addActionListener(new java.awt.event.ActionListener() {
  108.    public void actionPerformed(java.awt.event.ActionEvent evt) {
  109.     bluePlus(evt);
  110.    }
  111.   });
  112.   jLabel3.setText("Green:");
  113.   tfGreen.setText(DEFAULT_RGB.getGreen() + "");
  114.   btnGMinus.setText("-");
  115.   btnGMinus.addActionListener(new java.awt.event.ActionListener() {
  116.    public void actionPerformed(java.awt.event.ActionEvent evt) {
  117.     greenMinus(evt);
  118.    }
  119.   });
  120.   btnGPlus.setText("+");
  121.   btnGPlus.addActionListener(new java.awt.event.ActionListener() {
  122.    public void actionPerformed(java.awt.event.ActionEvent evt) {
  123.     greenPlus(evt);
  124.    }
  125.   });
  126.   btnSave.setText("Save");
  127.   btnSave.addActionListener(new java.awt.event.ActionListener() {
  128.    public void actionPerformed(java.awt.event.ActionEvent evt) {
  129.     colorSave(evt);
  130.    }
  131.   });
  132.   btnReset.setText("Reset");
  133.   btnReset.addActionListener(new java.awt.event.ActionListener() {
  134.    public void actionPerformed(java.awt.event.ActionEvent evt) {
  135.     colorReset(evt);
  136.    }
  137.   });
  138.   javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  139.   jPanel1.setLayout(jPanel1Layout);
  140.   jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGap(18, 18, 18)).addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel3).addGap(9, 9, 9))).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(tfGreen).addComponent(tfRed, javax.swing.GroupLayout.Alignment.LEADING).addComponent(tfBlue, javax.swing.GroupLayout.Alignment.LEADING)).addGap(18, 18, 18).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(btnRMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(btnBMinus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(btnGMinus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(btnBPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(btnRPlus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(btnGPlus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))).addGroup(jPanel1Layout.createSequentialGroup().addGap(42, 42, 42).addComponent(btnSave).addGap(27, 27, 27).addComponent(btnReset))).addContainerGap(25, Short.MAX_VALUE)));
  141.   jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(tfRed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(btnRMinus).addComponent(btnRPlus)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2).addComponent(tfBlue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(btnBMinus).addComponent(btnBPlus)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel3).addComponent(tfGreen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(btnGMinus).addComponent(btnGPlus)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(btnSave).addComponent(btnReset)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
  142.   javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  143.   getContentPane().setLayout(layout);
  144.   layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(panelSampleColor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGap(18, 18, 18).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
  145.   layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(panelSampleColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addComponent(jScrollPane1)).addContainerGap()));
  146.   pack();
  147.  }
  148.  private void redMinus(java.awt.event.ActionEvent evt) {
  149.   int redColor = Math.abs(Integer.parseInt(tfRed.getText()) - 1 + 256) % 256;
  150.   Color color = new Color(redColor, CURRENT_RGB.getGreen(), CURRENT_RGB.getBlue());
  151.   setColor(color);
  152.  }
  153.  private void blueMinus(java.awt.event.ActionEvent evt) {
  154.   int blueColor = Math.abs(Integer.parseInt(tfBlue.getText()) - 1 + 256) % 256;
  155.   Color color = new Color(CURRENT_RGB.getRed(), CURRENT_RGB.getGreen(), blueColor);
  156.   setColor(color);
  157.  }
  158.  private void greenMinus(java.awt.event.ActionEvent evt) {
  159.   int greenColor = Math.abs(Integer.parseInt(tfGreen.getText()) - 1 + 256) % 256;
  160.   Color color = new Color(CURRENT_RGB.getRed(), greenColor, CURRENT_RGB.getBlue());
  161.   setColor(color);
  162.  }
  163.  private void redPlus(java.awt.event.ActionEvent evt) {
  164.   int redColor = Math.abs(Integer.parseInt(tfRed.getText()) + 1) % 256;
  165.   Color color = new Color(redColor, CURRENT_RGB.getGreen(), CURRENT_RGB.getBlue());
  166.   setColor(color);
  167.  }
  168.  private void bluePlus(java.awt.event.ActionEvent evt) {
  169.   int blueColor = Math.abs(Integer.parseInt(tfBlue.getText()) + 1) % 256;
  170.   Color color = new Color(CURRENT_RGB.getRed(), CURRENT_RGB.getGreen(), blueColor);
  171.   setColor(color);
  172.  }
  173.  private void greenPlus(java.awt.event.ActionEvent evt) {
  174.   int greenColor = Math.abs(Integer.parseInt(tfGreen.getText()) + 1) % 256;
  175.   Color color = new Color(CURRENT_RGB.getRed(), greenColor, CURRENT_RGB.getBlue());
  176.   setColor(color);
  177.  }
  178.  private void colorReset(java.awt.event.ActionEvent evt) {
  179.   setColor(SAVED_RGB);
  180.   panelSampleColor.setBackground(SAVED_RGB);
  181.   String title = this.getTitle();
  182.   if (title.charAt(title.length() - 1) == '*') this.setTitle(title.substring(0, title.length() - 1));
  183.  }
  184.  private void colorSave(java.awt.event.ActionEvent evt) {
  185.   int blueColor = Integer.parseInt(tfBlue.getText());
  186.   int greenColor = Integer.parseInt(tfGreen.getText());
  187.   int redColor = Integer.parseInt(tfRed.getText());
  188.   SAVED_RGB = new Color(redColor, greenColor, blueColor);
  189.   CURRENT_RGB = SAVED_RGB;
  190.   colorReset(evt);
  191.  }
  192.  private void setListColor(java.awt.event.MouseEvent evt) {
  193.   int clickCount = evt.getClickCount();
  194.   if (clickCount == 2 || clickCount == 1) {
  195.    Color color = map.get(listColors.getSelectedValue());
  196.    tfRed.setText(color.getRed() + "");
  197.    tfBlue.setText(color.getBlue() + "");
  198.    tfGreen.setText(color.getGreen() + "");
  199.    setColor(color);
  200.   }
  201.  }
  202.  private void setColor(Color color) {
  203.   CURRENT_RGB = color;
  204.   System.out.println("CURRENT : " + CURRENT_RGB);
  205.   System.out.println("SAVED   : " + SAVED_RGB + "\n");
  206.   tfRed.setText(CURRENT_RGB.getRed() + "");
  207.   tfBlue.setText(CURRENT_RGB.getBlue() + "");
  208.   tfGreen.setText(CURRENT_RGB.getGreen() + "");
  209.   panelSampleColor.setBackground(CURRENT_RGB);
  210.   String title = this.getTitle();
  211.   if (title.charAt(title.length() - 1) != '*') this.setTitle(title + "*");
  212.  }
  213.  public static void main(String args[]) {
  214.   try {
  215.    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  216.   } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
  217.    java.util.logging.Logger.getLogger(Chegg264.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  218.   }
  219.   java.awt.EventQueue.invokeLater(() -> {
  220.    new Chegg264().setVisible(true);
  221.   });
  222.  }
  223.  private javax.swing.JButton btnBMinus;
  224.  private javax.swing.JButton btnBPlus;
  225.  private javax.swing.JButton btnGMinus;
  226.  private javax.swing.JButton btnGPlus;
  227.  private javax.swing.JButton btnRMinus;
  228.  private javax.swing.JButton btnRPlus;
  229.  private javax.swing.JButton btnReset;
  230.  private javax.swing.JButton btnSave;
  231.  private javax.swing.JPanel jPanel1;
  232.  private javax.swing.JScrollPane jScrollPane1;
  233.  private javax.swing.JList < String > listColors;
  234.  private javax.swing.JPanel panelSampleColor;
  235.  private javax.swing.JTextField tfBlue;
  236.  private javax.swing.JTextField tfGreen;
  237.  private javax.swing.JTextField tfRed;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement