Topic: 怎么取鼠标点击处的颜色?

  Print this page

1.怎么取鼠标点击处的颜色? Copy to clipboard
Posted by: huwd
Posted on: 2005-05-19 13:27

一个按钮,设定某种颜色
点击它,触发一个事件,怎么取出按钮处的颜色
谢谢

2.Re:怎么取鼠标点击处的颜色? [Re: huwd] Copy to clipboard
Posted by: digital2003
Posted on: 2005-06-01 00:52

import java.awt.*;
import java.awt.event.*

class qs extends Frame implements ActionListener
{
TextField t1=new TextField();
Button b1=new Button("红色");

qs()
{
super("简单的取色测试");
t1.setText("请取色");
b1.addactionListener(this);
this.add(t1,BorderLayout.NORTH);
this.add(b1,BorderLayout.CENTER);
this.setSize(200,40);
this.setVisual(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand=="红色")
t1.setText("红色");
}
public static void main(String[] args)
{
qs q1=new qs();
}

}

3.Re:怎么取鼠标点击处的颜色? [Re: digital2003] Copy to clipboard
Posted by: huwd
Posted on: 2005-06-01 08:58

还是不太对啊,可能我没说清楚
我要将按钮表面全部涂成一种颜色,可能会多做好几个按钮
然后我点击其中一个按钮,就将系统颜色设置为按钮表面的颜色,然后画线段都按照这种颜色
我知道可以用多个if来判断,但这会因按钮数量的增加而增加if语句
有没有变通的方法,就是如我上述说的,无论按哪个颜色的按钮,都将系统颜色设置为按钮表面的颜色,然后画线段都按照这种颜色

4.Re:怎么取鼠标点击处的颜色? [Re: huwd] Copy to clipboard
Posted by: digital2003
Posted on: 2005-06-01 23:06

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

class quse extends Frame implements ActionListener, MouseListener
{
  
  Button red=new Button("Red");
  Button green=new Button("Green");
  Button black=new Button("Black");
  Panel p1=new Panel();
  Panel p2=new Panel();
  Color c1=new Color(10,10,10);
  Color c2=new Color(255,255,255);
  Color c3=new Color(0,255,64);
  Graphics g;
  int xz;
  int x,y; //用来保存用户按下鼠标开始坐标
  boolean draw=false; //判断用户是否已经选择了要画图
  quse()
  {
    xz=x=y=0;//初始化参数
    
    this.add(p1,BorderLayout.NORTH);
    this.add(p2,BorderLayout.CENTER);
    p1.setSize(400,10);
    p1.setLayout(new GridLayout(1,3,2,1));
    p1.add(red);
    p1.add(green);
    p1.add(black);
    
    red.addActionListener(this);
    green.addActionListener(this);
    black.addActionListener(this);
    p2.addMouseListener(this);
    p2.setSize(400,390);
    p2.setBackground(c2);
    this.setBackground(c1);
    this.setResizable(false);
    this.setSize(400,400);
    double w=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
    double h=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
    this.setLocation((int)(w-400)/2,(int)(h-400)/2);
    this.setVisible(true);
    this.addWindowListener(new WindowAdapter()
      {
        public void windowClosing(WindowEvent e)
        {
          System.exit(0);
        }
      });  
      
  }
  public void getg()
  {
    g=p2.getGraphics();
  }
  
  public static void main(String[] args)
  {
    quse q1=new quse();
    q1.getg();
  }

  public void actionPerformed(ActionEvent e)
  {
    String str=e.getActionCommand();
    if(str.equals("Red"))
    {
      xz=1;
      draw=true;
      c3=Color.red;
    }
    else if(str.equals("Green"))
    {
      xz=1;
      draw=true;
      c3=Color.green;
    }
    else if(str.equals("Black"))
    {
      xz=1;
      draw=true;
      c3=Color.BLACK;
    }
    else
    c3=Color.blue;
    
  }

  public void mouseClicked(MouseEvent e) {
    // TODO: Add your code here
  }

  public void mousePressed(MouseEvent e) {
    // TODO: Add your code here
    x=e.getX();
    y=e.getY();
    if(draw==true)
      this.setCursor(Cursor.CROSSHAIR_CURSOR);
  }

  public void mouseReleased(MouseEvent e) {
    // TODO: Add your code here
    if(draw==true)
      this.setCursor(Cursor.DEFAULT_CURSOR);
    if(draw==true)
    {
      g.setColor(c3);//首先将画笔的颜色设置为用户选择的颜色
      switch(xz)
      {
        case 1:
          g.drawLine(x,y,e.getX(),e.getY());
          break;
        
      }
    }
  }

  public void mouseEntered(MouseEvent e) {
    // TODO: Add your code here
  }

  public void mouseExited(MouseEvent e) {
    // TODO: Add your code here
  }
  
}

5.Re:怎么取鼠标点击处的颜色? [Re: huwd] Copy to clipboard
Posted by: digital2003
Posted on: 2005-06-01 23:09

先开始的代码是取按纽上的写的颜色,把它赋予画板的系统颜色
因为先开始没有编辑器,所以写的代码有问题
现在看到你的要求,
看看现在的程序运行如何?

6.Re:怎么取鼠标点击处的颜色? [Re: huwd] Copy to clipboard
Posted by: yinpeng263
Posted on: 2005-06-08 03:57

Use the button

http://www.java2s.com/ExampleCode/Swing-JFC/Button.htm

draw
http://www.java2s.com/ExampleCode/2D-Graphics-GUI/Shape.htm

7.Re:怎么取鼠标点击处的颜色? [Re: huwd] Copy to clipboard
Posted by: huwd
Posted on: 2005-06-12 10:59


if(str.equals("Red"))
{
xz=1;
draw=true;
c3=Color.red;
}
else if(str.equals("Green"))
{
xz=1;
draw=true;
c3=Color.green;
}
else if(str.equals("Black"))
{
xz=1;
draw=true;
c3=Color.BLACK;
}
else
c3=Color.blue;

谢谢digital2003 多次回复
可是,你还是用了以上的多个if判断,如果再加两个不同颜色的按钮,那就要再加两个if语句判断
我就是想知道,有没有办法,在 actionPerformed或者mouseClicked里,直接写一句:g.setColor(当前鼠标点击位置的颜色);

8.Re:怎么取鼠标点击处的颜色? [Re: huwd] Copy to clipboard
Posted by: snowbird2005
Posted on: 2005-06-12 11:12

按钮button类有这样一个方法:getBackground()
通过这个方法得到按钮的背景色,就不用多个if判断了。

9.Re:怎么取鼠标点击处的颜色? [Re: huwd] Copy to clipboard
Posted by: snowbird2005
Posted on: 2005-06-12 21:03

试试以下代码:
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GetColorFromButton extends Applet implements ActionListener
{
private Button button1,button2,button3;
public void init()
{
button1=new Button("red");
button1.setBackground(Color.red);
button1.addActionListener(this);

button2=new Button("yellow");
button2.setBackground(Color.yellow);
button2.addActionListener(this);

button3=new Button("blue");
button3.setBackground(Color.blue);
button3.addActionListener(this);

add(button1);
add(button2);
add(button3);
}

public void actionPerformed(ActionEvent e)
{
Color color=((Button)e.getSource()).getBackground();
this.setForeground(color);
repaint();
}

public void paint(Graphics g)
{
g.drawLine(100,100,200,200);
}

}

10.Re:怎么取鼠标点击处的颜色? [Re: snowbird2005] Copy to clipboard
Posted by: huwd
Posted on: 2005-06-13 19:04

谢谢snowbird2005


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923