java graphics 实例化_请教一下Graphics的问题,一直以来都不懂它的用法,大神帮我看看...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package jiao;

import java.awt.Color;

import java.awt.Container;

import java.awt.Graphics;

import javax.swing.JFrame;

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 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;

}

}

}

package jiao;

public class TestDrawLine {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new DrawLine();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值