看了,张老师讲的交通灯,我有一股想把这个程序从新写一编的冲动,因为我认为学习不能光看,还得亲自动手才行。
设计思想:典型MVC,数据模型加控制器加界面显示。具体情况如下:
数据模型:Light类,他主要是保存灯的各种状态,属性状态如下:
1. 开关,标识灯的亮于灭
2. 颜色,红灯,绿灯,黄灯
3. 位置,东,西,男,北
4. 类型,是控制什么信号的,通行,直行,左转,右转,调头
5. 组,现在国际上一般一个颜色信号用3个灯表示,这三个灯就为一组
6. 生命的开始,标识灯在一个信号周期内打开的起始时间,可以有多个开始时间
7. 生命长度,标识在一个信号周期内打开多长时间,可以有间隔
控制器:Control类,他主要是控制等如何有序的亮灭,以及时间间隔的长短。
显示:MainFrame类,他主要是实时显示交通灯和路面的情况,
下面是核心程序
看了,张老师讲的交通灯,我有一股想把这个程序从新写一编的冲动,因为我认为学习不能光看,还得亲自动手才行。
设计思想:典型MVC,数据模型加控制器加界面显示。具体情况如下:
数据模型:Light类,他主要是保存灯的各种状态,属性状态如下:
1. 开关,标识灯的亮于灭
2. 颜色,红灯,绿灯,黄灯
3. 位置,东,西,男,北
4. 类型,是控制什么信号的,通行,直行,左转,右转,调头
5. 组,现在国际上一般一个颜色信号用3个灯表示,这三个灯就为一组
6. 生命的开始,标识灯在一个信号周期内打开的起始时间,可以有多个开始时间
7. 生命长度,标识在一个信号周期内打开多长时间,可以有间隔
控制器:Control类,他主要是控制等如何有序的亮灭,以及时间间隔的长短。
显示:MainFrame类,他主要是实时显示交通灯和路面的情况,
下面是核心程序,和运行图片
下面是原程序:
1.
package heima_alai;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Polygon;
import java.util.Vector;
import java.awt.BasicStroke;
import java.awt.Font;
/**
*/
public class Light {
/**灯的颜色属性-红色**/
public static final String RED="red";
/**灯的颜色属性-黄色**/
public static final String YELLOW="yellow";
/**灯的颜色属性-绿色**/
public static final String GREEN="green";
/**灯的控制信号**/
public static final String 通行="通行";
/**灯的控制信号**/
public static final String 直行="直行";
/**灯的控制信号**/
public static final String 左行="左行";
/**灯的控制信号**/
public static final String 调头="调头";
/**灯的控制信号**/
public static final String 右行="右行";
public String name="";
/**灯的控制信号类型,初值 通行**/
public String type="通行";
/**灯位置**/
public String position="北";
/**灯位组,现在的马路一般都是3个一组**/
public String 组="1";
/**灯的打开时间长度,这是由程序控制的,外部一般不需要访问**/
private int life=0;
/**灯离关闭还剩的时间,不提供外部访问**/
private int resLife=0;
/**灯的是否关闭状态,false-关,true-开**/
private boolean isActive=false;
/**灯的颜色属性**/
private String color="red";
/**灯打开的起点时间**/
private int startInCycle=0;
/**放置灯在一个信号周期里,打开的时间int[]{start,length}**/
public Vector<int[]> time=new Vector();
/**得到当前的灯很剩多长时间关闭**/
public int getResLife() {
return resLife;
}
/**得到当前的灯什么时候启动**/
public int getStartInCycle() {
return startInCycle;
}
/**得到当前的灯工作时间**/
public int getLife() {
return life;
}
/**得到当前的灯的开与关**/
public boolean isActive() {
return isActive;
}
public void setStartInCycle(int startInCycle) {
this.startInCycle = startInCycle;
}
public void add区间(int start,int length){
time.add(new int[]{start,length});
}
public int getMaxLife(){
int ll=0;
for(int i=0;i<time.size();i++){
int[] bas=time.get(i);
if(ll<bas[0]+bas[1])ll=bas[0]+bas[1];
}
return ll;
}
/**计算在当前时间灯的状态**/
public void update(int timeInCyle){
for(int i=0;i<time.size();i++){
int[] bas=time.get(i);
if(timeInCyle>bas[0]&&timeInCyle<(bas[0]+bas[1]+1)){
isActive=true;
resLife=bas[0]-timeInCyle+bas[1];
startInCycle=bas[0];
life=bas[1];
break;
}else{isActive=false;}
}
}
public Light(Vector<int[]> v,String color){
time=v;
this.color=color;
}
/**用时间数组,颜色,类型,位置,组来实例化一个灯**/
public Light(Vector<int[]> v,String color,String type,String positon,String chu){
time=v;
this.color=color;
this.type=type;
this.position=positon;
this.组=chu;
}
public void setLife(int life) {
this.life = life;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setPostion(String Postion) {
this.position = position;
}
public String getPostion() {
return position;
}
public void set组(String 组) {
this.组 = 组;
}
public String get组() {
return 组;
}
/*用来在马路的正确位置显示自己的*/
public void rendSelf(Graphics2D g,int x,int y,int r){
Color c=g.getColor();
Polygon pol=null;
if(type.equals(this.直行)){
if(this.position.equals("北"))
pol= Road.getDirection(x+r/2,y+r,r,0);
if(this.position.equals("南"))
pol= Road.getDirection(x+r/2,y,r,1);
if(this.position.equals("西"))
pol= Road.getDirection(x+r,y+r/2,r,2);
if(this.position.equals("东"))
pol= Road.getDirection(x,y+r/2,r,3);
}
if(type.equals(this.左行)){
if(this.position.equals("北"))
pol= Road.getDirection(x+r,y+r/2,r,2);
if(this.position.equals("南"))
pol= Road.getDirection(x,y+r/2,r,3);
if(this.position.equals("西"))
pol= Road.getDirection(x+r/2,y,r,1);
if(this.position.equals("东"))
pol= Road.getDirection(x+r/2,y+r,r,0);
}
if(type.equals(this.右行)){
if(this.position.equals("北"))
pol= Road.getDirection(x,y+r/2,r,3);
if(this.position.equals("南"))
pol= Road.getDirection(x+r,y+r/2,r,2);
if(this.position.equals("西"))
pol= Road.getDirection(x+r/2,y+r,r,0);
if(this.position.equals("东"))
pol= Road.getDirection(x+r/2,y,r,1);
}
if(!isActive){
g.setColor(Color.black);
if(type.equals(this.调头)){
g.fillRect(x,y,r,r);
}else{
g.fillRoundRect(x,y,r,r,r,r);}
g.setColor(c);
return;
}
if(type.equals(this.调头)){
if(color.equals(this.RED)){
g.setColor(Color.red);
g.fillRect(x,y,r,r);
}
if(color.equals(this.GREEN)){
g.setColor(Color.GREEN);
g.fillRect(x,y,r,r);
}
if(color.equals(this.YELLOW)){
g.setColor(Color.YELLOW);
g.fillRect(x,y,r,r);
}
if(color.equals(this.GREEN)||color.equals(this.YELLOW)){
if(isActive){
Font f=g.getFont();
g.setFont(new Font("serif", 1|2, 60));
if(this.position.equals("东"))
g.drawString(""+this.resLife,Road.w-Road.lw/2,Road.h-Road.lh/2);
if(this.position.equals("西"))
g.drawString(""+this.resLife,Road.lw/2,Road.lh/2);
if(this.position.equals("南"))
g.drawString(""+this.resLife,Road.lw/2,Road.h-Road.lh/2);
if(this.position.equals("北"))
g.drawString(""+this.resLife,Road.w-Road.lw/2,Road.lh/2);
g.setFont(f);}
}
g.setColor(c);
return ;
}
if(color.equals(this.RED)){
g.setColor(Color.red);
if(!type.equals(this.通行)){
g.drawRoundRect(x,y,r,r,r,r);
g.fill(pol);
} else{
g.fillRoundRect(x,y,r,r,r,r);
}
}
if(color.equals(this.GREEN)){
g.setColor(Color.GREEN);
if(!type.equals(this.通行)){
g.drawRoundRect(x,y,r,r,r,r);
g.fill(pol);
} else{
g.fillRoundRect(x,y,r,r,r,r);
}
}
if(color.equals(this.YELLOW)){
g.setColor(Color.yellow);
if(!type.equals(this.通行)){
g.drawRoundRect(x,y,r,r,r,r);
g.fill(pol);
} else{
g.fillRoundRect(x,y,r,r,r,r);
}
}
if(color.equals(this.GREEN)||color.equals(this.YELLOW)){
if(isActive){
Font f=g.getFont();
g.setFont(new Font("serif", 1|2, 60));
if(this.position.equals("东"))
g.drawString(""+this.resLife,Road.w-Road.lw/2,Road.h-Road.lh/2);
if(this.position.equals("西"))
g.drawString(""+this.resLife,Road.lw/2,Road.lh/2);
if(this.position.equals("南"))
g.drawString(""+this.resLife,Road.lw/2,Road.h-Road.lh/2);
if(this.position.equals("北"))
g.drawString(""+this.resLife,Road.w-Road.lw/2,Road.lh/2);
g.setFont(f);}
}
g.setColor(c);
}
public void render(Graphics2D g,int y,int h,float T){
Color c=g.getColor();
for(int i=0;i<time.size();i++){
int bas[]=time.get(i);
if(!isActive){
g.setColor(Color.darkGray);
g.fillRect(Math.round(bas[0]*T),y,Math.round(bas[1]*T),h);
}else{
if(color.equals(this.RED)){
g.setColor(Color.red);
g.fillRect(Math.round(bas[0]*T),y,Math.round(bas[1]*T),h);
}
if(color.equals(this.GREEN)){
g.setColor(Color.GREEN);
g.fillRect(Math.round(bas[0]*T),y,Math.round(bas[1]*T),h);
}
if(color.equals(this.YELLOW)){
g.setColor(Color.yellow);
g.fillRect(Math.round(bas[0]*T),y,Math.round(bas[1]*T),h);
}
}
}//for
g.setColor(c);
}
}
2.
package heima_alai;
import java.util.Vector;
import java.io.FileInputStream;
import java.io.*;
public class Control extends Thread {
public boolean isRun=false;
public Vector<Light> lights=new Vector();
private int cycle=60;//单位是秒
public int nowTime=0;
public int st=1000;
public int getCycle() {
calculateCycle();
return cycle;
}
public void setCycle(int cycle) {
this.cycle = cycle;
}
public Control(){
BufferedReader in=null;
try {
in= new BufferedReader(new FileReader("res/红录灯.txt"));
String s=null;
while((s=in.readLine())!=null){
Vector v=new Vector();
String sm[]=s.split("\t");
String s2=sm[3].replace("\"","");
s2=s2.replaceAll(",","/");
String ss[]=s2.split("]");
for(int i=0;i<ss.length;i++){
ss[i]=ss[i].replace("[","");
String sm2[]=ss[i].split("/");
v.add(new int[]{Integer.parseInt(sm2[0]),Integer.parseInt(sm2[1])});
}
// public Light(Vector<int[]> v,String color,String type,String positon,String chu)
Light l1=new Light(v,sm[2],sm[1],sm[0],sm[4]);
lights.addElement(l1);
}
in.close();
} catch (Exception ex) {
System.out.println("找不到res/红录灯.txt 文件");
try {
in.close();
} catch (IOException ex1) {
}
}
calculateCycle();
}
public void calculateCycle(){
if(lights.size()==0)return;
int t=1;
for(int j=0;j<lights.size();j++){
Light temp= ((Light)lights.get(j));
if(t<temp.getMaxLife())
{t=temp.getMaxLife();
}
}
cycle=t;
}
public void disposeEx(){}
public Vector getLights(){return lights;}
public static void main(String ss[]){
// Control co= new Control();
float f=(float)3/4;
System.out.println(f);
}
public void run(){
while(true){
for(int i=1;i<cycle+1;i++){
nowTime=i;
try {
for(int j=0;j<lights.size();j++){
Light temp= ((Light)lights.get(j));
temp.update(i);
}
if(isRun)
disposeEx();
Thread.sleep(st);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
3.
package heima_alai;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
public class LightTableMode extends DefaultTableModel{
Vector colName=new Vector();
public LightTableMode(){
System.out.println("sdfsd");
}
public LightTableMode(Vector<Light> data) {
colName.addElement("路口");//0
colName.addElement("种类");//2
colName.addElement("颜色");//3
colName.addElement("起点-长度");//4
//5
colName.addElement("状态");
colName.addElement("剩余");
colName.addElement("组");
dataVector=new Vector();
dataVector.addElement(data);
System.out.println(data+"++++++++1");
setDataVector(dataVector, colName);
System.out.println(data+"++++++++");
}
public void setDataVector(Vector dataVector, Vector columnIdentifiers) {
this.dataVector = dataVector;
this.columnIdentifiers = columnIdentifiers;
fireTableStructureChanged();
}
public String getS(Vector<int[]> v){
String ss="";
for(int i=0;i<v.size();i++){
int bas[]=v.get(i);
ss=ss+"["+bas[0]+","+bas[1]+"]";
}
return ss;
}
private void setInV(String s,Vector<int[]> v){
v.clear();
s=s.replaceAll(",","/");
String ss[]=s.split("]");
for(int i=0;i<ss.length;i++){
ss[i]=ss[i].replace("[","");
String sm[]=ss[i].split("/");
v.add(new int[]{Integer.parseInt(sm[0]),Integer.parseInt(sm[1])});
}
}
public void updateJpanel(){}
public Object getValueAt(int row, int column) {
Vector rowVector = (Vector)dataVector.elementAt(0);
Light li=(Light)rowVector.elementAt(row);
if(column==0)return li.getPostion();
if(column==1)return li.getType();
if(column==2)return li.getColor();
if(column==3)return getS(li.time);
// if(column==4)return li.time.toString();
if(column==4)return li.isActive()?"打开":"关闭";
if(column==5)return li.getResLife();
if(column==6)return li.get组();
return null;
}
public void setValueAt(Object aValue, int row, int column) {
Vector rowVector = (Vector)dataVector.elementAt(0);
Light li=(Light)rowVector.elementAt(row);
if(column==0){li.position=aValue+""; updateJpanel();}
if(column==1){li.type=aValue+""; updateJpanel();}
if(column==2){li.setColor(aValue+""); updateJpanel();}
//if(column==3)li.setStartInCycle(Integer.parseInt(aValue+""));
if(column==3){setInV(aValue.toString(),li.time);
updateJpanel();
}
if(column==4) {
if(aValue==null){ li.setActive(false);}else{
if(aValue.equals("关闭"))li.setActive(false);
if(aValue.equals("打开"))li.setActive(true);
}
}
// if(column==5) li.setResLife(Integer.parseInt(aValue+""));
if(column==6) li.set组(aValue+"");
fireTableCellUpdated(row, column);
}
public Class<?> getColumnClass(int columnIndex) {
if(columnIndex==0)
return AlaiString.class;
if(columnIndex==1)
return AlaiString2.class;
if(columnIndex==2)
return AlaiString3.class;
return Object.class;
}
public int getRowCount() {
if(dataVector.size()==0)return 0;
return ((Vector)dataVector.elementAt(0)).size();
}
public void insertRow(int row, Light ligh) {
if(row==-1)row=this.getRowCount();
// if(row==-1)return;
((Vector)dataVector.elementAt(0)).insertElementAt(ligh, row);
fireTableRowsInserted(row, row);
}
public void removeRow(int row) {
if(row==-1)row=this.getRowCount()-1;
if(row==-1)return;
((Vector)dataVector.elementAt(0)).removeElementAt(row);
fireTableRowsDeleted(row, row);
}
public boolean isCellEditable(int row, int column) {
if(column==0)return true;
if(column==1)return true;
if(column==2)return true;
if(column==3)return true;
if(column==4)return true;
if(column==6)return true;
return false;
}
}
4.
package heima_alai;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.TexturePaint;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.geom.Rectangle2D;
import java.io.*;
import java.awt.Color;
import java.awt.Polygon;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
public class Road {
Hashtable cont=new Hashtable();
TexturePaint t;
BufferedImage texureImage;
Control cot;
public Road(Control cot){
this.cot=cot;
try {
texureImage = ImageIO.read(new File("res\\1174.png"));
} catch (IOException ex) {
}
Rectangle2D anc=new Rectangle2D.Double(0,0,texureImage.getWidth() ,texureImage.getHeight());
t=new TexturePaint(texureImage,anc);
cont.put("东",new Hashtable());//
cont.put("西",new Hashtable());
cont.put("南",new Hashtable());
cont.put("北",new Hashtable());
}
static int lw=0;
static int lh=0;
static int w=0;
static int h=0;
public void updateCont(){
Enumeration en=cont.elements();
while(en.hasMoreElements()){
Hashtable ht=(Hashtable)en.nextElement();
Enumeration en2= ht.elements();
while(en2.hasMoreElements()){
Vector v=(Vector)en2.nextElement();
v.clear();
}
}
Vector lights=cot.getLights();
for(int i=0;i<lights.size();i++){
Light li=(Light)lights.get(i);
Hashtable h= (Hashtable)cont.get(li.getPostion());
if(h.get(li.get组())==null){
Vector v=new Vector();
h.put(li.get组(),v);
v.addElement(li);
}else{
//绿色第一,黄色第二,红色最后
Vector v=(Vector)h.get(li.get组());
v.addElement(li);
}
}
}
public void paintLight(Graphics2D g,int r){
updateCont();
Hashtable has=(Hashtable)cont.get("东");
Enumeration en=has.elements();
int col=0;
while(en.hasMoreElements()){
Vector v=( Vector)en.nextElement();
for(int row=0;row<v.size();row++){
Light li=(Light)v.get(row);
li.rendSelf(g,w-lw+60-r*row,h-(lh+r*col+r),r);
}
col++;
}//
col=0;
Hashtable has2=(Hashtable)cont.get("西");
Enumeration en2=has2.elements();
while(en2.hasMoreElements()){
Vector v=( Vector)en2.nextElement();
for(int row=0;row<v.size();row++){
Light li=(Light)v.get(row);
li.rendSelf(g,lw-60-r*row,lh+r*col,r);
}
col++;
}//
col=0;
Hashtable has3=(Hashtable)cont.get("南");
Enumeration en3=has3.elements();
while(en3.hasMoreElements()){
Vector v=( Vector)en3.nextElement();
for(int row=0;row<v.size();row++){
Light li=(Light)v.get(row);
li.rendSelf(g,lw+r*col,h-lh+r*row,r);
}
col++;
}//
col=0;
Hashtable has4=(Hashtable)cont.get("北");
Enumeration en4=has4.elements();
while(en4.hasMoreElements()){
Vector v=( Vector)en4.nextElement();
for(int row=0;row<v.size();row++){
Light li=(Light)v.get(row);
li.rendSelf(g,w-lw-r-r*col,lh-r-r*row,r);
}
col++;
}
}
public void rend(Graphics2D g,int x,int y,int w,int h){
lw=w/3;
lh=h/4;
this.w=w;
this.h=h;
g.setPaint(t);
g.fillRect(0,0,lw,lh);
g.fillRect(w-lw,0,lw,lh);
g.fillRect(0,h-lh,lw,lh);
g.fillRect(w-lw,h-lh,lw,lh);
g.setColor(Color.GREEN);
g.fillRect(0,h/2-10,lw-40,20);
g.setColor(Color.BLACK);
g.drawString("西",lw-60,h/2+5);
g.setColor(Color.GREEN);
g.fillRect(w-lw+40,h/2-10,lw,20);
g.setColor(Color.BLACK);
g.drawString("东",w-lw+60,h/2+5);
g.setColor(Color.GREEN);
g.fillRect(w/2-10,0,20,lh-40);
g.setColor(Color.BLACK);
g.drawString("北",w/2-5,lh-60);
g.setColor(Color.GREEN);
g.fillRect(w/2-10,h-lh+40,20,lh);
g.setColor(Color.BLACK);
g.drawString("南",w/2-5,h-lh+60);
g.setColor(Color.white);
int temp=0;
for(int i=0;i<lw-20;i=temp*40){//东西
temp++;
g.fillRect(i,(h/2+lh)/2-3,20,6);//西上
g.fillRect(i,h/2+(h/2-lh)/2-3,20,6);//西下
g.fillRect(w-lw+i+40,(h/2+lh)/2-3,20,6);
g.fillRect(w-lw+i+40,h/2+(h/2-lh)/2-3,20,6);
}
g.fill(this.getDirection(80,((h/2+lh)/2+lh)/2,30,2));
g.fill(this.getDirection(80+w-lw,((h/2+lh)/2+lh)/2,30,2));
g.fill(this.getDirection(80,(h/2+(h/2+lh)/2)/2,30,2));
g.fill(this.getDirection(80+w-lw,(h/2+(h/2+lh)/2)/2,30,2));
g.fill(this.getDirection(80,(h/2+h/2+(h/2-lh)/2)/2,30,3));
g.fill(this.getDirection(80+w-lw,(h/2+h/2+(h/2-lh)/2)/2,30,3));
g.fill(this.getDirection(80,(h/2+(h/2-lh)/2+h-lh)/2,30,3));
g.fill(this.getDirection(80+w-lw,(h/2+(h/2-lh)/2+h-lh)/2,30,3));
temp=0;
for(int i=0;i<lh-20;i=temp*40){
temp++;
g.fillRect((w/2+lw)/2-3,i-40,6,20);//北左
g.fillRect(w/2+(w/2-lw)/2-3,i-40,6,20);//北右
g.fillRect((w/2+lw)/2-3,h-lh+i+40,6,20);
g.fillRect(w/2+(w/2-lw)/2-3,h-lh+i+40,6,20);
}
g.fill(this.getDirection(((w/2+lw)/2+lw)/2,20,30,1));
g.fill(this.getDirection(((w/2+lw)/2+lw)/2,h-lh+50,30,1));
g.fill(this.getDirection(((w/2+lw)/2+w/2)/2,20,30,1));
g.fill(this.getDirection(((w/2+lw)/2+w/2)/2,h-lh+50,30,1));
g.fill(this.getDirection((w/2+w/2+(w/2-lw)/2)/2,60,30,0));
g.fill(this.getDirection((w/2+w/2+(w/2-lw)/2)/2,h-lh+90,30,0));
g.fill(this.getDirection((w-lw+w/2+(w/2-lw)/2)/2,60,30,0));
g.fill(this.getDirection((w-lw+w/2+(w/2-lw)/2)/2,h-lh+90,30,0));
temp=0;
for(int i=0;i<h-2*lh-40;i=temp*20){
// i=lh+temp*20;
temp++;
g.fillRect(lw-30,i+lh+20,40,10);
g.fillRect(w-lw,i+lh+20,40,10);
}
temp=0;
for(int i=0;i<w-2*lw-40;i=temp*20){
// i=lh+temp*20;
temp++;
g.fillRect(i+lw+20,lh-40,10,40);
g.fillRect(i+lw+20,h-lh,10,40);
}
paintLight( g,30);
}
public static Polygon getDirection(int x,int y,int h,int dire){
float scl1=(float)1/12;//尾巴的1/2
float scl2=(float)1/6;//箭头台宽
float scl3=(float)1/3;//箭头的高度
if(dire==0){//上
Polygon pol=new Polygon();
pol.addPoint(x,y);pol.addPoint(Math.round(x+scl1*h),y);
pol.addPoint(Math.round(x+scl1*h),Math.round(y-(h-scl3*h)));//3
pol.addPoint(Math.round(x+scl1*h+scl2*h),Math.round(y-(h-scl3*h)));//4
pol.addPoint(x,(y-h));//5
pol.addPoint(Math.round(x-scl1*h-scl2*h),Math.round(y-(h-scl3*h)));//6
pol.addPoint(Math.round(x-scl1*h),Math.round(y-(h-scl3*h)));//7
pol.addPoint(Math.round(x-scl1*h),y);//8
return pol;
}
if(dire==1){//下
Polygon pol=new Polygon();
pol.addPoint(x,y);pol.addPoint(Math.round(x-scl1*h),y);
pol.addPoint(Math.round(x-scl1*h),Math.round(y+(h-scl3*h)));//3
pol.addPoint(Math.round(x-scl1*h-scl2*h),Math.round(y+h-scl3*h));//4
pol.addPoint(x,(y+h));//5
pol.addPoint(Math.round(x+scl1*h+scl2*h),Math.round(y+(h-scl3*h)));//6
pol.addPoint(Math.round(x+scl1*h),Math.round(y+(h-scl3*h)));//7
pol.addPoint(Math.round(x+scl1*h),y);//8
return pol;
}
if(dire==2){//左
Polygon pol=new Polygon();
pol.addPoint(x,y);pol.addPoint(x,Math.round(y-scl1*h));
pol.addPoint(Math.round(x-(h-scl3*h)),Math.round(y-scl1*h));//3
pol.addPoint(Math.round(x-(h-scl3*h)),Math.round(y-scl1*h-scl2*h));//4
pol.addPoint(x-h,y);//5
pol.addPoint(Math.round(x-(h-scl3*h)),Math.round(y+scl1*h+scl2*h));//6
pol.addPoint(Math.round(x-(h-scl3*h)),Math.round(y+scl1*h));//7
pol.addPoint(x,Math.round(y+scl1*h));//8
return pol;
}
if(dire==3){//右
Polygon pol=new Polygon();
pol.addPoint(x,y);pol.addPoint(x,Math.round(y+scl1*h));
pol.addPoint(Math.round(x+ (h-scl3*h)),Math.round(y+scl1*h));//3
pol.addPoint(Math.round(x+(h-scl3*h)),Math.round(y+scl1*h+scl2*h));//4
pol.addPoint(x+h,y);//5
pol.addPoint(Math.round(x+(h-scl3*h)),Math.round(y-scl1*h-scl2*h));//6
pol.addPoint(Math.round(x+(h-scl3*h)),Math.round(y-scl1*h));//7
pol.addPoint(x,Math.round(y-scl1*h));//8
return pol;
}
return null;
}
}
5.
package heima_alai;
import javax.swing.JTable;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.Component;
import javax.swing.table.DefaultTableCellRenderer;
public class AlaiTableRend extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
super.setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
}
else {
if(table.getModel().getValueAt(row,2).toString().equals(Light.YELLOW)){
super.setBackground(Color.yellow);
super.setForeground(table.getForeground());}
if(table.getModel().getValueAt(row,2).toString().equals(Light.RED)){
super.setBackground(Color.red);//new Color(102,193,123)
super.setForeground(table.getForeground());
}
if(table.getModel().getValueAt(row,2).toString().equals(Light.GREEN)){
super.setBackground(Color.GREEN);//new Color(102,193,123)
super.setForeground(table.getForeground());
}
}
setFont(table.getFont());
if (hasFocus) {
setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
if (!isSelected && table.isCellEditable(row, column)) {
Color col;
col = UIManager.getColor("Table.focusCellForeground");
if (col != null) {
super.setForeground(col);
}
col = UIManager.getColor("Table.focusCellBackground");
if (col != null) {
super.setBackground(col);
}
}
} else {
setBorder(noFocusBorder);
}
setValue(value);
return this;
}
}
6.
package heima_alai;
public class AlaiString {
String s=null;
public AlaiString(String ss) {
s=ss;
}
public String toString(){
return s;
}
}
7.
package heima_alai;
public class AlaiString2 extends AlaiString{
public AlaiString2(String ss) {
super(ss);
}
}
8.
package heima_alai;
public class AlaiString3 extends AlaiString{
public AlaiString3(String ss) {
super(ss);
}
}
9.
package heima_alai;
import javax.swing.JFrame;
import javax.swing.JTable;
import java.awt.Rectangle;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.DefaultCellEditor;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.geom.Rectangle2D;
import java.awt.TexturePaint;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.GridLayout;
import java.awt.Dimension;
import javax.swing.UIManager;
import java.util.Vector;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
public class MainFrame extends JFrame implements DocumentListener{
static{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
System.err.println("Error loading L&F: " + exc);
}
}
BufferedImage image1=new BufferedImage(800,600,BufferedImage.TYPE_INT_ARGB);
TexturePaint t;
BufferedImage texureImage;
LightTableMode mode;
JComboBox jComboBox1 = new JComboBox(new String[]{"东","西","南","北"});
DefaultCellEditor kuCell=new DefaultCellEditor(jComboBox1);
JComboBox jComboBox2 = new JComboBox(new String[]{"通行","直行","左行","调头","右行"});
DefaultCellEditor kuCel2=new DefaultCellEditor(jComboBox2);
JComboBox jComboBox3 = new JComboBox(new String[]{Light.RED,Light.GREEN,Light.YELLOW});
DefaultCellEditor kuCel3=new DefaultCellEditor(jComboBox3);
Control cot=new Control(){
public void disposeEx(){
mode.fireTableDataChanged();
jPanel1.repaint();
jPanel3.repaint();
}
};
Road road=new Road(cot);
public MainFrame() {
mode=new LightTableMode(cot.lights){
public void updateJpanel(){
jPanel1.repaint();
jPanel3.repaint();
}
};
cot.start();
try {
texureImage=ImageIO.read(new File("res\\马路2.png"));
Rectangle2D anc=new Rectangle2D.Double(0,0,texureImage.getWidth()/60 ,texureImage.getHeight()/60);
t=new TexturePaint(texureImage,anc);
// Graphics2D g=image1.createGraphics();
// g.fillRect(0,0,image1.getWidth(),image1.getHeight());
// g.dispose();
jbInit();
System.out.println(jTable1.rowAtPoint(new Point(10,0)));
} catch (Exception ex) {
ex.printStackTrace();
}
}
int left=0;
int btom=10;
public void render(Graphics2D g){
g.setColor(jPanel1.getBackground());
g.fillRect(0,0,jPanel1.getWidth(),jPanel1.getHeight());
g.setColor(jPanel1.getForeground());
int cycle=cot.getCycle();
int w=jPanel1.getWidth();
float t=(float)(w-20)/cycle;
g.drawLine(left,20,left,jPanel1.getHeight() );
g.drawLine(left,20,w,20 );
//g.drawString("0",1,jPanel1.getHeight());
int t2=5;if(cycle>100)t2=10;
for(int i=0;i<=cycle;i++ ){
if(i%t2==0){
g.drawString(i+"",Math.round(left+i*t-4),10);
g.drawLine(Math.round(left+i*t),Math.round(20),Math.round(left+i*t),20-8 );
}else{
g.drawLine(Math.round(left+i*t),Math.round(20),Math.round(left+i*t),20-4 );
}
}
Rectangle rec=jScrollPane1.getViewport().getViewRect();
int h=jTable1.getRowHeight();
int rowC=mode.getRowCount();
if(mode.getRowCount()>=rec.height/h)rowC=rec.height/h;
for(int i=0;i<rowC;i++){
int row=jTable1.rowAtPoint(new Point(10,rec.y+h*i));
int realRowInMode=jTable1.convertRowIndexToModel(row);
Light lig=(Light) cot.getLights().get(realRowInMode);
lig.render(g,h*i+25,h/2,t);
}
Color c=g.getColor();
g.setColor(Color.blue);
g.drawLine(Math.round(cot.nowTime*t+left),0,Math.round(cot.nowTime*t+left),jPanel1.getHeight()-btom);
g.setColor(c);
}
public static void main(String[] args) {
MainFrame mainframe = new MainFrame();
}
private void jbInit() throws Exception {
this.setTitle("heima_钱凤来 可任意生成交通规则的试例 可以用EXCEL打开当前目录res/红绿灯.TXT 进行交通规则编辑,或直接下面的table中编辑");
this.getContentPane().setLayout(borderLayout1);
jButton1.setText("增加");
jButton1.addActionListener(new MainFrame_jButton1_actionAdapter(this));
jPanel2.setLayout(borderLayout2);
jButton2.addActionListener(new MainFrame_jButton2_actionAdapter(this));
jTable1.setRowHeight(20);
jPanel4.setLayout(gridLayout1);
gridLayout1.setColumns(2);
jPanel3.setPreferredSize(new Dimension(10, 10));
jScrollPane1.setPreferredSize(new Dimension(452, 164));
jButton3.setText("启动");
jButton3.addActionListener(new MainFrame_jButton3_actionAdapter(this));
jButton4.setText("停止");
jButton4.addActionListener(new MainFrame_jButton4_actionAdapter(this));
jLabel1.setText("时间间隔");
jTextField1.setMaximumSize(new Dimension(100, 100));
this.getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
jButton2.setText("删除");
jToolBar1.add(jButton1);
jToolBar1.add(jButton2);
jToolBar1.add(jButton3);
jToolBar1.add(jButton4);
jToolBar1.add(jTextField1);
jToolBar1.add(jLabel1);
jPanel4.add(jScrollPane1);
jPanel4.add(jPanel1);
this.getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH);
jScrollPane1.getViewport().add(jTable1);
jPanel2.add(jPanel3, java.awt.BorderLayout.CENTER);
jPanel2.add(jPanel4, java.awt.BorderLayout.NORTH);
jPanel1.setBackground(Color.white);
jTable1.setModel(mode);
jTable1.setDefaultEditor(AlaiString.class,kuCell);
jTable1.setDefaultEditor(AlaiString2.class,kuCel2);
jTable1.setDefaultEditor(AlaiString3.class,kuCel3);
jTable1.setDefaultRenderer(AlaiString3.class,new AlaiTableRend());
jTextField1.getDocument().addDocumentListener(this);
jTextField1.setText(cot.st+"");
this.setSize(885,685);
this.setVisible(true);
}
JPanel jPanel1 = new JPanel(){ public void paintComponent(Graphics g){
render((Graphics2D)g);
}
};
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();
JToolBar jToolBar1 = new JToolBar();
JButton jButton1 = new JButton();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel2 = new JPanel();
JButton jButton2 = new JButton();
JPanel jPanel3 = new JPanel(){
public void paintComponent(Graphics g){
( (Graphics2D)g).setPaint(t);
g.fillRect(0,0,this.getWidth(),this.getHeight());
road.rend(( (Graphics2D)g),0,0,this.getWidth(),this.getHeight());
}
};
BorderLayout borderLayout2 = new BorderLayout();
JPanel jPanel4 = new JPanel();
GridLayout gridLayout1 = new GridLayout();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JTextField jTextField1 = new JTextField();
JLabel jLabel1 = new JLabel();
public void jButton1_actionPerformed(ActionEvent e) {
Vector<int[]> v=new Vector();
v.add(new int[]{0,0});
mode.insertRow(jTable1.getSelectedRow(),new Light( v,"red"));
jPanel1.repaint();
jPanel3.repaint();
}
public void jButton2_actionPerformed(ActionEvent e) {
mode.removeRow(jTable1.getSelectedRow());
jPanel1.repaint();
jPanel3.repaint();
}
public void jButton3_actionPerformed(ActionEvent e) {
cot.isRun=true;
}
public void jButton4_actionPerformed(ActionEvent e) {
cot.isRun=false;
}
public void insertUpdate(DocumentEvent e) {
try{
int s=Integer.parseInt(jTextField1.getText());
cot.st=s;
}catch(Exception e2){}
}
public void removeUpdate(DocumentEvent e) {
try{
int s=Integer.parseInt(jTextField1.getText());
cot.st=s;
}catch(Exception e2){}
}
public void changedUpdate(DocumentEvent e) {
}
}
class MainFrame_jButton4_actionAdapter implements ActionListener {
private MainFrame adaptee;
MainFrame_jButton4_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton4_actionPerformed(e);
}
}
class MainFrame_jButton3_actionAdapter implements ActionListener {
private MainFrame adaptee;
MainFrame_jButton3_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
class MainFrame_jButton2_actionAdapter implements ActionListener {
private MainFrame adaptee;
MainFrame_jButton2_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class MainFrame_jButton1_actionAdapter implements ActionListener {
private MainFrame adaptee;
MainFrame_jButton1_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
好了今天日记就写道这里
---------------------- android培训、java培训、期待与您交流! ---------------------- 详细请查看:http://edu.csdn.net/heima