收藏 分享(赏)

马俊-兰大java实验-09.doc

上传人:dreamzhangning 文档编号:2306501 上传时间:2018-09-10 格式:DOC 页数:7 大小:49KB
下载 相关 举报
马俊-兰大java实验-09.doc_第1页
第1页 / 共7页
马俊-兰大java实验-09.doc_第2页
第2页 / 共7页
马俊-兰大java实验-09.doc_第3页
第3页 / 共7页
马俊-兰大java实验-09.doc_第4页
第4页 / 共7页
马俊-兰大java实验-09.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

1、/AppletPrg.javaimport java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;public class AppletPrg extends JApplet implements ActionListenerJPanel centerPanel,northPanel;JButton b1,b2;CardLayout card;public void init()setLayout(new BorderLayout();centerPanel=new JPanel();north

2、Panel=new JPanel();card=new CardLayout();centerPanel.setLayout(card);Circle circle=new Circle();Trangle trangle=new Trangle();centerPanel.add(“trangle“,trangle);centerPanel.add(“circle“,circle);b1=new JButton(“计算圆面积 “);b2=new JButton(“计算三角形面积 “);b1.addActionListener(this);b2.addActionListener(this);

3、northPanel.add(b1);northPanel.add(b2);add(centerPanel,BorderLayout.CENTER);add(northPanel,BorderLayout.NORTH);public void actionPerformed(ActionEvent e)if(e.getSource()=b1)card.show(centerPanel,“circle“);else if(e.getSource()=b2)card.show(centerPanel,“trangle“);class Circle extends JPanel implements

4、 ActionListenerdouble r,s;JTextField rad=null,area=null;JButton b=null;Circle()rad=new JTextField(10);area=new JTextField(10);b=new JButton(“确定“);add(new JLabel(“输入半径 “);add(rad);add(new JLabel(“面积是“);add(area);add(b);b.addActionListener(this);public void actionPerformed(ActionEvent e)tryr=Integer.p

5、arseInt(rad.getText();s=Math.PI*r*r;area.setText(“+s);catch(Exception ee)rad.setText(“请输入数字字符“);class Trangle extends JPanel implements ActionListenerdouble a=0,b=0,c=0,s;JTextField ba=new JTextField(6),bb=new JTextField(6),bc=new JTextField(6),area=new JTextField(24);JButton button=new JButton(“确定“

6、);Trangle()add(new JLabel(“输入三边的长度 “);add(ba);add(bb);add(bc);add(new JLabel(“面积是“);add(area);add(button);button.addActionListener(this);public void actionPerformed(ActionEvent e)trya=Double.parseDouble(ba.getText();b=Double.parseDouble(bb.getText();c=Double.parseDouble(bc.getText();if(a+bcs=Math.sq

7、rt(p*(p-a)*(p-b)*(p-c);area.setText(“+s);elsearea.setText(“您输入的数字不能形成三角形“); catch(Exception ee)area.setText(“请输入数字字符“);/Flower.javaimport java.awt.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.*;public class Flower extends Canvaspublic void paint(Graphics g)Graphics2D g_2d=(Gra

8、phics2D)g;QuadCurve2D curve_1=new QuadCurve2D.Double(200,200,150,160,200,100);CubicCurve2D curve_2=new CubicCurve2D.Double(200,200,260,145,190,120,200,100);Line2D line=new Line2D.Double(200,200,200,100);QuadCurve2D leaf_line1=new QuadCurve2D.Double(200,180,195,175,190,170);QuadCurve2D leaf_line2=new

9、 QuadCurve2D.Double(200,180,210,175,220,170);QuadCurve2D leaf_line3=new QuadCurve2D.Double(200,160,195,155,190,150);QuadCurve2D leaf_line4=new QuadCurve2D.Double(200,160,214,155,220,150);AffineTransform trans=new AffineTransform();for(int i=0;i6;i+)trans.rotate(60*Math.PI/180,200,200);g_2d.setTransf

10、orm(trans);GradientPaint gradient_1=new GradientPaint (200,200,Color.green,200,100,Color.yellow);g_2d.setPaint(gradient_1);g_2d.fill(curve_1);GradientPaint gradient_2=new GradientPaint (200,145,Color.green,260,145,Color.red,true);g_2d.setPaint(gradient_2);g_2d.fill(curve_2);Color c3=new Color(0,200,

11、0);g_2d.setColor(c3);g_2d.draw(line);g_2d.draw(leaf_line1);g_2d.draw(leaf_line2);g_2d.draw(leaf_line3);g_2d.draw(leaf_line4);QuadCurve2D center_curve_1=new QuadCurve2D.Double(200,200,190,185,200,180);AffineTransform trans_1=new AffineTransform();for(int i=0;i12;i+)trans_1.rotate(30*Math.PI/180,200,2

12、00);g_2d.setTransform(trans_1);GradientPaint gradient_3=new GradientPaint(200,200,Color.red,200,180,Color.yellow);g_2d.setPaint(gradient_3);g_2d.fill(center_curve_1);AffineTransform trans_2=new AffineTransform();trans_2.scale(0.4,0.4);for(int i=0;i6;i+)trans_2.rotate(60*Math.PI/180,200,200);g_2d.set

13、Transform(trans_2);g_2d.setColor(Color.pink);g_2d.fill(curve_1);g_2d.setColor(Color.green);g_2d.fill(curve_2);public static void main(String args)Flower myflower=new Flower();JFrame myapp=new JFrame(“Java2D 图形测试“);myapp.setSize(400,400);myapp.add(myflower);myapp.setVisible(true);myapp.setDefaultClos

14、eOperation(JFrame.EXIT_ON_CLOSE);/GTest.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class GTest extends JFramepublic static void main(String args)new GTest().show();public GTest()setBackground(Color.white);setTitle(“图形示例“);setSize(300,180);addWindowListener(new WindowAd

15、apter()public void windowClosing(WindowEvent evt)setVisible(false);System.exit(0););Container contentPane=getContentPane();contentPane.add(new GTestPanel();class GTestPanel extends JPanelpublic void paintComponent(Graphics g)Font font=new Font(“隶书“,Font.PLAIN+Font.BOLD,24);g.setFont(font);g.setColor

16、(Color.blue);g.fillRect(40,40,80,50);g.drawString(“矩形“,50,110);g.setColor(Color.green);g.fillOval(170,40,80,50);g.drawString(“椭圆形“,194,110);/JiechengApplet.javaimport java.applet.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class JiechengApplet extends JApplet implements A

17、ctionListenerJButton button;JTextField number;int jiecheng=1;public void init()setBackground(Color.yellow);setLayout(new FlowLayout();number=new JTextField(“1“,8);button=new JButton(“求阶乘“);button.addActionListener(this);add(number);add(button);public void actionPerformed(ActionEvent e)int m=Integer.

18、parseInt(number.getText();jiecheng=1;for(int i=1;i=m;i+)jiecheng=jiecheng*i;repaint();public void paint(Graphics g)g.setColor(Color.blue);g.drawString(“学习 Java Applet“,80,60);g.setColor(Color.yellow);g.setColor(Color.red);g.fillRect(60,80,80,100);g.drawString(“jiecheng=“+jiecheng,80,100);/Star.javai

19、mport java.awt.*;import javax.swing.*;public class Star extends Canvasint pointX=new int5;int pointY=new int5;public void paint(Graphics g)g.translate(100,100);pointX0=0;pointY0=80;double arcAngle=(72*Math.PI)/180;for(int i=1;i5;i+)pointXi=(int)(pointXi-1*Math.cos(arcAngle)-pointYi-1*Math.sin(arcAng

20、le);pointYi=(int)(pointYi-1*Math.cos(arcAngle)+pointXi-1*Math.sin(arcAngle); g.setColor(Color.red);int starX=pointX0,pointX2,pointX4,pointX1,pointX3,pointX0;int starY=pointY0,pointY2,pointY4,pointY1,pointY3,pointY0;g.drawPolygon(starX,starY,6);public static void main(String agrs)JFrame myapp=new JFrame(“绘制五角星“);Star mstart=new Star();myapp.setSize(200,200);myapp.add(mstart);myapp.setVisible(true);myapp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 大学课件

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报