注:关于x轴y轴对称忘记做了!
package Testh;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Author cc
* @create 2022/5/15 16:43
*/
public class exam extends JFrame implements ActionListener { //四叶玫瑰线画布组件{
public Color color; //颜色
private int r;
Canvas canvas;
public exam(Color color, int r) {
this.setColor(color);
this.r = r;
}
public void setColor(Color color) {
this.color = color;
}
public class RoseCanvas extends Canvas {
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); //画X轴
g.drawLine(0, y0, x0 * 2, y0); //画Y轴
for (int j = 40; j < 200; j += 20) //画若干圈四叶玫瑰线
for (int i = 0; i < 1 << 10; i++) //画一圈四叶玫瑰线的若干点
{
double angle = i * Math.PI / 512;
double radius = j * Math.sin(r * angle); //四叶玫瑰线
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的圆就是一个点
}
validate();
}
}
public void init() {
JFrame frame = new JFrame("多叶玫瑰线");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1, p2;
JList<String> list;
p1 = new JPanel();
JRadioButton j[] = new JRadioButton[7];
ButtonGroup bg = new ButtonGroup();
j[0] = new JRadioButton("red");
j[2] = new JRadioButton("blue");
j[3] = new JRadioButton("green");
j[4] = new JRadioButton("magenta");
j[5] = new JRadioButton("cyan");
j[6] = new JRadioButton("orange");
j[1] = new JRadioButton("pink");
for (int i = 0; i < 7; i++) {
j[i].addActionListener(this);
bg.add(j[i]);
p1.add(j[i]);
}
frame.getContentPane().add(p1, BorderLayout.NORTH);
p2 = new JPanel();
list = new JList<String>();
list.setListData(new String[]{"一叶", "四叶", "三叶", "八叶"});
list.setPreferredSize(new Dimension(50, 400));
list.addListSelectionListener(e -> {
if (e.getValueIsAdjusting()) {
if (list.getSelectedIndex() == 0) {
r = 1;
} else if (list.getSelectedIndex() == 1) {
r = 2;
} else if (list.getSelectedIndex() == 2) {
r = 3;
} else if (list.getSelectedIndex() == 3) {
r = 4;
}
if(r % 2 == 0){
bg1.clearSelection();
j1.setSelected(true);
}else{
bg1.clearSelection();
j2.setSelected(true);
}
}//repaint方法不会立即重绘
canvas.repaint();
validate();
});
p2.add(list);
this.canvas = new RoseCanvas();
frame.getContentPane().add(p2, BorderLayout.WEST);
frame.getContentPane().add(this.canvas, BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args) {
exam e = new exam(Color.red, 2);
e.init();
//frame.add(new exam(Color.red));
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("red")) {
setColor(Color.red);
}
if (e.getActionCommand().equals("blue")) {
setColor(Color.blue);
}
if (e.getActionCommand().equals("green")) {
setColor(Color.green);
}
if (e.getActionCommand().equals("magenta")) {
setColor(Color.magenta);
}
if (e.getActionCommand().equals("cyan")) {
setColor(Color.cyan);
}
if (e.getActionCommand().equals("orange")) {
setColor(Color.orange);
}
if (e.getActionCommand().equals("pink")) {
setColor(Color.pink);
}
canvas.repaint();
validate();
}
}