Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.HashMap;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.UIManager;
- public class Chegg264 extends javax.swing.JFrame {
- private Color CURRENT_RGB = new Color(255, 255, 255);
- private final Color DEFAULT_RGB = new Color(255, 255, 255);
- private Color SAVED_RGB = new Color(255, 255, 255);
- private final HashMap < String, Color > map;
- public Chegg264() {
- initComponents();
- map = new HashMap < > ();
- readFromFile();
- }
- private void readFromFile() {
- try {
- try (Scanner in = new Scanner(new File("colors.txt"))) {
- while ( in .hasNext()) {
- map.put( in .next(), new Color( in .nextInt(), in .nextInt(), in .nextInt()));
- }
- }
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Chegg264.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- private void initComponents() {
- panelSampleColor = new javax.swing.JPanel();
- jScrollPane1 = new javax.swing.JScrollPane();
- listColors = new javax.swing.JList < > ();
- jPanel1 = new javax.swing.JPanel();
- javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
- tfRed = new javax.swing.JTextField();
- btnRMinus = new javax.swing.JButton();
- btnRPlus = new javax.swing.JButton();
- javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
- tfBlue = new javax.swing.JTextField();
- btnBMinus = new javax.swing.JButton();
- btnBPlus = new javax.swing.JButton();
- javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
- tfGreen = new javax.swing.JTextField();
- btnGMinus = new javax.swing.JButton();
- btnGPlus = new javax.swing.JButton();
- btnSave = new javax.swing.JButton();
- btnReset = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Color Sampler");
- panelSampleColor.setBackground(DEFAULT_RGB);
- javax.swing.GroupLayout panelSampleColorLayout = new javax.swing.GroupLayout(panelSampleColor);
- panelSampleColor.setLayout(panelSampleColorLayout);
- panelSampleColorLayout.setHorizontalGroup(panelSampleColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
- panelSampleColorLayout.setVerticalGroup(panelSampleColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 140, Short.MAX_VALUE));
- listColors.setModel(new javax.swing.AbstractListModel < String > () {
- String[] strings = {
- "Red",
- "Green",
- "Blue",
- "Yellow",
- "Cyan",
- "Magenta",
- "Orange",
- "Pink",
- "Gray",
- "Black",
- "White"
- };
- public int getSize() {
- return strings.length;
- }
- public String getElementAt(int i) {
- return strings[i];
- }
- });
- listColors.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- setListColor(evt);
- }
- });
- jScrollPane1.setViewportView(listColors);
- jLabel1.setText("Red:");
- tfRed.setText(DEFAULT_RGB.getRed() + "");
- btnRMinus.setText("-");
- btnRMinus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- redMinus(evt);
- }
- });
- btnRPlus.setText("+");
- btnRPlus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- redPlus(evt);
- }
- });
- jLabel2.setText("Blue:");
- tfBlue.setText(DEFAULT_RGB.getBlue() + "");
- btnBMinus.setText("-");
- btnBMinus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- blueMinus(evt);
- }
- });
- btnBPlus.setText("+");
- btnBPlus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bluePlus(evt);
- }
- });
- jLabel3.setText("Green:");
- tfGreen.setText(DEFAULT_RGB.getGreen() + "");
- btnGMinus.setText("-");
- btnGMinus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- greenMinus(evt);
- }
- });
- btnGPlus.setText("+");
- btnGPlus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- greenPlus(evt);
- }
- });
- btnSave.setText("Save");
- btnSave.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- colorSave(evt);
- }
- });
- btnReset.setText("Reset");
- btnReset.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- colorReset(evt);
- }
- });
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- 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)));
- 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)));
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- 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)));
- 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()));
- pack();
- }
- private void redMinus(java.awt.event.ActionEvent evt) {
- int redColor = Math.abs(Integer.parseInt(tfRed.getText()) - 1 + 256) % 256;
- Color color = new Color(redColor, CURRENT_RGB.getGreen(), CURRENT_RGB.getBlue());
- setColor(color);
- }
- private void blueMinus(java.awt.event.ActionEvent evt) {
- int blueColor = Math.abs(Integer.parseInt(tfBlue.getText()) - 1 + 256) % 256;
- Color color = new Color(CURRENT_RGB.getRed(), CURRENT_RGB.getGreen(), blueColor);
- setColor(color);
- }
- private void greenMinus(java.awt.event.ActionEvent evt) {
- int greenColor = Math.abs(Integer.parseInt(tfGreen.getText()) - 1 + 256) % 256;
- Color color = new Color(CURRENT_RGB.getRed(), greenColor, CURRENT_RGB.getBlue());
- setColor(color);
- }
- private void redPlus(java.awt.event.ActionEvent evt) {
- int redColor = Math.abs(Integer.parseInt(tfRed.getText()) + 1) % 256;
- Color color = new Color(redColor, CURRENT_RGB.getGreen(), CURRENT_RGB.getBlue());
- setColor(color);
- }
- private void bluePlus(java.awt.event.ActionEvent evt) {
- int blueColor = Math.abs(Integer.parseInt(tfBlue.getText()) + 1) % 256;
- Color color = new Color(CURRENT_RGB.getRed(), CURRENT_RGB.getGreen(), blueColor);
- setColor(color);
- }
- private void greenPlus(java.awt.event.ActionEvent evt) {
- int greenColor = Math.abs(Integer.parseInt(tfGreen.getText()) + 1) % 256;
- Color color = new Color(CURRENT_RGB.getRed(), greenColor, CURRENT_RGB.getBlue());
- setColor(color);
- }
- private void colorReset(java.awt.event.ActionEvent evt) {
- setColor(SAVED_RGB);
- panelSampleColor.setBackground(SAVED_RGB);
- String title = this.getTitle();
- if (title.charAt(title.length() - 1) == '*') this.setTitle(title.substring(0, title.length() - 1));
- }
- private void colorSave(java.awt.event.ActionEvent evt) {
- int blueColor = Integer.parseInt(tfBlue.getText());
- int greenColor = Integer.parseInt(tfGreen.getText());
- int redColor = Integer.parseInt(tfRed.getText());
- SAVED_RGB = new Color(redColor, greenColor, blueColor);
- CURRENT_RGB = SAVED_RGB;
- colorReset(evt);
- }
- private void setListColor(java.awt.event.MouseEvent evt) {
- int clickCount = evt.getClickCount();
- if (clickCount == 2 || clickCount == 1) {
- Color color = map.get(listColors.getSelectedValue());
- tfRed.setText(color.getRed() + "");
- tfBlue.setText(color.getBlue() + "");
- tfGreen.setText(color.getGreen() + "");
- setColor(color);
- }
- }
- private void setColor(Color color) {
- CURRENT_RGB = color;
- System.out.println("CURRENT : " + CURRENT_RGB);
- System.out.println("SAVED : " + SAVED_RGB + "\n");
- tfRed.setText(CURRENT_RGB.getRed() + "");
- tfBlue.setText(CURRENT_RGB.getBlue() + "");
- tfGreen.setText(CURRENT_RGB.getGreen() + "");
- panelSampleColor.setBackground(CURRENT_RGB);
- String title = this.getTitle();
- if (title.charAt(title.length() - 1) != '*') this.setTitle(title + "*");
- }
- public static void main(String args[]) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Chegg264.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- java.awt.EventQueue.invokeLater(() -> {
- new Chegg264().setVisible(true);
- });
- }
- private javax.swing.JButton btnBMinus;
- private javax.swing.JButton btnBPlus;
- private javax.swing.JButton btnGMinus;
- private javax.swing.JButton btnGPlus;
- private javax.swing.JButton btnRMinus;
- private javax.swing.JButton btnRPlus;
- private javax.swing.JButton btnReset;
- private javax.swing.JButton btnSave;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JList < String > listColors;
- private javax.swing.JPanel panelSampleColor;
- private javax.swing.JTextField tfBlue;
- private javax.swing.JTextField tfGreen;
- private javax.swing.JTextField tfRed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement