• 图形程序设计


    1.绘制几何图形

    package draw;
    
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class DrawTest
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(() ->
             {
                var frame = new DrawFrame();
                frame.setTitle("DrawTest");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
             });
       }
    }
    
    /**
     * A frame that contains a panel with drawings.
     */
    class DrawFrame extends JFrame
    {
       public DrawFrame()
       {      
          add(new DrawComponent());
          pack();
       }
    }
    
    /**
     * A component that displays rectangles and ellipses.
     */
    class DrawComponent extends JComponent
    {
       private static final int DEFAULT_WIDTH = 400;
       private static final int DEFAULT_HEIGHT = 400;
    
       public void paintComponent(Graphics g)
       {
          var g2 = (Graphics2D) g;
    
          // draw a rectangle
    
          double leftX = 100;
          double topY = 100;
          double width = 200;
          double height = 150;
    
          var rect = new Rectangle2D.Double(leftX, topY, width, height);
          g2.draw(rect);
    
          // draw the enclosed ellipse
    
          var ellipse = new Ellipse2D.Double();
          ellipse.setFrame(rect);
          g2.draw(ellipse);
    
          // draw a diagonal line
    
          g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height));
    
          // draw a circle with the same center
    
          double centerX = rect.getCenterX();
          double centerY = rect.getCenterY();
          double radius = 150;
    
          var circle = new Ellipse2D.Double();
          circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
          g2.draw(circle);
       }
       
       public Dimension getPreferredSize()
       {
          return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
       }
    }
    
    

    2.事件响应

    编写图形界面程序,接受用户输入的5个浮点数据和一个文件目录名,将这五个数据保存在该文件中。

    package test;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    
    import javax.swing.*;
    import java.io.*;
    import java.util.Arrays;
    @SuppressWarnings("unused")
    public class shiyan3 {	
    	public static void  main(String[] args) {
    		shiyan3Frame s3 = new shiyan3Frame();
    		s3.setVisible(true);
    	}
    }
    
    @SuppressWarnings("serial")
    class shiyan3Frame extends JFrame{
    	private JLabel lbl1;
    	private JLabel lbl2;
    	private JTextField text1;
    	private JTextField text2;
    	private JTextField text3;
    	private JTextField text4;
    	private JTextField text5;
    	private JTextField Text;
    	private JButton btn1;
    	private JButton btn2;
    	private String str;
    	shiyan3Frame(){
    		setTitle("实验3");
    		setSize(600, 400);
    		this.setLayout(new GridLayout(10,1));
    		text1 = new JTextField();
    		text2 = new JTextField();
    		text3 = new JTextField();
    		text4 = new JTextField();
    		text5 = new JTextField();
    		Text = new JTextField();
    		lbl1 = new JLabel("输入文件名:");
    		lbl2 = new JLabel("输入数字:");
    		btn1 = new JButton("打开文件");
    		btn2 = new JButton("发送数字");
    		btn1.addActionListener(new ActionListener(){  //添加监听器,单击打开文件的按钮后产生事件
    			public void actionPerformed(ActionEvent e){
    				str = Text.getText();
    				System.out.println(str);
    			}
    		});
    		btn2.addActionListener(new ActionListener(){  //添加监听器,单击打开文件的按钮后产生事件
    			@SuppressWarnings("deprecation")
    			public void actionPerformed(ActionEvent e){
    				String s1 = text1.getText()+" ";	
    				String s2 = text2.getText()+" ";
    				String s3 = text3.getText()+" ";
    				String s4 = text4.getText()+" ";
    				String s5 = text5.getText();
    				float[] f = new float[5];
    				try {
    //					System.out.println(str);
    					System.out.println(s1+s2+s3+s4+s5);
    					File outFile = new File(str);
    					FileOutputStream fos = new FileOutputStream(outFile);
    					DataOutputStream dos = new DataOutputStream(fos);
    					dos.writeBytes(s1);
    					dos.writeBytes(s2);
    					dos.writeBytes(s3);
    					dos.writeBytes(s4);
    					dos.writeBytes(s5);
    					dos.writeBytes("
    ");
    					dos.close();
    					fos.close();
    					
    					File inFile = new File(str);
    					FileInputStream fis = new FileInputStream(inFile);
    					DataInputStream dis = new DataInputStream(fis);
    					String str1,Str="";
    					while((str1 = dis.readLine())!=null) {
    						Str += str1;
    					}
    					String[] arr = Str.split(" ");
    					dis.close();
    					fis.close();
    					for (int i = 0; i <= 4; i++) {
    						f[i] = Float.parseFloat(arr[i]);
    					}
    					Arrays.sort(f);
    					s1 = f[4]+" ";
    					s2 = f[3]+" ";
    					s3 = f[2]+" ";
    					s4 = f[1]+" ";
    					s5 = f[0]+"";
    					
    					outFile = new File(str);
    					fos = new FileOutputStream(outFile,true);//加上true后表明是追加,不然就一直会覆盖
    					dos = new DataOutputStream(fos);
    					dos.writeBytes(s1);
    					dos.writeBytes(s2);
    					dos.writeBytes(s3);
    					dos.writeBytes(s4);
    					dos.writeBytes(s5);
    					dos.close();
    					fos.close();
    				}catch(FileNotFoundException e1) {
    					System.out.println("FileStream"+e1);
    				}catch(IOException e2) {
    					System.err.println("FileStream"+e2);
    				}
    			}
    		});
    		
    		
    		add(lbl1);
    		add(Text);
    		add(btn1);
    		add(lbl2);
    		add(text1);
    		add(text2);
    		add(text3);
    		add(text4);
    		add(text5);
    		add(btn2);
    	}
    }
    
    
  • 相关阅读:
    python文本处理
    转 Deep Learning for NLP 文章列举
    backpropagation
    开发者必备的6款源码搜索引擎 转
    转【面向代码】学习 Deep Learning(二)Deep Belief Nets(DBNs)
    Sampling
    各种形式的熵函数,KL距离
    [转] 如何在ie11里使用a连接创建动态下载文件流
    修改chrome背景色
    [转] 使用 Spring 5 的 WebFlux 开发反应式 Web 应用
  • 原文地址:https://www.cnblogs.com/xgcl/p/14219436.html
Copyright © 2020-2023  润新知