1.FileChooserT est.java类:
package OperationOfPictures;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class FileChooserTest {
public static void main(String[] args){
ImageV iewerFrame frame=new ImageV iewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setV isible(true);
Toolkit kit=Toolkit.getDefaultToolkit();
Image image = kit.getImage("Mark.jpg");
frame.setIconImage(image);
}
}
2.Canvas.java类:
package OperationOfPictures;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.*;
import javax.swing.JPanel;
public class Canvas extends JPanel {
private BufferedImage bufferedImage;//Buffered
private AffineTransform trans = new AffineTransform();
public void setImage(BufferedImage bufferedImage) {
if (bufferedImage != null) {
this.bufferedImage = bufferedImage;
}
if (isV isible()) {
paintImmediately(0, 0, getWidth(), getHeight());
}
}
public BufferedImage getImage() {
return bufferedImage;
}
public void setRota(double rota) {
trans.setToRotation(rota, (bufferedImage.getWidth()) >> 1, (bufferedImage.getHeight()) >> 1);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
if (bufferedImage == null) {
return;
}
Graphics2D gg = (Graphics2D) g.create();
int iw = bufferedImage.getWidth(), ih = bufferedImage.getHeight();
int anchorX = (getWidth() - iw) >> 1, anchorY = (getHeight() - ih) >> 1;
AffineTransform af = gg.getTransform();
af.translate(anchorX, anchorY);
af.concatenate(trans);
gg.drawImage(bufferedImage, af, this);
gg.dispose();
}
}
3.Cellapplet.java类:
package OperationOfPictures;
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
public class Cellapplet extends Applet{
private int default_r = 1;
private long default_nr = 18;
private int r;
private long nr;
private boolean layout= false;
private Thread drawThread = null;
private Cellcanvas canvas;
private Choice radiusChoice;
private JTextField ruleField;
private JLabel ruleSequence;
private Button stopButton,restartButton;
public Cellapplet() {
}
public void init() {
if (drawThread != null) {
drawThread = null;
}
r=1;
nr=18;
if (r < 1) {
r = default_r;
}
if (nr < 0) {
nr = default_nr; }
if (layout == false) {
GridBagLayout gridLayout = new GridBagLayout();//布局管理器
GridBagConstraints constr;//使用GridBagLayout布局管理器的约束setLayout(gridLayout);//设定添加布局管理器
Panel cellPanel = new Panel();
cellPanel.setLayout(new GridLayout(1, 0));//设定添加布局管理器canvas = new Cellcanvas(r, nr);
cellPanel.add("Center", canvas);
constr = new GridBagConstraints();
constr.fill = GridBagConstraints.BOTH;
constr.insets = new Insets(0, 0, 0, 0);
constr.weightx = 1.0;
constr.weighty = 1.0;
constr.gridwidth = GridBagConstraints.REMAINDER;
gridLayout.setConstraints(cellPanel, constr);
add(cellPanel);
JLabel radius_label = new JLabel("半径:");
constr = new GridBagConstraints();
constr.fill = GridBagConstraints.BOTH;
constr.insets = new Insets(2, 4, 2, 4);
constr.weightx = 0.1;
constr.weighty = 0.0;
constr.gridx = 3;
constr.gridy = 2;
gridLayout.setConstraints(radius_label, constr);
add(radius_label);
radiusChoice = new Choice();
radiusChoice.addItem("1");
radiusChoice.addItem("2");
radiusChoice.addItem("3");
radiusChoice.select(r - 1);
radiusChoice.setEnabled(false);
constr = new GridBagConstraints();
constr.fill = GridBagConstraints.BOTH;
constr.insets = new Insets(2, 4, 2, 4);
constr.weightx = 0.3;
constr.weighty = 0.0;
constr.gridx = 4;
constr.gridy = 2;
gridLayout.setConstraints(radiusChoice, constr);
add(radiusChoice);
JLabel rule_label = new JLabel("编码:");
constr = new GridBagConstraints();
constr.fill = GridBagConstraints.BOTH;
constr.weightx = 0.1;
constr.weighty = 0.0;
constr.gridx = 5;
constr.gridy = 2;
gridLayout.setConstraints(rule_label, constr);
add(rule_label);
ruleField = new JTextField("" + nr, 6);
ruleField.setEnabled(false);
constr = new GridBagConstraints();
constr.fill = GridBagConstraints.BOTH;
constr.insets = new Insets(2, 4, 2, 4);
constr.weightx = 0.3;
constr.weighty = 0.0;
constr.gridx = 6;
constr.gridy = 2;
constr.gridwidth = GridBagConstraints.REMAINDER; gridLayout.setConstraints(ruleField, constr);
add(ruleField);
stopButton = new Button("Stop");
constr = new GridBagConstraints();
constr.fill = GridBagConstraints.BOTH;
constr.insets = new Insets(2, 4, 2, 4);
constr.weightx = 0.3;
constr.weighty = 0.0;
constr.gridx = 3;
constr.gridy = 3;
constr.gridwidth = 2;
gridLayout.setConstraints(stopButton, constr);
add(stopButton);
restartButton = new Button("Restart"); restartButton.setEnabled(false);
constr = new GridBagConstraints();
constr.fill = GridBagConstraints.BOTH;
constr.insets = new Insets(2, 4, 2, 4);
constr.weightx = 0.3;
constr.weighty = 0.0;
constr.gridx = 5;
constr.gridy = 3;
constr.gridwidth = GridBagConstraints.REMAINDER; gridLayout.setConstraints(restartButton, constr);
add(restartButton);
ruleSequence = new JLabel("");
ruleSequence.setText("基元序列:"+canvas.getRule());
constr.fill = GridBagConstraints.BOTH;
constr.insets = new Insets(2, 4, 2, 4);
constr.weightx = 0.3;
constr.weighty = 0.0;
constr.gridx = 1;
constr.gridy = 4;
constr.gridwidth = GridBagConstraints.REMAINDER;
gridLayout.setConstraints(ruleSequence, constr);
add(ruleSequence);
ruleSequence.setVisible(true);
validate();
layout = true;
}
}
public void start(){
drawThread = new Thread(canvas);
drawThread.setPriority(Thread.MIN_PRIORITY);
drawThread.start();
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button) {
if (arg.equals("Restart")) {
int new_r;
new_r = Integer.parseInt(radiusChoice.getSelectedItem());
long new_nr;
new_nr = Long.parseLong(ruleField.getText());
if (drawThread != null)
drawThread = null;
r = new_r;
nr = new_nr;
canvas.reinit(r, nr);
drawThread = new Thread(canvas);
drawThread.setPriority(Thread.MIN_PRIORITY);
drawThread.start();
ruleSequence.setText("基元序列:" + canvas.getRule());
restartButton.setEnabled(false);
radiusChoice.setEnabled(false);
ruleField.setEnabled(false);
stopButton.setEnabled(true);
} else if (arg.equals("Stop")) {
if (drawThread != null) {
drawThread.interrupt();
drawThread = null;
restartButton.setEnabled(true);
radiusChoice.setEnabled(true);
ruleField.setEnabled(true);
stopButton.setEnabled(false);
} } }
return true;
}}
4.Cellauto.java类:
package OperationOfPictures;
import java.util.Random;
public class Cellauto {//细胞序列类
private final int cellState = 2;//状态数
private int radius = 1;//半径
private long ruleMaster = 18;//编码
private int[] ruleSequence = makeRule();//制定细胞状态演化规则
public Cellauto( int r, long Master) {
if (radius != r || ruleMaster != Master ) {
if (r >= 0) {
radius = r;//细胞的邻域半径
}
if (Master >= 0) {
ruleMaster = Master;//细胞规则的控制者
}
ruleSequence = makeRule();//重新制定规则
}
}
private int[] makeRule() {
int n= (int) (Math.pow(cellState, 2 * radius + 1));//确定规则序列的长度
int[] Sequence = new int[n];
for (int k = 1; k <= n; k++) {
Sequence[k - 1] = computeV alue(k, ruleMaster);//确定规则序列每个元素的值}
return Sequence;
}
private int computeV alue(int count, long n) {
if (count == 1) {
return (int) (n % cellState);
} else {
return computeV alue(count - 1, n / cellState);//递归求值
}
}
public int[] initSequence(int n) {//随机生成细胞初始状态序列
Random random = new Random();
int[] Sequence = new int[n];
for (int i = 0; i < n; i++) {
Sequence[i] = (int) (cellState * random.nextDouble());
if (Sequence[i] > cellState - 1) {
Sequence[i] = cellState - 1;
} }
return Sequence;
}
public int[] nextSequence(int[] Sequence) {//求当前细胞序列的下一个状态序列
int len = Sequence.length;
int[] newSequence = new int[len];
for (int i = 0; i < len; i++) {
newSequence[i] = 0;
for (int j = -radius; j <= radius; j++)
newSequence[i] = newSequence[i] * cellState + Sequence[(i + j + len)%len];
newSequence[i] = ruleSequence[newSequence[i]];
}
return newSequence;
}
public int[] getRuleSequence(){
return ruleSequence;
}
}
5.Cellcanvas.java类:
package OperationOfPictures;
import java.awt.*;
public class Cellcanvas extends Canvas implements Runnable{//细胞状态板
private int width = -1;//宽度
private int height = -1;//高度
private int gridwidth = 16;//细胞的大小
private int number1, number2;//分别控制细胞的横向变化和纵向变化
private Cellauto cell = null;//细胞序列类型
private int[] startSequence = null;//细胞序列的开始状态
private Color[] colors = new Color[2];//细胞的状态数组
private Image cellPicture =null;//细胞图像
private int[] ruleSequence = null;
public Cellcanvas(int r, long Master) {
cell = new Cellauto(r, Master);//细胞序列初始化实例
colors[0]= Color.white;//黑白色表示细胞的两种状态
colors[1]= Color.black;
ruleSequence=cell.getRuleSequence();
}
public void reinit(int r, long Master) {//改变条件后的细胞序列实例
cell = new Cellauto(r, Master);
ruleSequence=cell.getRuleSequence();
}
public void run() {
Graphics2D g;
Dimension d = getSize();
int[] nextSequence;
width = d.width;
number1 = (int) (width / gridwidth);//确定面板内一行容纳的细胞数,即细胞序列长度height = d.height;
number2 = (int) (height / gridwidth);
startSequence = cell.initSequence(number1);
cellPicture = this.createImage(width, height);
g = (Graphics2D) cellPicture.getGraphics();
// Draw the background
g.setColor(Color.white);//设置背景色
g.fillRect(0, 0, width, height);//填充背景色
// Draw the initial configuration
for (int i = 0; i < number1; i++) {
g.setColor(colors[startSequence[i]]);//确定初始细胞序列各个细胞的状态颜色
g.fillRect(i * gridwidth, 0, gridwidth, gridwidth);//画出初始细胞序列的状态
}
show_picture();//显示初始细胞序列的状态
// Compute and draw rest of the picture
nextSequence = startSequence;
for (int j = 1; j < number2; j++) {
nextSequence = cell.nextSequence(nextSequence);//求演化后的细胞序列
for (int i = 0; i < number1; i++) {
g.setColor(colors[nextSequence[i]]);//求演化后的细胞序列各个细胞的状态颜色g.fillRect(i * gridwidth, j * gridwidth, gridwidth, gridwidth);//画出演化后的细胞序列的状态
}
show_picture();//显示演化后的细胞序列的状态
}
try {
while (true) {
g.copyArea(0, 0, width, height, 0, -gridwidth);
nextSequence = cell.nextSequence(nextSequence);
for (int i = 0; i < number1; i++) {
g.setColor(colors[nextSequence[i]]);
g.fillRect(i * gridwidth, height - gridwidth, gridwidth, gridwidth);
}
show_picture();
Thread.sleep(100);//视觉停留时间的设定
}
} catch (InterruptedException e) {
}
}
synchronized void show_picture() {
Graphics gp = this.getGraphics();
if (gp != null && cellPicture != null) {
Image picture = cellPicture;
gp.drawImage(picture, 0, 0, this);//画出细胞序列的状态颜色表示
gp.dispose();
}
}
public void paint(Graphics g) {//重载绘画函数,自行调用
show_picture();
}
public String getRule(){
String rule = "";
for (int k = 0; k < ruleSequence.length; k++) {
rule+= ruleSequence[k];
}
return rule;
}}
https://www.doczj.com/doc/ec8122377.html,plex.java类:
package OperationOfPictures;
public class Complex {
private double re;
private double im;
public Complex() {
this.re = 0;
this.im = 0;
}
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
public void setRE(double re) {
this.re = re;
}
public void setIM(double im) {
this.im = im;
}
public double getRE() {
return this.re;
}
public double getIM() {
return this.im;
}
public double abs(){
return Math.sqrt(re*re+im*im);
}
public double angle(){
return Math.atan(im/re);
}
public void equal(Complex that){
this.re=that.getRE();
this.im=that.getIM();
}
public void addEqual(Complex that){
this.re+=that.getRE();
this.im+=that.getIM();
}
public void subEqual(Complex that){
this.re-=that.getRE();
this.im-=that.getIM();
}
public void mulEqual(Complex that){
double Re=this.getRE()*that.getRE()-this.getIM()*that.getIM();
double Im=this.getRE()*that.getIM()+this.getIM()*that.getRE();
this.re=Re;
this.im=Im;
}
public void mulEqual(double temp){
this.re*=temp;
this.im*=temp;
}
public void divEqual(double temp){
this.re/=temp;
this.im/=temp;
}
public Complex add(Complex that){
return new Complex(this.getRE()+that.getRE(),
this.getIM()+that.getIM());
}
public Complex sub(Complex that){
return new Complex(this.getRE()-that.getRE(),
this.getIM()-that.getIM());
}
public Complex mul(Complex that){
return new Complex(this.getRE()*that.getRE()-this.getIM()*that.getIM(),
this.getRE()*that.getIM()+this.getIM()*that.getRE());
}
public Complex div(double temp){
return new Complex(this.getRE()/temp,
this.getIM()/temp);
}
public void inverse(){
im=-im;
}}
7.Coordinate.java类:
package OperationOfPictures;
import java.awt.*;
import javax.swing.*;
public class Coordinate extends JFrame {
private int Max;
private static int height = 500;
private static int width = 400;
private int[] grayHistogram;
private int[] rHistogram;
private int[] gHistogram;
private int[] bHistogram;
private boolean colorFlag = false;
private boolean grayFlag = false;
public Coordinate(int max) {
Max = max;
setTitle("图像直方图");
JPanel pdown;
setSize(width, height);
pdown = new JPanel();
add(pdown, BorderLayout.SOUTH);
}
public void paint(Graphics g) {
int k=1,max=400;
if (max < Max) {
while (max < Max) {
k++;
max = max + 400;
}
g.setColor(Color.BLACK);
g.drawLine(52, 460, 337, 460);
g.drawLine(52, 45, 52, 460);
g.drawLine(337, 460, 332, 455);
g.drawLine(337, 460, 332, 465);
g.drawLine(52, 45, 47, 50);
g.drawLine(52, 45, 57, 50);
g.drawString("频度", 43, 45);
int j = 32;
for (int i = 0; i < 9; i++) {
g.drawLine(52 + 32 * i, 455, 52 + 32 * i, 460);
g.drawString(Integer.toString(j * i), 49 + 32 * i, 475);
}
j = 40;
for (int i = 1; i < 11; i++) {
g.drawLine(52, 460 - i * 40, 57, 460 - i * 40);
g.drawString(Integer.toString(j * i * k), 20, 466 - i * 40);
}
if (grayFlag) {
g.drawString("灰度直方图", 200, 45);
g.drawString("灰度值", 340, 465);
g.setColor(Color.gray);
for (int i = 0; i < 256; i++)
g.drawLine(52 + i, 459 - (int) (grayHistogram[i] / k), 52 + i, 460);
}
if (colorFlag) {
g.drawString("彩色直方图", 200, 45);
g.drawString("RGB值", 343, 465);
g.drawString("R值", 430, 50);
g.drawString("G值", 430, 55);
g.drawString("B值", 430, 60);
g.setColor(Color.red);
g.drawLine(425, 50, 430, 50);
for (int i = 0; i < 256; i++)
g.drawLine(52 + i, 459 - (int) (rHistogram[i] / k), 52 + i, 460);
g.setColor(Color.green);
g.drawLine(425, 55, 430, 55);
for (int i = 0; i < 256; i++)
g.drawLine(52 + i, 459 - (int)(gHistogram[i] / k), 52 + i,460);
g.setColor(Color.blue);
g.drawLine(425, 60, 430, 60);
for (int i = 0; i < 256; i++)
g.drawLine(52 + i, 459 - (int) (bHistogram[i] / k), 52 + i, 460);
}
}
else{
int[] yinZi={1,2,4,5,8,10,20,40};