import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* <applet code="m0104434_02.class" width=480 height=480></applet> */

public class m0104434_02 extends Applet implements MouseListener {
	private static final double V_SIZE = 480;
	private int bX, bY, bW, bH, bD, bC, bO;
	private double bR;
	private Color[] cBrick = new Color[2];
	private Color cMortar;
	
	public m0104434_02() {
		cBrick[0] = new Color(127, 31, 31);
		cBrick[1] = new Color(79, 31, 31);
		cMortar = new Color(191, 191, 191);
		bX = 50; bY = 6; bW = 120; bH = 35; bD = 6; bC = 3; bR = 0.15; bO = 4;
		
		openPanel();
		addMouseListener(this);
	}
	
	public void paint(Graphics g) {
		int vsize = (int) V_SIZE;
		int x, y, R, G, B, tX, tB, bRr;
		
		// *** BEGIN YOUR PROGRAM *** //
		
		for (x = 0; x < vsize; x++) {
			for (y = 0; y < vsize; y++) {
				tX = (bW + bD) / bC * ((y + bY) % ((bH + bD) * bC) / (bH + bD));
				if ((x + bX + tX) % (bW + bD) < bW && (y + bY) % (bH + bD) < bH) {
					tB = ((x + bX + tX) / (bW + bD) + Math.pow((y + bY) / (bH + bD), (y + bY) / (bH + bD))) % bO == 0 ? 1 : 0;
					R = cBrick[tB].getRed();
					G = cBrick[tB].getGreen();
					B = cBrick[tB].getBlue();
				} else {
					R = cMortar.getRed();
					G = cMortar.getGreen();
					B = cMortar.getBlue();
				}
				
				bRr = (int) ((long) Math.pow(x + y, Math.sqrt((x + 40) / 13.0)) % (int) (255 * bR) - (int) (255 * bR / 2));
				R += bRr; if (R >= 256) R = 255; if (R < 0) R = 0;
				G += bRr; if (G >= 256) G = 255; if (G < 0) G = 0;
				B += bRr; if (B >= 256) B = 255; if (B < 0) B = 0;
				
 				// *** END YOUR PROGRAM *** //
				g.setColor(new Color(R,G,B));
				g.drawLine(x,y,x,y);
			}
		}
	}
	
	public void mouseClicked(MouseEvent e) { openPanel(); }
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	
	private void openPanel() {
		Container parent = getParent();
		while (!(parent instanceof Frame) && parent != null) parent = parent.getParent();
		final JDialog dValue = new JDialog((Frame) parent, "値の設定", true);
		dValue.setSize(new Dimension(210, 300));
		dValue.setLocation(40, 40);
		Container cp = dValue.getContentPane();
		cp.setLayout(new FlowLayout());
		JPanel p0 = new JPanel(new GridLayout(10, 2));
		cp.add(p0);
		JTextField tbX = new JTextField(Integer.toString(bX)), tbY = new JTextField(Integer.toString(bY)),
		tbW = new JTextField(Integer.toString(bW)), tbH = new JTextField(Integer.toString(bH)),
		tbD = new JTextField(Integer.toString(bD)), tbC = new JTextField(Integer.toString(bC)),
		tbR = new JTextField(Double.toString(bR)), tbO = new JTextField(Integer.toString(bO));
		JTextField[][] tcB = new JTextField[2][3];
		for (int n = 0; n < 2; n++) {
			tcB[n][0] = new JTextField(Integer.toString(cBrick[n].getRed()));
			tcB[n][1] = new JTextField(Integer.toString(cBrick[n].getGreen()));
			tcB[n][2] = new JTextField(Integer.toString(cBrick[n].getBlue()));
		}
		JPanel cp0 = new JPanel(new GridLayout(1, 3)), cp1 = new JPanel(new GridLayout(1, 3));
		p0.add(new JLabel("色1(RGB)：")); p0.add(cp0); for (int c = 0; c < 3; c++) cp0.add(tcB[0][c]);
		p0.add(new JLabel("色2(RGB)：")); p0.add(cp1); for (int c = 0; c < 3; c++) cp1.add(tcB[1][c]);
		p0.add(new JLabel("原点のX座標：")); p0.add(tbX);
		p0.add(new JLabel("原点のY座標：")); p0.add(tbY);
		p0.add(new JLabel("レンガの幅：")); p0.add(tbW);
		p0.add(new JLabel("レンガの高さ：")); p0.add(tbH);
		p0.add(new JLabel("レンガの間隔：")); p0.add(tbD);
		p0.add(new JLabel("レンガの周期：")); p0.add(tbC);
		p0.add(new JLabel("ざらつき(0-1)：")); p0.add(tbR);
		p0.add(new JLabel("色2の頻度(1/n)：")); p0.add(tbO);
		JButton bOK = new JButton("OK"); bOK.addActionListener(new AbstractAction() {
			public void actionPerformed(ActionEvent e) { dValue.setVisible(false); }
		});
		cp.add(bOK);
		dValue.setVisible(true);
		
		for (int n = 0; n < 2; n++)
			cBrick[n] = new Color(Integer.parseInt(tcB[n][0].getText()), Integer.parseInt(tcB[n][1].getText()), Integer.parseInt(tcB[n][2].getText()));
		
		bX = Integer.parseInt(tbX.getText());
		bY = Integer.parseInt(tbY.getText());
		bW = Integer.parseInt(tbW.getText());
		bH = Integer.parseInt(tbH.getText());
		bD = Integer.parseInt(tbD.getText());
		bC = Integer.parseInt(tbC.getText());
		bR = Double.parseDouble(tbR.getText());
		bO = Integer.parseInt(tbO.getText());
		
		repaint();
	}
}
