62,614
社区成员
发帖
与我相关
我的任务
分享
package com.lun.part6;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RoseJFrame extends JFrame implements ActionListener {
public RoseCanvas canvas;
public JButton jbtn;
public JPanel jp;
public RoseJFrame() {
super("四叶玫瑰线");
Dimension dim = this.getToolkit().getScreenSize();
this.setBounds(dim.width / 4, dim.height / 4, dim.width / 2, dim.height / 2);
// this.setSize(new Dimension(300, 300));
// this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
jbtn = new JButton("选择颜色");
jp = new JPanel();
canvas = new RoseCanvas(Color.blue);
this.getContentPane().add(jbtn, BorderLayout.NORTH);
jp.add(canvas);
this.getContentPane().add(jp, BorderLayout.CENTER);
jp.setOpaque(true);
jbtn.addActionListener(this);
this.setVisible(true);
}
class RoseCanvas extends Canvas {
private Color color;
public RoseCanvas(Color c) {
this.setColor(c);
}
public void setColor(Color c) {
this.color = c;
}
public void paint(Graphics g) {
int x = this.getWidth() / 2;
int y = this.getHeight() / 2;
g.setColor(this.color);
g.drawLine(x, 0, x, y * 2);
g.drawLine(0, y, x * 2, 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 x1 = (int) Math.round(radius * Math.cos(angle) * 2);
int y1 = (int) Math.round(radius * Math.sin(angle));
g.fillOval(x1 + x, y1 + y, 2, 2);
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new RoseJFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Color c = JColorChooser.showDialog(this, "选择颜色", Color.blue);
this.canvas.setColor(c);
this.canvas.repaint();
}
}