[java绘图问题]为什么在Canvas上,无法显示图形讷?

Forrest-Lyu 2015-12-13 02:56:33
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();
}

}


我不知道,为什么显示不出自己在RoseCanvas中绘画出的图形?
...全文
114 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧