RandomRectangles类中的X、Y和n分别代表生成举行的横坐标范围、纵坐标范围和个数。
FrameWork类用于演示。
RandomRectangle.java
public class RandomRectangles {
private static final int maxn = 1010;
private static double[] x = new double[maxn];
private static double[] y = new double[maxn];
private static double[] w = new double[maxn];
private static double[] h =new double[maxn];
private static double[] xmax = new double[maxn];
private static double[] ymax = new double[maxn];
private static double[] xx = new double[maxn];
private static double[] yy = new double[maxn];
private static double X = 1000;
private static double Y = 600;
private static int n = 2;
private static double[][] ans = new double[n][4];
private static int cmp(int i, int j) {
if(y[i] < y[j] || y[i] == y[j] && x[i] < x[j]) return -1;
return 1;
}
private static void sort(int l, int r) {
if(l == r) return;
int mid = (l + r) >> 1;
sort(l, mid);
sort(mid+1, r);
int i = l, j = mid + 1, k = l;
while(i <= mid || j <= r) {
if(i > mid) {
xx[k] = x[j];
yy[k++] = y[j++];
}
else if(j > r) {
xx[k] = x[i];
yy[k++] = y[i++];
}
else if(cmp(i,j) == -1) {
xx[k] = x[i];
yy[k++] = y[i++];
}
else {
xx[k] = x[j];
yy[k++] = y[j++];
}
}
for(i=l;i<=r;i++) {
x[i] = xx[i];
y[i] = yy[i];
}
return;
}
private static boolean create_points() {
for(int i=0;i<n;i++) {
xmax[i] = X;
ymax[i] = Y;
x[i] = Math.random() * X;
y[i] = Math.random() * Y;
}
sort(0, n-1);
for(int i=0;i<n-1;i++) {
if(y[i] == y[i+1] && x[i] == x[i+1]) return false;
}
return true;
}
public static double[][] getData() {
while(true) {
if(create_points() == true) break;
}
for(int i=0;i<n-1;i++) {
if(y[i] == y[i+1]) xmax[i] = x[i];
}
for(int i=0;i<n;i++) {
w[i] = Math.random() * (xmax[i] - x[i]);
for(int j=i+1;j<n;j++) {
if(x[j] >= x[i] && x[j] <= x[i]+w[i]) {
ymax[i] = y[j];
break;
}
}
h[i] = Math.random() * (ymax[i] - y[i]);
for(int j=i+1;j<n;j++) {
if(x[j] < x[i] && y[j] >= y[i] && y[j] <= y[i]+h[i]) {
xmax[j] = xmax[j] < x[j] ? xmax[j] : x[j];
}
}
}
for(int i=0;i<n;i++) {
ans[i][0] = x[i];
ans[i][1] = y[i];
ans[i][2] = w[i];
ans[i][3] = h[i];
}
return ans;
}
public static void main(String[] args) {
ans = getData();
System.out.println(ans[2][2]);
}
}
FrameWork.java
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class FrameWork extends JFrame {
private static final double pi = Math.acos(-1.0);
private static final int Width = 1200;
private static final int Height = 800;
private static JFrame frame = null;
public FrameWork() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(Width, Height);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(0, 0, Width, Height);
getContentPane().add(panel);
setVisible(true);
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, Width, Height);
g.setColor(Color.black);
double[][] ans = RandomRectangles.getData();
for(int i=0;i<ans.length;i++) {
double x0 = ans[i][0];
double y0 = ans[i][1];
double x1 = x0 + ans[i][2];
double y1 = y0 + ans[i][3];
System.out.println(x0 + " " + y0 + " " + x1 + " " + y1);
}
for(int i=0;i<ans.length;i++)
g.drawRect((int)ans[i][0], (int)ans[i][1], (int)ans[i][2], (int)ans[i][3]);
}
}
public static void main(String[] args) {
new FrameWork();
}
}
posted on 2015-04-15 20:27
marchalex 阅读(351)
评论(0) 编辑 收藏 所属分类:
java小程序