[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中绘画出的图形?
...全文
111 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
Java中的Canvas绘图源码实例,主要是画线条、弧线、圆角矩形、三角形、文字、圆形等简单的几何图形,主要是通过创建DrawingCanvas对象canvas,为canvas设置命令监听者,实现接口CommandListener的方法,把缓冲区图像的内容绘制到画布上,画图部分代码:   int w = getWidth(); // 画布的宽度   int h = getHeight(); // 画布的高度   Image buffer = Image.createImage(w, h); // 用于绘图的缓冲图像   Graphics gc = buffer.getGraphics(); // 获取缓冲图像的图形环境   // 清除画布   public void clearScreen() {    gc.setColor(255,255,255); // 设置绘图颜色为白色    gc.fillRect(0,0,w,h); // 把缓冲图像填充为白色    gc.setColor(255,0,0); // 设置绘图颜色为红色   }   // 绘制直线   public void drawLine() {    setTitle("直线"); // 设置画布的标题    clearScreen(); // 清除画布    gc.drawLine(10,10,w-20,h-20); // 绘制黑色直线    gc.setColor(0,0,255); // 设置绘图颜色为蓝色    gc.drawLine(10,h/2,w-10,h/2); // 绘制蓝色直线   }   // 绘制弧   public void drawArc() {    setTitle("弧线和填充弧");    clearScreen();    gc.drawArc(5,5,w/2-20,h/2-20,60,216); // 绘制弧线    gc.drawArc(5,h/2-10,w/2-20,h/2-20,0,360); // 绘制圆    gc.setColor(0,0,255);    gc.fillArc(w/2,5,w/2-20,h/2-20,60,216); // 绘制填充弧线    gc.fillArc(w/2,h/2-10,w/2-20,h/2-20,0,360); // 绘制填充圆   }   // 绘制矩形   public void drawRect() {    setTitle("矩形和填充矩形");    clearScreen();    gc.drawRect(25,25,w/2-30,h/2-30); // 绘制矩形    gc.fillRect(w/2 25,25,w/2-30,h/2-30); // 绘制填充矩形   }   // 绘制圆角矩形   public void drawRoundRect() {    setTitle("圆角矩形和填充圆角矩形");    clearScreen();    gc.drawRoundRect(5,5,w-5-30,h/2-30,20,20); // 绘制圆角矩形    gc.setColor(0,0,255);    gc.fillRoundRect(5,h/2,w-30,h/2-30,20,20); // 绘制填充圆角矩形   }   // 绘制三角形   public void drawTriangle() {    setTitle("填充三角形");    clearScreen();    gc.fillTriangle(w/2, h/6, w/6, h/2, w/2, h/2);   }   // 绘制文字   public void drawText() {    setTitle("文字"); //设置标题    clearScreen();    gc.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_SMALL)); // 设置字体    gc.drawString("Hello World!",0,0,gc.TOP|gc.LEFT); // 使用当前字体绘制文字    gc.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD|Font.STYLE_UNDERLINED,Font.SIZE_LARGE));    gc.drawString("Hello

62,614

社区成员

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

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