表格连线

package java2d;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;


import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class LineTable extends JPanel {
	Surface surf;
	public LineTable() {
		List<Integer> list = new ArrayList<Integer>();
		 List<Integer> kuaduList = new ArrayList<Integer>();
		list.add(0);
		list.add(2);
		list.add(3);
		list.add(9);
		list.add(4);
		list.add(3);
		list.add(4);
		list.add(4);
		list.add(5);
		list.add(7);	
		kuaduList.add(0);kuaduList.add(2);kuaduList.add(1);kuaduList.add(6);kuaduList.add(5);
		kuaduList.add(1);kuaduList.add(1);kuaduList.add(0);kuaduList.add(1);kuaduList.add(2);
		setLayout(new BorderLayout());
		setBorder(new TitledBorder(new EtchedBorder(), "line table"));
		add(surf = new Surface(list,kuaduList));
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {		
		final LineTable demo = new LineTable();		
		JFrame f = new JFrame("Java2D Demo - MemoryMonitor");
		f.getContentPane().add("Center", demo);
		f.pack();
		f.setSize(new Dimension(500, 400));
		f.setVisible(true);
	}

	/**
	 * 
	 * @author Administrator
	 * 
	 * @param imageicon
	 *            图片(会影响表格的高度和宽度)
	 * @param number
	 *            号码
	 * @param qishu
	 *            期数 (影响表格的高度)
	 * @param x
	 *            起点x
	 * @param y
	 *            起点y
	 */

	public class Surface extends JPanel {

		private Graphics2D big;
		private int w = 500;
		private int h = 400;
		private Rectangle graphOutlineRect = new Rectangle();
		private Line2D graphLine = new Line2D.Float();
		private Color graphColor = new Color(46, 139, 87);
		private List<Integer> list;
		private List<Integer> kuaduList = new ArrayList<Integer>();
		private int [][] yilou = {{0,1,2,3,4,5,6,7,8,9},
				{1,1,0,3,4,5,6,7,8,9},
				{2,1,2,0,4,5,6,7,8,9},
				{3,1,2,3,4,5,6,7,8,0},
				{4,1,2,3,0,5,6,7,8,9},
				{5,1,2,0,4,5,6,7,8,9},
				{6,1,2,3,0,5,6,7,8,9},
				{7,1,2,3,0,5,6,7,8,9},
				{8,1,2,3,4,0,6,7,8,9},
				{9,1,2,3,4,5,6,0,8,9},};
		public Surface() {
			setBackground(Color.WHITE);
		}
		
		public Surface(List<Integer> list) {
			this.list = list;
			setBackground(Color.WHITE);
		}
		
		public Surface(List<Integer> list,List<Integer> kuaduList) {
			this.list = list;
			this.kuaduList = kuaduList;
			setBackground(Color.WHITE);
		}

		public Dimension getMinimumSize() {
			return getPreferredSize();
		}

		public Dimension getMaximumSize() {
			return getPreferredSize();
		}

		public Dimension getPreferredSize() {
			return new Dimension(135, 80);
		}

		public void paint(Graphics g) {
			big = (Graphics2D) g;
			if (big == null) {
				return;
			}
			ImageIcon icon = new ImageIcon("e:/0.gif");
			Image image = icon.getImage();
			big.setBackground(getBackground());
			big.setColor(graphColor);
			int graphX = 30;// 起点的x坐标
			int graphY = 30;// 起点的y坐标
			int graphW = icon.getIconWidth() * 30;// 画图纸的宽度
			int graphH = icon.getIconHeight()*list.size();// 画图纸的高度
			int cellWidth = icon.getIconWidth();
			int cellHeight = icon.getIconHeight();
			graphOutlineRect.setRect(graphX, graphY, graphW, graphH);
			big.draw(graphOutlineRect);
			int graphRow = icon.getIconHeight();

			// .. Draw row ..
			for (int j = graphY; j <= graphH + graphY; j += graphRow) {
				graphLine.setLine(graphX, j, graphX + graphW, j);
				big.draw(graphLine);
			}
			//	           
			int graphColumn = cellWidth;
			for (int j = graphX + graphColumn; j < graphW + graphX; j += graphColumn) {
				graphLine.setLine(j, graphY, j, graphY + graphH);
				big.draw(graphLine);
			}
			
			//draw image
			for(int i=0;i<list.size();i++){
				int value = list.get(i);
				Image imag = getImage(value);
				big.drawImage(imag, graphX+value*icon.getIconWidth(), graphY+i*icon.getIconHeight(), null);
			}
			
			//draw joinline
			int value0= list.get(0);
			for(int i=1;i<list.size();i++){
				int value = list.get(i);
				graphLine.setLine(graphX+value0*cellWidth+cellWidth/2, graphY+(i-1)*cellHeight+cellHeight/2,
						graphX+value*cellWidth+cellWidth/2, graphY+i*cellHeight+cellHeight/2);
				big.setColor(Color.RED);
				big.draw(graphLine);
				value0 = value;
			}
			
			//draw yilou String
			for(int i=0;i<yilou.length;i++){
				int [] row = yilou[i];
				for(int j=0;j<row.length;j++){
					if(row[j] !=0 ){
						big.setColor(Color.BLACK);
						big.drawString(row[j]+"", graphX+j*cellWidth+cellWidth/3, graphY+i*cellHeight+cellHeight*3/4);
					}
				}
			}
			
			//draw danshuang
			for(int i=0;i<list.size();i++){
				int value = list.get(i);
				big.setColor(Color.BLACK);
				if(value%2 == 1)
					big.drawString("单", graphX+11*cellWidth+cellWidth/3, graphY+i*cellHeight+cellHeight*3/4);
				else
					big.drawString("双", graphX+12*cellWidth+cellWidth/3, graphY+i*cellHeight+cellHeight*3/4);
			}
			
			//draw daxiao
			for(int i=0;i<list.size();i++){
				int value = list.get(i);
				big.setColor(Color.BLACK);
				if(value>5)
					big.drawString("大", graphX+14*cellWidth+cellWidth/3, graphY+i*cellHeight+cellHeight*3/4);
				else
					big.drawString("小", graphX+15*cellWidth+cellWidth/3, graphY+i*cellHeight+cellHeight*3/4);
			}
			
			//draw 012
			for(int i=0;i<list.size();i++){
				int value = list.get(i);
				big.setColor(Color.BLACK);
				if(value%3==0)
					big.drawString("0", graphX+17*cellWidth+cellWidth/3, graphY+i*cellHeight+cellHeight*3/4);
				else if((value%3==1))
					big.drawString("1", graphX+18*cellWidth+cellWidth/3, graphY+i*cellHeight+cellHeight*3/4);
				else 
					big.drawString("2", graphX+19*cellWidth+cellWidth/3, graphY+i*cellHeight+cellHeight*3/4);
			}
			
			//draw kuadu 
			for(int i=0;i<kuaduList.size();i++){
				int value = kuaduList.get(i);
				Image imag = getImage(value);
				big.drawImage(imag, graphX+(value+21)*icon.getIconWidth(), graphY+i*icon.getIconHeight(), null);
			}
			
			//draw kuadujoinline
			int valuex= kuaduList.get(0);
			for(int i=1;i<kuaduList.size();i++){
				int value = kuaduList.get(i);
				graphLine.setLine(graphX+(valuex+21)*cellWidth+cellWidth/2, graphY+(i-1)*cellHeight+cellHeight/2,
						graphX+(value+21)*cellWidth+cellWidth/2, graphY+i*cellHeight+cellHeight/2);
				big.setColor(Color.RED);
				big.draw(graphLine);
				valuex = value;
			}
		}
		
		public Image getImage(int value){
			switch (value) {
			case 0:
				return new ImageIcon("e:/0.gif").getImage();
			case 1:
				return new ImageIcon("e:/1.gif").getImage();
			case 2:
				return new ImageIcon("e:/2.gif").getImage();
			case 3:
				return new ImageIcon("e:/3.gif").getImage();
			case 4:
				return new ImageIcon("e:/4.gif").getImage();
			case 5:
				return new ImageIcon("e:/5.gif").getImage();
			case 6:
				return new ImageIcon("e:/6.gif").getImage();
			case 7:
				return new ImageIcon("e:/7.gif").getImage();
			case 8:
				return new ImageIcon("e:/8.gif").getImage();
			case 9:
				return new ImageIcon("e:/9.gif").getImage();
			default:
				return new ImageIcon("e:/0.gif").getImage();
			}
		}

	}
}

你可能感兴趣的:(java)