java中,用Graphics 在canvas画图,图形一伞就没有,奖励分数20!

小码哥的进阶 2014-06-12 11:34:48

import java.awt.*;
import javax.swing.JFrame;

public class TestCirCles {
public static void main(String[] args) {
// Circle.drawCircle(100, 100, 50);
// System.out.println("hello world");
// Parts.drawCircle(100, 100, 50);

MyCanvas ca = new MyCanvas();
ca.paint();

}
}

class MyCanvas {
static Graphics g1;
static JFrame f;
static Image imag;
static {
f = new JFrame("画图演示");
// 创建一个区域
Canvas canvas = new Canvas();
f.add(canvas);
f.setSize(400, 400);
f.setBackground(Color.white);
f.setVisible(true);
//imag = f.createImage(400,300);
//g1 = imag .getGraphics();
// 要先让组件显示后,才会返回Graphics对象,否则会返回null
g1=canvas.getGraphics();
f.setDefaultCloseOperation(3);
}
int x = 100, y = 100, r = 100, n = 100;
int x0 = 10, y0 = 10;

/**
* 画圆 知识点:引入 double数据类型
*/
public void paint() {
for (int i = 0; i <= 360; i = i + 360 / n) {
// 数据类型double
double cs = Math.cos(i);
double ss = Math.sin(i);
x0 = (int) (x + r * cs);
y0 = (int) (y + r * ss);
g1.drawString("*", x0, y0);
}
}
}

另一个代码也是这种情况

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;

public class Circle extends JFrame {

private static final long serialVersionUID = -2420677068593785097L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
// static MyPanel panel = new MyPanel();
static Graphics g;
static JFrame f;
static {
f = new JFrame("画图演示");
// 创建一个区域
Canvas canvas = new Canvas();
f.add(canvas);
f.setSize(400, 400);
f.setBackground(Color.white);
f.setVisible(true);
// 要先让组件显示后,才会返回Graphics对象,否则会返回null
g = canvas.getGraphics();
}

public static void drawCircle(double pointX, double pointY, float radius) {
Graphics2D g2 = (Graphics2D) g;
radius = 100;
g.setColor(Color.RED);
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(pointX, pointY, pointX + radius, pointY
+ radius);
g2.fill(circle);

}

}

...全文
273 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
grapepaul 2014-06-13
  • 打赏
  • 举报
回复
标题改成 献上XX分数,会有更多人来回答你的问题。 ps. 20太少了
小码哥的进阶 2014-06-12
  • 打赏
  • 举报
回复
好像说用双缓冲技术,不好使啊,不太会用 /**双缓冲画布*/ private Image memoryImage; /**双缓冲画笔*/ private Graphics memoryGraphics; //重新开辟一块内存区域,用于存放叠加后的图片 this.memoryImage = this.createImage(900,700); //从双缓冲画布当中得到双缓冲画笔,这样通过双缓冲画笔绘制的图片就会叠加在双缓冲画布中 this.memoryGraphics = this.memoryImage.getGraphics(); //绘制文字 this.memoryGraphics.drawString("分数:"+pointer, 100, 57); //绘制图片 this.memoryGraphics.drawImage(hartNumImg,50,80,30,25,this);

62,620

社区成员

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

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