RoseCanvas類
package yan03; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; public class RoseCanvas extends Canvas //四葉玫瑰線畫布組件 { private Color color; //顏色 public RoseCanvas(Color color) { this.setColor(color); } 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(this.color); //設置畫線顏色 g.drawLine(x0,0,x0,y0*2); //畫X軸 g.drawLine(0,y0,x0*2,y0); //畫Y軸 for (int j=40; j<200; j+=20) //畫若干圈四葉玫瑰線 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,2,2); //畫直徑為1的圓就是一個點 } } }
RoseJFrame類
package yan03; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RoseJFrame extends JFrame implements ActionListener { private RoseCanvas canvas; //自定義畫布組件 public RoseJFrame() { super("四葉玫瑰線"); //框架邊布局 Dimension dim=this.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"); JButton button_color = new JButton("選擇顏色"); jpanel.add(button_color); button_color.addActionListener(this); this.canvas = new RoseCanvas(Color.red); //創建自定義畫布組件 this.getContentPane().add(this.canvas,"Center"); this.setVisible(true); } public void actionPerformed(ActionEvent ev) //按鈕動作事件處理方法 { Color c=JColorChooser.showDialog(this,"選擇顏色",Color.blue); //彈出JColorChooser顏色選擇對話框,返回選中顏色 this.canvas.setColor(c); this.canvas.repaint(); //調用canvas的paint(Graphics)方法,重畫 } public static void main(String arg[]) { new RoseJFrame(); } }
RoseNJFrame類
package yan03; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RoseNJFrame extends JFrame implements ActionListener,ComponentListener { private JRadioButton radiobutton[]; //單選按鈕 private JCheckBox checkbox; //復選框 private RoseNCanvas canvas; //自定義畫布組件 public RoseNJFrame() { super("多葉玫瑰線"); //框架邊布局 Dimension dim=getToolkit().getScreenSize(); //獲得屏幕分辨率 this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2); //窗口居中 this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.addComponentListener(this); //注冊組件事件監聽器 JToolBar toolbar=new JToolBar(); //創建工具欄,默認水平方向 this.getContentPane().add(toolbar,"North"); //工具欄添加到框架內容窗格北部 String rosestr[]={"一葉","四葉","三葉","八葉","五葉","十二葉","七葉","十六葉","九葉"}; ButtonGroup bgroup = new ButtonGroup(); //按鈕組 radiobutton = new JRadioButton[rosestr.length]; //單選按鈕數組 for (int i=0; i<radiobutton.length; i++) { radiobutton[i]=new JRadioButton(rosestr[i]); //單選按鈕 radiobutton[i].addActionListener(this); bgroup.add(radiobutton[i]); //單選按鈕添加到按鈕組 toolbar.add(radiobutton[i]); //單選按鈕添加到工具欄 } radiobutton[0].setSelected(true); //設置單選按鈕的選中狀態 checkbox = new JCheckBox("Y軸",false); //復選框 toolbar.add(checkbox); checkbox.addActionListener(this); //復選框注冊動作事件監聽器 JButton button_color = new JButton("選擇顏色"); toolbar.add(button_color); button_color.addActionListener(this); canvas = new RoseNCanvas(1,Color.red); //創建自定義畫布組件 this.getContentPane().add(canvas,"Center"); this.setVisible(true); } public void actionPerformed(ActionEvent e) //按鈕動作事件處理方法 { if (e.getSource() instanceof JRadioButton) //選擇一個顏色復選框 for (int i=0; i<radiobutton.length; i++) if (e.getSource()==radiobutton[i]) { canvas.setLeaf(i+1); break; } if (e.getSource()==checkbox) { canvas.setAxis(checkbox.isSelected()); if (e.getActionCommand().equals("Y軸")) checkbox.setText("X軸"); else checkbox.setText("Y軸"); } if (e.getActionCommand().equals("選擇顏色")) { Color c=JColorChooser.showDialog(this,"選擇顏色",Color.blue); //彈出JColorChooser顏色選擇對話框,返回選中顏色 canvas.setColor(c); } canvas.repaint(); //重畫 } public void componentResized(ComponentEvent e) //改變組件大小時 { canvas.repaint(); //重畫 } public void componentMoved(ComponentEvent e) {} public void componentHidden(ComponentEvent e) {} public void componentShown(ComponentEvent e) {} public static void main(String arg[]) { new RoseNJFrame(); } } class RoseNCanvas extends Canvas //畫布組件 { private int leaf; //多葉玫瑰線的葉數 private boolean axis; //軸,默認Y軸 private Color color; //顏色 public RoseNCanvas(int leaf, Color color) { this.axis = false; this.setLeaf(leaf); this.setColor(color); } void setLeaf(int leaf) { this.leaf = leaf; } void setAxis(boolean axis) { this.axis = axis; } 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(this.color); //設置畫線顏色 g.drawLine(x0,0,x0,y0*2); //畫X軸 g.drawLine(0,y0,x0*2,y0); //畫Y軸 for (int j=40; j<200; j+=20) //畫若干圈多葉玫瑰線 for (int i=0; i<1023; i++) //畫一圈多葉玫瑰線的若干點 { double angle = i*Math.PI/512, radius; if (!axis) radius = j*Math.sin(this.leaf*angle); //多葉玫瑰線沿X軸 else radius = j*Math.cos(this.leaf*angle); //多葉玫瑰線沿Y軸 int x =(int)Math.round(radius * Math.cos(angle)); int y =(int)Math.round(radius * Math.sin(angle)); g.fillOval(x0+x,y0+y,2,2); //畫直徑為1的圓就是一個點 } } }