主题:[讨论]应用
buaacse3606
[专家分:0] 发布于 2007-11-25 00:02:00
本人有四个JAVA程序(见上传文件),希望做出一个界面,将Clock程序放在界面上,并设置三个按钮“计算”,“益智”,“游戏”,当鼠标点击按钮时,分别运行Caculator,blackvswhite,ufo_attack.
初学JAVA,希望各位大侠不令赐教!!!
http://upload.programfan.com/upfile/20071125142571.rar
最后更新于:2007-11-25 14:27:00
回复列表 (共23个回复)
沙发
buaacse3606 [专家分:0] 发布于 2007-11-25 13:41:00
clock(1)
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.awt.image.*;
class RotateG
{
private Graphics g;
private int ox = 0;
private int oy = 0;
private double radians = 0.0;
private double cos = 1.0;
private double sin = 0.0;
public RotateG(Graphics g)
{
this.g = g.create();
}
public void setOrigin(int x, int y)
{
ox = x;
oy = y;
}
public void setAngle(double a)
{
radians = (a * Math.PI) / 180;
cos = Math.cos(radians);
sin = Math.sin(radians);
}
public void setColor(Color c)
{
g.setColor(c);
}
public int rotate_x(int x, int y)
{
return ((int) (ox + x * cos - y * sin));
}
public int rotate_y(int x, int y)
{
return ((int) (oy + y * cos + x * sin));
}
public void drawLine(int x1, int y1, int x2, int y2)
{
g.drawLine(rotate_x(x1, y1),
rotate_y(x1, y1),
rotate_x(x2, y2),
rotate_y(x2, y2));
}
public void drawOval(int x, int y, int width, int height)
{
g.drawOval(x,y,width,height);
}
public void fillOval(int x, int y, int width, int height)
{
g.fillOval(x,y,width,height);
}
public void drawPolygon(int x[], int y[], int n)
{
int new_x[] = new int[n];
int new_y[] = new int[n];
for (int i = 0; i < n; i++)
{
new_x[i] = rotate_x(x[i], y[i]);
new_y[i] = rotate_y(x[i], y[i]);
}
g.drawPolygon(new_x, new_y, n);
}
public void fillPolygon(int x[], int y[], int n)
{
int new_x[] = new int[n];
int new_y[] = new int[n];
for (int i = 0; i < n; i++)
{
new_x[i] = rotate_x(x[i], y[i]);
new_y[i] = rotate_y(x[i], y[i]);
}
g.fillPolygon(new_x, new_y, n);
}
public void drawImage(Image img,int x, int y,ImageObserver observer)
{
g.drawImage(img,x,y,observer);
}
}
板凳
buaacse3606 [专家分:0] 发布于 2007-11-25 13:44:00
clock(2)
public class Clock extends Applet implements Runnable
{
private Date date;
private int iHour,iMinute,iSecond;
private Thread thread = null;
private Image offI,backG;
private Graphics offG;
private int ixHour[]={4,4,0,-4,-4}
,iyHour[]={0,-30,-35,-30,0}
,ixMinute[]={3,3,0,-3,-3}
,iyMinute[]={0,-45,-50,-45,0};
public void init()
{
this.setLayout(null);
date = new Date();
SimpleDateFormat df = new SimpleDateFormat("'现在时刻' hh:mm:ss");
String lasttime = df.format(date);
offI = createImage(300,300);
offG = offI.getGraphics();
backG = getImage(getCodeBase(),"Clock.jpg");
}
public void start()
{
if(thread == null)
{
thread = new Thread(this);
thread.start();
}
}
public void run()
{
RotateG g = new RotateG(offG);
while(true)
{
try
{
thread.sleep(1000);
date = new Date();
SimpleDateFormat df = new SimpleDateFormat("'现在时刻' hh:mm:ss");
String lasttime = df.format(date);
System.out.println(lasttime);
iSecond = date.getSeconds();
iMinute = date.getMinutes();
iHour = date.getHours();
offG.setColor(Color.white);
offG.fillRect(0,0,300,300);
g.setOrigin(0,0);
g.drawImage(backG,0,0,this);
g.setOrigin(107,107);
System.out.println(iHour);
g.setAngle(iHour*30+iMinute*0.5);
g.setColor(Color.green);
g.fillPolygon(ixHour,iyHour,5);
g.setAngle(iMinute*6);
g.setColor(Color.red);
g.fillPolygon(ixMinute,iyMinute,5);
g.setAngle(iSecond*6);
g.setColor(Color.blue);
g.drawLine(0,0,0,-65);
g.setColor(Color.black);
g.fillOval(102,102,10,10);
repaint();
}
catch(InterruptedException e){}
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(offI,0,0,this);
}
}
3 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 13:47:00
Caculator(1)
import java.awt.*;
import java.applet.*;
public class Calculator extends Applet
{
private TextField tfAnswer;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button bPoint,bEqual,bPlus,bMinus,bClear,bMulti,bDivision;
private String OperatorCurrent,OperatorPre;
private String ForeScreen,BackScreen;
private boolean isFloat = false;
public void init()
{
OperatorCurrent = new String("");
OperatorPre = new String("");
ForeScreen = new String("");
BackScreen = new String("");
this.setBackground(Color.gray);
this.setForeground(Color.blue);
setLayout(null);
tfAnswer = new TextField();
tfAnswer.setBounds(5,5,100,20);
this.add(tfAnswer);
bClear = new Button("C");
bClear.setBounds(5,30,20,20);
bClear.setBackground(Color.pink);
this.add(bClear);
bDivision = new Button("/");
bDivision.setBounds(30,30,20,20);
bDivision.setBackground(Color.green);
this.add(bDivision);
bMulti = new Button("*");
bMulti.setBounds(55,30,20,20);
bMulti.setBackground(Color.green);
this.add(bMulti);
bMinus = new Button("-");
bMinus.setBounds(80,30,20,20);
bMinus.setBackground(Color.green);
this.add(bMinus);
b7 = new Button("7");
b7.setBounds(5,55,20,20);
this.add(b7);
b8 = new Button("8");
b8.setBounds(30,55,20,20);
this.add(b8);
b9 = new Button("9");
b9.setBounds(55,55,20,20);
this.add(b9);
b4 = new Button("4");
b4.setBounds(5,80,20,20);
this.add(b4);
b5 = new Button("5");
b5.setBounds(30,80,20,20);
this.add(b5);
b6 = new Button("6");
b6.setBounds(55,80,20,20);
this.add(b6);
bPlus = new Button("+");
bPlus.setBounds(80,55,20,45);
bPlus.setBackground(Color.green);
this.add(bPlus);
b1 = new Button("1");
b1.setBounds(5,105,20,20);
this.add(b1);
b2 = new Button("2");
b2.setBounds(30,105,20,20);
this.add(b2);
b3 = new Button("3");
b3.setBounds(55,105,20,20);
this.add(b3);
b0 = new Button("0");
b0.setBounds(5,130,45,20);
this.add(b0);
bPoint = new Button(".");
bPoint.setBounds(55,130,20,20);
this.add(bPoint);
bEqual = new Button("=");
bEqual.setBounds(80,105,20,45);
bEqual.setBackground(Color.green);
this.add(bEqual);
}
4 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 13:48:00
Caculator(2)
public boolean action(Event e, Object o)
{
String s = new String("");
if(e.target instanceof Button)
{
if((e.target == b0)||(e.target == b1)||(e.target == b2)||
(e.target == b3)||(e.target == b4)||(e.target == b5)||
(e.target == b6)||(e.target == b7)||(e.target == b8)||
(e.target == b9)||(e.target == bPoint))
{
if(e.target != bPoint)
{
s = (String)o;
System.out.print(s);
doForeScreen(s);
}
if((e.target == bPoint)&&(!isFloat))
{
isFloat = true;
s = (String)o;
if(ForeScreen.equals(""))
{
ForeScreen += "0.";
repaint();
}
else
{
System.out.print(s);
doForeScreen(s);
}
}
}
if(e.target == bClear)
{
doClear();
}
if((e.target == bMulti)||(e.target == bDivision)||
(e.target == bPlus)||(e.target == bMinus))
{
if(ForeScreen != "")
{
OperatorCurrent = ((String)o);
doOperator();
System.out.print((String)o);
}
else
{
OperatorPre = ((String)o);
}
}
if(e.target == bEqual)
{
doOperator();
}
}
return true;
}
public void doOperator()
{
double dFore,dBack;
Double d;
if(OperatorPre.equals(""))
{
BackScreen = ForeScreen;
ForeScreen = "";
tfAnswer.setText(BackScreen);
repaint();
}
else
{
dFore = (new Double(ForeScreen)).doubleValue();
dBack = (new Double(BackScreen)).doubleValue();
ForeScreen = "";
BackScreen = tfAnswer.getText();
if(OperatorPre.equals("+"))
{
d = new Double((dBack + dFore));
tfAnswer.setText(d.toString());
BackScreen = d.toString();
repaint();
}
if(OperatorPre.equals("-"))
{
d = new Double((dBack - dFore));
tfAnswer.setText(d.toString());
BackScreen = d.toString();
repaint();
}
if(OperatorPre.equals("*"))
{
d = new Double((dBack * dFore));
tfAnswer.setText(d.toString());
BackScreen = d.toString();
repaint();
}
if(OperatorPre.equals("/"))
{
d = new Double((dBack / dFore));
tfAnswer.setText(d.toString());
BackScreen = d.toString();
repaint();
}
}
OperatorPre = OperatorCurrent;
repaint();
}
public void paint(Graphics g)
{
//tfAnswer.setText(ForeScreen);
}
public void doForeScreen(String s)
{
ForeScreen += s;
tfAnswer.setText(ForeScreen);
repaint();
}
public void doBackScreen(String s)
{
BackScreen = ForeScreen;
ForeScreen = "";
tfAnswer.setText(ForeScreen);
repaint();
}
public void doClear()
{
OperatorCurrent = "";
OperatorPre = "";
ForeScreen = "";
BackScreen = "";
isFloat = false;
tfAnswer.setText(ForeScreen);
repaint();
}
}
5 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 13:49:00
black vs white
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.PrintStream;
import java.net.URL;
public class BlackVSWhite extends Applet implements Runnable,ActionListener,MouseListener
{
private MediaTracker imageTracker;
private Image redImage;
private Image blueImage;
private Image offScrImage;
private Graphics offScrGraphics;
private Font f;
private int fontHeight;
private int fontAscent;
private Button button;
private int buttonHeight;
private Choice choice;
private Choice moveFirst;
private boolean redMoveFirst;
private boolean illegalMove;
private boolean blueNotMoving;
private boolean animating;
private int redScore;
private int blueScore;
private Thread aThread;
private static final int EMPTY = 0;
private static final int RED = 2;
private static final int BLUE = 3;
private static final int BLUE_TEMP = 4;
private static final int RED_TEMP = 5;
private static final int VERYGOOD = 9999;
private static int DIM = 10;
private int boardSize = 400;
private int[] grid = new int[400];
private synchronized void newGame()
{
DIM = (new Integer((String) choice.getSelectedItem())).intValue();
int whoMoveFirst=moveFirst.getSelectedIndex();
if(whoMoveFirst==0)
redMoveFirst = true;
else
redMoveFirst = false;
int n1;
int n2;
for( n1 = 0; n1 < DIM; ++n1 )
{
for( n2 = 0; n2 < DIM; ++n2 )
grid[n1 * DIM + n2] = EMPTY;
}
n2 = DIM / 2;
grid[(n2 - 1) * DIM + (n2 - 1)] = RED;
grid[(n2 - 1) * DIM + n2] = BLUE;
grid[n2 * DIM + (n2 - 1)] = BLUE;
grid[n2 * DIM + n2] = RED;
offScrGraphics.setColor( Color.white );
offScrGraphics.fillRect( 0, 0, boardSize, boardSize );
redScore = blueScore = 0;
illegalMove = blueNotMoving = animating = false;
boardSize = -1;
repaint();
if( !redMoveFirst )
{
blueMove();
checkScore();
repaint();
}
}
public synchronized void init()
{
aThread = null;
imageTracker = new MediaTracker( this );
offScrImage = createImage( 400, 400 );
offScrGraphics = offScrImage.getGraphics();
f = new Font( "TimesRoman", 0, 12 );
offScrGraphics.setFont( f );
fontHeight = getFontMetrics( f ).getHeight();
fontAscent = getFontMetrics( f ).getAscent();
loadImages();
if( button == null )
{
button = new Button( "新游戏" );
button.addActionListener(this);
setLayout( new FlowLayout( ) );
add( button );
choice = new Choice();
choice.addItem( "6" );
choice.addItem( "8" );
choice.addItem( "10" );
choice.addItem( "12" );
choice.addItem( "14" );
add( choice );
choice.select( "10" );
}
if( moveFirst == null )
{
moveFirst = new Choice();
moveFirst.addItem( "玩家先走" );
moveFirst.addItem( "电脑先走" );
add( moveFirst );
moveFirst.select( 0 );
redMoveFirst = true;
}
addMouseListener(this);
newGame();
}
public synchronized void loadImages()
{
int i;
try
{
redImage = getImage( getDocumentBase(), "p2.gif" );
blueImage = getImage( getDocumentBase(), "p1.gif" );
offScrGraphics.drawImage( redImage, 0, 0, this );
offScrGraphics.drawImage( blueImage, 0, 0, this );
}
catch( Exception e1 )
{
System.out.println( e1 );
}
imageTracker.addImage( redImage, 0 );
imageTracker.addImage( blueImage, 1 );
for( i = 0; i < 2; ++i )
{
try
{
imageTracker.checkID( i, true );
imageTracker.waitForID( i );
showStatus( "Loading image P" + i + ".gif" );
}
catch( Exception e2 )
{
System.out.println( e2 );
}
}
}
public void start()
{
aThread = new Thread( this );
aThread.start();
imageTracker.statusID( 0, true );
imageTracker.statusID( 1, true );
}
6 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 13:54:00
black vs white(2)
public void run()
{
for( ;; )
{
try
{
Thread.sleep( 50L );
continue;
}
catch( Exception unused1 )
{
continue;
}
}
}
/*public void stop()
{
if( aThread != null && aThread.isAlive() )
aThread.stop();
}*/
public void destroy()
{
}
public synchronized void blueMove()
{
int num1 = -1;
int num2 = -1;
int num3 = -1;
int num4 = -1;
int num5;
for( num5 = 0; num5 < DIM; ++num5 )
{
int num6;
for( num6 = 0; num6 < DIM; ++num6 )
{
if( grid[num5 * DIM + num6] == 0 )
{
num2 = isGood( num5, num6, 3, 2 );
if( num1 < num2 && num2 > 0 )
{
num1 = num2;
num3 = num5;
num4 = num6;
}
}
}
}
blueNotMoving = false;
if( num1 > 0 )
{
grid[num3 * DIM + num4] = BLUE;
change( num3, num4, 3, 2, 4 );
}
else
blueNotMoving = true;
}
public synchronized int isGood(int num1, int num2, int num3, int num4)
{
int num6 = 0;
int num5 = 0;
int num7;
int num8;
int num9;
int num10;
for( num8 = num2 + 1; num8 < DIM; ++num8 )
{
if( grid[num1 * DIM + num8] == num4 )
++num5;
else
{
if( grid[num1 * DIM + num8] == num3 )
break;
if( grid[num1 * DIM + num8] == 0 )
{
num5 = 0;
break;
}
}
}
if( num8 >= DIM )
num5 = 0;
num6 += num5;
num5 = 0;
for( num8 = num2 - 1; num8 >= 0; --num8 )
{
if( grid[num1 * DIM + num8] == num4 )
++num5;
else
{
if( grid[num1 * DIM + num8] == num3 )
break;
if( grid[num1 * DIM + num8] == 0 )
{
num5 = 0;
break;
}
}
}
7 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 13:57:00
black vs white(3)
if( num8 <= -1 )
num5 = 0;
num6 += num5;
num5 = 0;
for( num7 = num1 + 1; num7 < DIM; ++num7 )
{
if( grid[num7 * DIM + num2] == num4 )
++num5;
else
{
if( grid[num7 * DIM + num2] == num3 )
break;
if( grid[num7 * DIM + num2] == 0 )
{
num5 = 0;
break;
}
}
}
if( num7 >= DIM )
num5 = 0;
num6 += num5;
num5 = 0;
num7 = num1 - 1;
while( num7 >= 0 )
{
if( grid[num7 * DIM + num2] == num4 )
++num5;
if( grid[num7 * DIM + num2] == num3 )
break;
if( grid[num7 * DIM + num2] == 0 )
{
num5 = 0;
break;
}
else
--num7;
}
if( num7 <= -1 )
num5 = 0;
num6 += num5;
num5 = 0;
num9 = num2 + 1;
num10 = num1 - 1;
while( num9 >= 0 && num10 >= 0 )
{
if( grid[num10 * DIM + num9] == num4 )
++num5;
if( grid[num10 * DIM + num9] == num3 )
break;
if( grid[num10 * DIM + num9] == 0 )
{
num5 = 0;
break;
}
else
{
++num9;
--num10;
}
}
if( num9 >= DIM || num10 <= -1 )
num5 = 0;
num6 += num5;
num5 = 0;
num9 = num2 + 1;
for( num10 = num1 + 1; num9 < DIM && num10 < DIM; ++num10 )
{
if( grid[num10 * DIM + num9] == num4 )
++num5;
else
{
if( grid[num10 * DIM + num9] == num3 )
break;
if( grid[num10 * DIM + num9] == 0 )
{
num5 = 0;
break;
}
}
++num9;
}
if( num9 >= DIM || num10 >= DIM )
num5 = 0;
num6 += num5;
num5 = 0;
num9 = num2 - 1;
for( num10 = num1 - 1; num9 >= 0 && num10 >= 0; --num10 )
{
if( grid[num10 * DIM + num9] == num4 )
++num5;
else
{
if( grid[num10 * DIM + num9] == num3 )
break;
if( grid[num10 * DIM + num9] == 0 )
{
num5 = 0;
break;
}
}
--num9;
}
if( num9 <= -1 || num10 <= -1 )
num5 = 0;
num6 += num5;
num5 = 0;
num9 = num2 - 1;
for( num10 = num1 + 1; num9 < DIM && num10 < DIM; ++num10 )
{
if( grid[num10 * DIM + num9] == num4 )
++num5;
else
{
if( grid[num10 * DIM + num9] == num3 )
break;
if( grid[num10 * DIM + num9] == 0 )
{
num5 = 0;
break;
}
}
--num9;
}
if( num9 <= -1 || num10 >= DIM )
num5 = 0;
num6 += num5;
return num6;
}
8 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 13:59:00
black vs white(4)
public synchronized void change(int num1, int num2, int num3, int num4, int num5)
{
int num6 = 0;
int num8 = num2 + 1;
int num7;
int num9;
int num10;
int num11;
int num12;
int num13;
int num14;
int num15;
int num16;
int num17;
int num18;
int num19;
while( num8 < DIM )
{
num7 = num1 * DIM + num8;
if( grid[num7] != num4 )
{
if( grid[num7] == num3 )
num6 = 1;
break;
}
else
++num8;
}
if( num6 != 0 )
{
for( num9 = num2 + 1; num9 < DIM; ++num9 )
{
num7 = num1 * DIM + num9;
if( grid[num7] == num4 )
grid[num7] = num5;
else if( grid[num7] == num3 )
break;
}
}
num6 = 0;
num9 = num2 - 1;
while( num9 >= 0 )
{
num7 = num1 * DIM + num9;
if( grid[num7] != num4 )
{
if( grid[num7] == num3 )
num6 = 1;
break;
}
else
--num9;
}
if( num6 != 0 )
{
for( num10 = num2 - 1; num10 >= 0; --num10 )
{
num7 = num1 * DIM + num10;
if( grid[num7] == num4 )
grid[num7] = num5;
else if( grid[num7] == num3 )
break;
}
}
num6 = 0;
num10 = num1 + 1;
while( num10 < DIM )
{
num7 = num10 * DIM + num2;
if( grid[num7] != num4 )
{
if( grid[num7] == num3 )
num6 = 1;
break;
}
else
++num10;
}
if( num6 != 0 )
{
for( num11 = num1 + 1; num11 < DIM; ++num11 )
{
num7 = num11 * DIM + num2;
if( grid[num7] == num4 )
grid[num7] = num5;
else if( grid[num7] == num3 )
break;
}
}
num6 = 0;
num11 = num1 - 1;
while( num11 >= 0 )
{
num7 = num11 * DIM + num2;
if( grid[num7] != num4 )
{
if( grid[num7] == num3 )
num6 = 1;
break;
}
else
--num11;
}
if( num6 != 0 )
{
for( num12 = num1 - 1; num12 >= 0; --num12 )
{
num7 = num12 * DIM + num2;
if( grid[num7] == num4 )
grid[num7] = num5;
if( grid[num7] == num3 )
break;
}
}
9 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:00:00
black vs white(5)
num6 = 0;
num12 = num2 + 1;
num13 = num1 - 1;
while( num12 < DIM && num13 >= 0 )
{
num7 = num13 * DIM + num12;
if( grid[num7] != num4 )
{
if( grid[num7] == num3 )
num6 = 1;
break;
}
else
{
++num12;
--num13;
}
}
if( num6 != 0 )
{
num14 = num2 + 1;
for( num15 = num1 - 1; num14 < DIM && num15 >= 0; --num15 )
{
num7 = num15 * DIM + num14;
if( grid[num7] == num4 )
grid[num7] = num5;
if( grid[num7] == num3 )
break;
++num14;
}
}
num6 = 0;
num14 = num2 + 1;
num15 = num1 + 1;
while( num14 < DIM && num15 < DIM )
{
num7 = num15 * DIM + num14;
if( grid[num7] != num4 )
{
if( grid[num7] == num3 )
num6 = 1;
break;
}
else
{
++num14;
++num15;
}
}
if( num6 != 0 )
{
num16 = num2 + 1;
for( num17 = num1 + 1; num16 < DIM && num17 < DIM; ++num17 )
{
num7 = num17 * DIM + num16;
if( grid[num7] == num4 )
grid[num7] = num5;
if( grid[num7] == num3 )
break;
++num16;
}
}
num6 = 0;
num16 = num2 - 1;
num17 = num1 - 1;
while( num16 >= 0 && num17 >= 0 )
{
num7 = num17 * DIM + num16;
if( grid[num7] != num4 )
{
if( grid[num7] == num3 )
num6 = 1;
break;
}
else
{
--num16;
--num17;
}
}
if( num6 != 0 )
{
num18 = num2 - 1;
for( num19 = num1 - 1; num18 >= 0 && num19 >= 0; --num19 )
{
num7 = num19 * DIM + num18;
if( grid[num7] == num4 )
grid[num7] = num5;
if( grid[num7] == num3 )
break;
--num18;
}
}
num6 = 0;
num18 = num2 - 1;
num19 = num1 + 1;
while( num18 >= 0 && num19 < DIM )
{
num7 = num19 * DIM + num18;
if( grid[num7] != num4 )
{
if( grid[num7] == num3 )
num6 = 1;
break;
}
else
{
--num18;
++num19;
}
}
if( num6 != 0 )
{
int num20 = num2 - 1;
int num21;
for( num21 = num1 + 1; num20 >= 0 && num21 < DIM; ++num21 )
{
num7 = num21 * DIM + num20;
if( grid[num7] == num4 )
grid[num7] = num5;
if( grid[num7] == num3 )
break;
--num20;
}
}
animating = true;
}
10 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:03:00
black vs white(6)
public void checkScore()
{
int num2;
redScore = blueScore = 0;
for( num2 = 0; num2 < DIM; ++num2 )
{
int num3;
for( num3 = 0; num3 < DIM; ++num3 )
{
int num1 = num2 * DIM + num3;
if( grid[num1] == 2 || grid[num1] == 5 )
++redScore;
else if( grid[num1] == 3 || grid[num1] == 4 )
++blueScore;
}
}
}
我来回复