Created
November 15, 2012 20:07
-
-
Save seronis/4080921 to your computer and use it in GitHub Desktop.
Color Cycle Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: seronis | |
* Date: 11/15/12 | |
* Time: 2:55 PM | |
*/ | |
public class TheApp extends JFrame | |
implements ActionListener | |
{ | |
private int value=0; | |
private JPanel panel; | |
public TheApp() { | |
initUI(); | |
} | |
private void initUI() { | |
panel = new JPanel(); | |
panel.setLayout(null); | |
add(panel); | |
setTitle("Color Cycling"); | |
setSize(1165,288); | |
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
for(int mode=0;mode<3;++mode) | |
for(int zloop=0; zloop<5; ++zloop){ | |
RGDisplay rgbp = new RGDisplay(zloop,mode); | |
rgbp.setSize(200,200); | |
rgbp.setLocation(25+(zloop*225),25+(mode*225)); | |
panel.add(rgbp); | |
} | |
Timer timer = new Timer(100,this); | |
timer.setRepeats(true); | |
timer.start(); | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
TheApp window = new TheApp(); | |
} | |
}); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
int rr = ((value )%5); | |
int gg = ((value/ 5)%5); | |
int bb = ((value/25)%5); | |
if(value<125) { | |
setTitle(" R:" + rr + " G:" + gg + " B:" + bb); | |
panel.setBackground(new Color(255*(rr)/4,255*(gg)/4,255*(bb)/4)); | |
} else { | |
setTitle("Color Cycle"); | |
panel.setBackground(Color.black); | |
} | |
value = (value+1)%256; | |
repaint(); | |
} | |
public class RGDisplay extends JPanel | |
{ | |
private int zval; | |
private int mode; | |
public RGDisplay(int z, int m){ | |
zval = z; | |
mode = m; | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
for( int xval=0; xval<5; ++xval){ | |
int x1 = getWidth()*(xval+0)/5; | |
for(int yval=0; yval<5; ++yval){ | |
int y1 = getHeight()*(yval+0)/5; | |
switch(mode){ | |
default: | |
case 0: | |
g.setColor(new Color(255*(xval)/4,255*(yval)/4,255*(zval)/4)); | |
break; | |
case 1: | |
g.setColor(new Color(255*(xval)/4,255*(zval)/4,255*(yval)/4)); | |
break; | |
case 2: | |
g.setColor(new Color(255*(zval)/4,255*(yval)/4,255*(xval)/4)); | |
break; | |
} | |
g.fillRect(x1,y1,getWidth()/5,getHeight()/5); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment