Java 颜色计算器帮助

发布于 2024-09-26 10:16:24 字数 1493 浏览 2 评论 0原文

我想知道如何根据用户输入的十六进制值的颜色来绘制背景。我有这个:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SimpleColorCalc extends JFrame implements Runnable 
{
  ColorPanel cp;
  JButton show;
  JTextField hexCode;

  public SimpleColorCalc() 
  { 
    super("Simple Color Calculator"); 
    cp = new ColorPanel(); 
    show = new JButton("Show the Color"); 
    hexCode = new JTextField("ffffff");
    show.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) 
      { 
        String text = hexCode.getText(); 
        try 
        { 
          int colorCode = Integer.parseInt(text, 16); 
          Color enteredColor = new Color(colorCode); 
          cp.setColor(enteredColor); 
        } 
        catch(NumberFormatException ex) 
        { 
          hexCode.setText("ffffff"); 
          cp.setColor(Color.white); 
        } 
        finally 
        { 
          repaint(); 
        }
      }
    });
  } 
  public static void main(String[] args) 
  {
    SimpleColorCalc scc = new SimpleColorCalc(); 
    javax.swing.SwingUtilities.invokeLater(scc); 
  } 
  public void run() 
  { 
    setSize(400,300); 
    Container c = getContentPane();
    JPanel top = new JPanel(); 
    c.add(BorderLayout.NORTH, top); 
    top.setLayout(new GridLayout(1,2)); 
    top.add(show); 
    top.add(hexCode);
    setVisible(true); 
  }
}

但我想知道如何修复它,以便万一用户决定在十六进制代码前面放置 0x 或不它会起作用。我还想知道如何在java中将十六进制代码转换为颜色。我遇到了这个问题。

I want to know how to paint the background based on the color of the hex value inputed by the user. I have this:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SimpleColorCalc extends JFrame implements Runnable 
{
  ColorPanel cp;
  JButton show;
  JTextField hexCode;

  public SimpleColorCalc() 
  { 
    super("Simple Color Calculator"); 
    cp = new ColorPanel(); 
    show = new JButton("Show the Color"); 
    hexCode = new JTextField("ffffff");
    show.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) 
      { 
        String text = hexCode.getText(); 
        try 
        { 
          int colorCode = Integer.parseInt(text, 16); 
          Color enteredColor = new Color(colorCode); 
          cp.setColor(enteredColor); 
        } 
        catch(NumberFormatException ex) 
        { 
          hexCode.setText("ffffff"); 
          cp.setColor(Color.white); 
        } 
        finally 
        { 
          repaint(); 
        }
      }
    });
  } 
  public static void main(String[] args) 
  {
    SimpleColorCalc scc = new SimpleColorCalc(); 
    javax.swing.SwingUtilities.invokeLater(scc); 
  } 
  public void run() 
  { 
    setSize(400,300); 
    Container c = getContentPane();
    JPanel top = new JPanel(); 
    c.add(BorderLayout.NORTH, top); 
    top.setLayout(new GridLayout(1,2)); 
    top.add(show); 
    top.add(hexCode);
    setVisible(true); 
  }
}

But I want to know how to fix it such that incase the user decides to put a 0x in front of the hexadecimal code or not it will work. I also want to know how to convert the hex code into a color in java. I am having trouble with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金兰素衣 2024-10-03 10:16:24

这个 JUnit 测试可能会帮助您理解:

@Test
public void test1() {
    Integer hexInt = Integer.parseInt("FF0000", 16);
    Color createdColor = new Color(hexInt);
    assertEquals(Color.RED, createdColor);
}

您可以使用 Integer.parseInt 将十六进制字符串转换为以 16 为基数的数字。请注意,如果字符串无效(包含数字以外的字符),这将引发异常或 af)。

然后可以使用Integer 创建Color 实例。

我添加了一个断言来表明所创建的就是我们期望的那样。

This JUnit test may help you understand:

@Test
public void test1() {
    Integer hexInt = Integer.parseInt("FF0000", 16);
    Color createdColor = new Color(hexInt);
    assertEquals(Color.RED, createdColor);
}

You can use Integer.parseInt to turn the hexadecimal string into a number of base 16. Note this will throw an exception if the string is invalid (contains characters other than digits or a-f).

Color instances can then be created using the Integer.

I have included an assertion to show that the created is what we expect it to be.

一生独一 2024-10-03 10:16:24

好吧,将十六进制字符串转换为颜色:

Color myColor = Color.decode("0xFF0000");

或者您可以这样做:

Color myColor2 = new Color(0xFF0000);

但是输入不能是字符串,否则您将得到 NumberFormatException。

判断输入是否以 0x 开头应该很容易

Well, converting a hex-string into a Color:

Color myColor = Color.decode("0xFF0000");

Or you could do:

Color myColor2 = new Color(0xFF0000);

But then the input can't be a String, or else you'll get a NumberFormatException.

Figuring out if the input starts with an 0x should be easy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文