import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Main extends JFrame{
Container c;
int color; // color가1이면 레드, 2이면 그린, 3이면 블루를 조정하겠다
int r=127, g=127, b=127;
JLabel b1=new JLabel("0");
JLabel b2=new JLabel("0");
JLabel b3=new JLabel("0");
public Main(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
c = getContentPane();
c.setLayout(null);
// 1. 버튼 셋팅
JButton a=new JButton("Red");
JButton a2=new JButton("Green");
JButton a3=new JButton("Blue");
a.addActionListener(new MyActionListener());
a2.addActionListener(new MyActionListener());
a3.addActionListener(new MyActionListener());
a.setLocation(105, 50);
a2.setLocation(205,50);
a3.setLocation(305,50);
a.setSize(56,30);
a2.setSize(68,30);
a3.setSize(59,30);
c.add(a);
c.add(a2);
c.add(a3);
//2. 레이블 셋팅
c.add(b1);
c.add(b2);
c.add(b3);
b1.setLocation(125, 100);
b2.setLocation(230, 100);
b3.setLocation(320, 100);
b1.setSize(30,10);
b2.setSize(30,10);
b3.setSize(30,10);
c.addMouseWheelListener(new Mymouselistener());
setSize(500, 400);
setVisible(true);
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
JButton p=(JButton)e.getSource();
String text = p.getText();
// 어떤 버튼이 눌렸는지 콘솔에 출력하기
if(text.equals("Red")){
System.out.println("빨강");
color=1;
}
else if(text.equals("Green"))
{
System.out.println("초록");
color=2;
}
else{
System.out.println("파랑");
color=3;
}
}
}
class Mymouselistener implements MouseWheelListener{
// Red Green Blue
// 0 ~ 255 0~255 0~255
// 255 0 0 Red
// 255 0 255
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if(color==1) {
b2.setForeground(Color.white);
b3.setForeground(Color.white);
b1.setForeground(Color.red);
if(e.getWheelRotation()<0) {
if(r<255) r++;
}
else {
if(r>0) r--;
}
}
else if(color==2) {
b1.setForeground(Color.white);
b3.setForeground(Color.white);
b2.setForeground(Color.green);
if(e.getWheelRotation()<0) {
if (g<255) g++;
}
else {
if(g>0) g--;
}
}
else {
b1.setForeground(Color.white);
b2.setForeground(Color.white);
b3.setForeground(Color.blue);
if(e.getWheelRotation()<0) {
if(b<255) b++;
}
else {
if(b>0) b--;
}
}
b1.setText(Integer.toString(r));
b2.setText(Integer.toString(g));
b3.setText(Integer.toString(b));
c.setBackground(new Color(r,g,b));
}
}
public static void main(String[] args) {
new Main();
}
}