• 图形程序设计


    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);
    	}
    }
    
    
  • 相关阅读:
    iOS 统计项目代码行数
    iOS Xcode 8 快捷键 (注释 失效 处理)
    iOS NSSet 学习 “无序数组” & 去重 案例
    iOS 学习@autoreleasepool{}
    iOS 学习如何声明私有变量和私有方法
    iOS 系统认知 debug distribution release 和 #ifdef DEBUG
    iOS copy 和 mutableCopy 学习
    iOS 关于 Missing iOS Distribution signing identity for.... 等 打包 校验 出现的事故 处理经验
    iOS NSCoding 的学习 和 使用
    TeamViewer被发现用于(检测为)商业用途解决方案(绝对有效)
  • 原文地址:https://www.cnblogs.com/xgcl/p/14219436.html
Copyright © 2020-2023  润新知