zoukankan      html  css  js  c++  java
  • Java第二次作业第三题

    四叶玫瑰线的图形设计:当用鼠标拖拽改变窗口大小时,四叶玫瑰线会重新绘制

    package naizi;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class RoseJFrame extends JFrame implements ComponentListener
    {	
    private RoseCanvas rose;
    public RoseJFrame(){
    	this.setSize(600,400);
    	this.addComponentListener(this);  //注册框架窗口的组件事件监听器
    	rose = new RoseCanvas(Color.red);
    	this.getContentPane().add(rose,"Center");//将rose添加到中心区域;
    	this.setVisible(true);
    }
    
    public void componentResized(ComponentEvent e){//改变窗口大小时,处理事件的方法
       rose.repaint(); //重画
    }
    
    public void componentMoved(ComponentEvent e)    {  }
    public void componentHidden(ComponentEvent e)   {  }
    public void componentShown(ComponentEvent e)    {  }
    
    public static void main(String arg[]){
       new RoseJFrame();
    }
    }
    
    class RoseCanvas extends Canvas                            //画布组件
    {
    private Color color;                                   //颜色
    
    public RoseCanvas(Color color)
    {
       this.color=color;
    }
    
    public void paint(Graphics g){ //在Canvas上绘图的方法
       int x0,y0;                                         //原点坐标
       x0 = this.getWidth() /2;                           //宽度
       y0 = this.getHeight()/2; 
       g.setColor(color); //设置画线颜色
       g.drawLine(x0,0,x0,y0*2);//画线
       g.drawLine(0,y0,x0*2,y0);//画线
       int j=40;
       while (j<200) 
       {
           for (int i=0;i<1023;i++)
           {
               double angle = i*Math.PI/512;
               double radius = j*Math.sin(2*angle);
               int x =(int) Math.round(radius * Math.cos(angle) * 2);
               int y =(int) Math.round(radius * Math.sin(angle));
               g.fillOval(x0+x,y0+y,1,1);                 //画直径为1的圆就是一个点
           }
           j += 20;
       }
    }
    }  
    

    运行结果如图:

  • 相关阅读:
    leetcode: Search in Rotated Sorted Array
    leetcode: Linked List Cycle
    leetcode: Longest Valid Parentheses
    leetcode: Next Permutation
    leetcode: Substring with Concatenation of All Words
    leetcode: Divide Two Integers
    leetcode: Implement strStr()
    Spider Studio 新版本 (20131201)
    已知问题汇总 (2013-11-30)
    C#代码获取或设置Iframe中的HTML
  • 原文地址:https://www.cnblogs.com/zqm-sau/p/9807670.html
Copyright © 2011-2022 走看看