博文
Java基础计算(2007-12-16 21:45:00)
摘要:一个小程序,自行编写一个HTML文件嵌入运行就行了。
import java.awt.*;import java.awt.event.*;import java.applet.*;public class Add extends Applet implements ActionListener{Label ask,result;TextField answer;Button submit;int a=6,b=9; public static void main(String args[]) {Add a=new Add(); a.init(); } public void init() {Panel p=new Panel(); setLayout(new BorderLayout()); p.setLayout(null); ask=new Label(Integer.toString(a)+"+"+Integer.toString(b)); ask.setFont(new Font("宋体",Font.BOLD,16)); answer=new TextField(5); result=new Label(); submit=new Button("OK"); submit.addActionListener(this); p.add(ask);p.add(answer);p.add(result);p.add(submit); ask.setBounds(50,20,70,20);answer.setBounds(160,20,70,20); result.setBounds(50,60,90,20);submit.setBounds(160,60,70,20); add("Center",p);p.setVisible(true); } public void actionPerformed(ActionEvent e) {try {if(Integer.valueOf(answer.getText())==a+......
渔网(2007-12-14 22:44:00)
摘要:给大家看张渔网,运行的时候请设置好图形驱动的路径。
#include <graphics.h>#include <math.h>
void ellip(int x0,int y0,int a,int b,int dt){int x,y,n,i; float t1,t=0; t1=dt*0.0174533; n=360/dt; moveto(x0+a,y0); for(i=1;i<n;i++) {t=t+t1; x=x0+a*cos(t); y=y0+b*sin(t); lineto(x,y); } lineto(x0+a,y0);}
main(){int i,a=200,x=320,y=200; int driver=DETECT,mode; registerbgidriver(EGAVGA_driver); initgraph(&driver,&mode,""); cleardevice(); setbkcolor(9); setcolor(4); for(i=0;i<=200;i=i+10) {ellip(x,y,a-i,i,10); } getch(); closegraph();}......
C画函数曲线(2007-12-14 22:35:00)
摘要:这是一个函数曲线的描绘问题,可根据注释修改程序,以画出不同的曲线。
#include<graphics.h>#define y(x) x*x /*在这里修改曲线方程,此处为x的平方*/#define xs(x) x0+x*s /*这里为x方向的坐标转换*/#define ys(x) y0-y(x)*s /*这里为y方向的坐标转换*/
main(){float x0,y0,xl,xr,s,x,dx; int driver=DETECT,mode; printf("Please input the x and y of new location:"); scanf("%f %f",&x0,&y0); /*输入要画曲线的坐标系原点坐标*/ printf("Please input the multiply:"); scanf("%f",&s); /*曲线放大倍数,以几十为效果最佳*/ printf("Please input the left edge and the right edge and dx:"); scanf("%f %f %f",&xl,&xr,&dx); /*输入曲线的自变量上下界xl和xr以及自变量的递增步长*/ registerbgidriver(EGAVGA_driver); initgraph(&driver,&mode,""); line(x0,getmaxy(),x0,0); line(0,y0,getmaxx(),y0); outtextxy(x0,y0,"0,0"); setcolor(14); while(ys(xl)<0)xl+=dx; moveto(xs(xl),ys(xl)); for(x=xl;x<=xr;x+=dx) {if(x>xl&&ys(x)>=0) {putpixel(xs(x),ys(x),14); lineto(xs(x),......
C图形学中的错切演示程序(2007-12-14 22:15:00)
摘要:此程序用到两个文件,一个是被调用程序affine.c,另一个是主程序cuoqie.c,在运行前请注意自己设置好图形驱动程序的路径。
affine.c如下:
double sin(),cos();double xmax=639.0,ymax=399.0;double f[3][3],xx,yy;
scx(xj)double xj;{int x; x=(int)(xj+xmax/2); return(x);}
scy(yj)double yj;{int y; y=(int)(ymax-(yj+(ymax/2))); return(y);}
parallel(dx,dy)double dx,dy;{f[0][0]=1.0;f[0][1]=0.0;f[0][2]=0.0; f[1][0]=0.0;f[1][1]=1.0;f[1][2]=0.0; f[2][0]=dx;f[2][1]=dy;f[2][2]=1.0;}
rotate(theta)double theta;{double th; th=theta/180*3.1415927; f[0][0]=cos(th);f[0][1]=sin(th);f[0][2]=0.0;f[1][0]=-sin(th); f[1][1]=cos(th);f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;}
scale(s)double s;{f[0][0]=s;f[0][1]=0.0;f[0][2]=0.0; f[1][0]=0.0;f[1][1]=s;f[1][2]=0.0; f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;}
taisho_x(){f[0][0]=1.0;f[0][1]=0.0;f[0][2]=0.0; f[1][0]=0.0;f[1][1]=-1;f[1][2]=0.0; f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;}
taisho_y(){f[0][0]=-1.0;f[0][1]=0.0;f[0][2]=0.0; f[1][0]=0.0;f[1][1]=1.0;f[1][2]=0.0;......
Java画布画圆(2007-12-14 22:07:00)
摘要:这个程序很简单,没有什么好说的了,希望大家多多指教一下,在JDK1.6下通过。
import java.awt.*;import java.awt.event.*;import java.util.*;public class Draw extends WindowAdapter implements ActionListener,WindowListener{Canvas c;TextField radius,x,y;Button b; public static void main(String args[]) {Draw d=new Draw(); Frame f=new Frame("画布画圆"); d.c=new Canvas(); d.x=new TextField(5); d.y=new TextField(5); d.radius=new TextField(5); d.b=new Button("清空画布"); Label x1=new Label("请输入左上角x:"); Label y1=new Label("请输入左上角y:"); Label radius1=new Label("请输入半径:"); Panel p=new Panel(); f.setLayout(new BorderLayout()); p.setLayout(new FlowLayout()); f.add("Center",d.c); f.add("South",p); p.add(radius1);p.add(d.radius);p.add(x1);p.add(d.x); p.add(y1);p.add(d.y);p.add(d.b); d.x.addActionListener(d); d.y.addActionListener(d); d.radius.addActionListener(d); d.b.addActionListener(d); f.addWindowListener(d);&......
是非程序(2007-12-13 08:11:00)
摘要:此程序在JDK1.6环境下一切正常,甚至可以不添加ButtonGroup也可以使用JRadioButton,但是至少在JDK1.4中是不正确的,需要明确使用容器,在不同的版本之间已经有了语法上的不同,而且也充分体现了教学中与实际中的的确确的脱节,还是跟上时代的好啊。
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Check extends JFrame implements ActionListener{JRadioButton c1,c2;ButtonGroup b; public static void main(String args[]) {new Check(); } public Check() {super("单选按钮"); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); } }); b=new ButtonGroup(); c1=new JRadioButton("启用",true); c2=new JRadioButton("禁用",false); b.add(c1);b.add(c2); c1.setEnabled(false); setLayout(null);add(c1);add(c2); c1.setBounds(60,140,60,20);c2.setBounds(180,140,60,20); c1.addActionListener(this); c2.addActionListener(this); setVisible(true); setSize(300,240); } public void actionPerformed(ActionEvent e) {if(e.getSource(......
Java中Menu的简单使用(2007-12-13 08:02:00)
摘要:在一个JFrame上使用Menu,Menu使用要先添加MenuBar,Menu加到MenuBar上,MenuItem加到Menu上,如此级联添加。
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Me extends JFrame implements ActionListener{MenuBar mb;Menu m;MenuItem mi1,mi2,mi3; public Me() {addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); } }); mb=new MenuBar(); m=new Menu("文件"); mi1=new MenuItem("功能1"); mi2=new MenuItem("功能2"); mi3=new MenuItem("退出"); m.add(mi1);m.add(mi2);m.addSeparator();m.add(mi3);mb.add(m); mi1.addActionListener(this); mi2.addActionListener(this); mi3.addActionListener(this); setMenuBar(mb); setSize(320,240);setVisible(true); } public static void main(String args[]) {new Me(); } public void actionPerformed(ActionEvent e) {if(e.getSource()==mi1)setTitle("功能1"); else if(e.getSource()==mi2)setTitle("功能2"); else ......
Java中布局的简单使用(2007-12-13 07:58:00)
摘要:本例是为了体现Java中布局的使用,在一个Frame上添加了两个Panel,在两个Panel上分别体现了一个Butoon,一个Label和一个TextField的边框布局和网格布局,而Frame本身使用了网格布局。
import java.awt.*;import java.awt.event.*;
class TwoLayout extends WindowAdapter implements WindowListener,ActionListener{Frame f;Panel pborder,pgrid; Label lborder,lgrid;TextField tborder,tgrid;Button bborder,bgrid; public static void main(String args[]) {TwoLayout a=new TwoLayout(); a.execute(); } public void execute() {f=new Frame("布局"); f.setLayout(new GridLayout(2,1)); f.addWindowListener(this); pborder=new Panel();pgrid=new Panel(); f.add(pborder);f.add(pgrid); pborder.setLayout(new BorderLayout()); pgrid.setLayout(new GridLayout(3,1)); lborder=new Label();tborder=new TextField();bborder=new Button("边框布局复制"); lgrid=new Label();tgrid=new TextField();bgrid=new Button("网格布局复制"); lborder.setBackground(Color.yellow);lgrid.setBackground(Color.yellow); pborder.add("East",bborder);pgri......
Java中awt的简单使用(2007-12-13 07:51:00)
摘要:这是Java中简单的java.awt包的使用,并且使用了监听接口,只是作为一个程序思想的展示,没有什么高深之处。
import java.awt.*;import java.awt.event.*;
class ChangeColor extends WindowAdapter implements WindowListener,ActionListener{Frame f;Button r,b,y; public static void main(String args[]) {ChangeColor c=new ChangeColor(); c.execute(); } public void execute() {f=new Frame("改变颜色"); f.setLayout(new FlowLayout()); f.addWindowListener(this); r=new Button("红色"); b=new Button("蓝色"); y=new Button("黄色"); f.add(r);f.add(b);f.add(y); r.addActionListener(this); b.addActionListener(this); y.addActionListener(this); f.setSize(400,300);f.setVisible(true); } public void windowClosing(WindowEvent e) {System.exit(0); } public void actionPerformed(ActionEvent e) {if(e.getSource()==r)f.setBackground(Color.red); else if(e.getSource()==b)f.setBackground(Color.blue); else if(e.getSource()==y)f.setBackground(Color.yellow);&......
Java中接口的简单使用(2007-12-13 07:45:00)
摘要:接口和C++中的抽象类差不多,是只能被实现但是不能在自身实现某种方法的一种结构,下面的程序是一个Interface的简单应用,功能是实现一个数组中几个数据的升序排序和求最大值,实际上排序后最后一个数组元素即为最大值,此处使用一个独立方法只为体现接口的用法,确有欠妥。
interface vir //这是一个接口,用interface关键字声明{void bigsmall(int a[]); //这里在接口里声明bigsmall和sort两个方法用来被实现它的类继承 void sort(int b[]);}
class function implements vir //这是一个来实现上面的接口的类,即implements(实现)接口 {public void bigsmall(int c[]) //实现接口的第一个函数,形式必须和接口中相同并且 {int i=0,small=c[0],big=c[0]; //前面要有public关键字 for(i=1;i<c.length;i++) {if(c[i]<small)small=c[i]; if(c[i]>big)big=c[i]; } System.out.print("The smallest number is: "+small+'\n'); System.out.print("The biggest number is: "+big+'\n'); } public void sort(int c[]) //这是实现接口的第二个函数,形式与与上相同,带public关键字 {int j=0,k=0,temp=0; //程序本身及算法没有什么可说之处了 for(j=0;j<4;j++) {for(k=j+1;k<5;k++) {if(c[j]>c[k]) {temp=c[j]; &......
