您的位置:首页 > 编程语言 > Java开发

java中Graphics相关类的使用方法

2010-06-19 22:44 831 查看
有很多问题:

1.怎么精确的画出一些曲线?所有的Graphics中的参数都是整数,因为像素不可分割,但是正是由于这个原因会是图像出现不光滑的现象,怎么解决?

2.就像任务管理器中的性能和联网中的图形,怎么直接动态显示,不断的重复擦除和重绘会不会很浪费时间和空间?

3.怎么实现重绘,尤其是在动态中绘制的图形?

4.所有的曲线从微积分的角度都是直线,所以通过画直线来画曲线,java是否可以胜任这项工作?开发桌面级处理教复杂的及时绘图程序?

简单的画正弦复合曲线所遇到的上面的一些问题,代码如下:

Code:

import java.awt.Color;

import java.awt.Container;

import java.awt.Graphics;



import javax.swing.JFrame;





public class TestDrawLine {

public static void main(String[] args) {

new DrawLine();

}

}



class DrawLine extends JFrame {



Point start;

Point end;

Container p;

public DrawLine() {

p = getContentPane();

setBounds(200, 200, 800, 400);

setVisible(true);

p.setBackground(Color.BLACK);

setLayout(null);

//paintZB(this.getGraphics());

//paintSin(this.getGraphics());

paintComponents(this.getGraphics());

setResizable(false);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

// public void paint(Graphics g) {

// g.drawLine(80, 80, 720, 80);

// paintSin(this.getGraphics());

// }

public void paintComponents(Graphics gg) {

gg.drawLine(10, 100, 200, 400);

final Graphics g = gg;

start = new Point(0, 80);

end = new Point(0, 80);

g.setColor(Color.red);

Runnable run = new Runnable() {

Point temp = null;

int x = 0;

public void run() {

int d = 1;

while(true) {

try {

temp = new Point(x, 80+(int)(40*Math.sin(Math.PI*(x-80)/30)));

g.drawLine(start.x, start.y, end.x, end.y);

//g.drawLine(80, 80, 720, 80);

g.drawLine(790-start.x, start.y+120, 790-end.x, end.y+120);

g.drawLine(start.x, start.y+240, end.x, end.y+240);

start = end;

end = temp;

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

if(x>800 || x<0) {

d = -d;

Color c = g.getColor();

if(c == Color.RED) {

g.setColor(Color.BLUE);

} else {

g.setColor(Color.RED);

}

}

x += d;

}

}

};

new Thread(run).start();

}



class Point {

int x, y;

public Point(int _x, int _y) {

x = _x;

y = _y;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: