四葉玫瑰線的圖形設計JAVA代碼__JAVA

來源:互聯網
上載者:User

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RoseJFrame extends JFrame implements ActionListener

   private RoseCanvas rose;              //自訂畫布組件
   private JButton button_color;         //選擇顏色按鈕

   public RoseJFrame()
   { 
      super("四葉玫瑰線");                            //架構邊布局
      Dimension dim=getToolkit().getScreenSize();     //獲得螢幕解析度
      this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2);//視窗置中            
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      JPanel jpanel=new JPanel();                     //面板流布局,置中  
      this.getContentPane().add(jpanel,"North");
      button_color=new JButton("選擇顏色");
      jpanel.add(button_color);
      button_color.addActionListener(this);
      rose=new RoseCanvas(Color.red);                 //建立自訂畫布組件
      this.getContentPane().add(rose,"Center");
      this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)        //單擊事件處理方法
    { 
       Color c=JColorChooser.showDialog(this,"選擇顏色",Color.blue);
                           //彈出JColorChooser顏色選擇對話方塊,返回選中顏色
       rose.setColor(c);
       rose.repaint();                                //重畫
    }

    public static void main(String arg[])
    {       
       new RoseJFrame();   
    }
}
class RoseCanvas extends Canvas implements ComponentListener//畫布組件
{
    private Color color;                                 //顏色
    public RoseCanvas(Color color)
    {
       this.setColor(color);
       this.addComponentListener(this);       //畫布註冊組件組件監聽器
    }
    public void setColor(Color color)
    {
       this.color=color;
    }
    public void paint(Graphics g)            //在Canvas上作圖
    { 
       int x0=this.getWidth()/2;            //(x0,y0)是組件正中點座標
       int y0=this.getHeight()/2;                            
       g.setColor(color);                    //設定畫線顏色
       g.drawLine(x0,0,x0,y0*2);             //畫Y軸
       g.drawLine(0,y0,x0*2,y0);             //畫X軸
       int j=40;
       while(j<this.getWidth()/3.5)                          //畫若干圈四葉玫瑰線
       {  
           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;
       }
    }
    public void componentResized(ComponentEvent e)   //改變組件大小時
    {                                               
        this.repaint();                              //重畫 
    }
    public void componentMoved(ComponentEvent e)    {  }
    public void componentHidden(ComponentEvent e)   {  }
    public void componentShown(ComponentEvent e)    {  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.