分享
 
 
 

Simplifying Graphics With Java and Threads

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Simplifying Graphics With Java and Threads

By Alex Chaffee and John Papageorge

July 1997

<!-- Begin Main Body Text -->

Here's some code that EarthWeb's Alex Chaffee likes to use as an example of how Java can be used to simplify algorithms by combining graphics with threading to render complex graphics.

Basically there are three threads: a Renderer, which performs a complex calculation; an Animator, which wakes up periodically and draws the current image to the screen; and of course the default User Interface thread, which responds to user events and changes the display as appropriate.

The Renderer runs at a low priority—just grinding away forever, or at least until it's done; it shouldn't interfere with the other two threads. The Animator only comes to life every 500 msec or so, to avoid the expense of drawing each pixel.

To perform a specific calculation, subclass the Renderer to make a Mandelbrot function. Note the object-oriented design: this way, the Mandelbrot subclass only needs to override the Run method and doesn't have to worry about the threading implementation, which is handled by its superclass. Of course, in this example, the threading implementation is trivial, but this is a teaching exercise.

Other items of note are the use of an offscreen Graphics buffer, and the use of progressive rendering. The code makes several rough passes over the image, calculating the value in each quadrant, then gradually refines the view. Unlike Netscape's rendering of progressive gifs, this program treats a calculated pixel as the "center" of a square of color, not as the top corner or a pixel-wide stripe, and so avoids that odd venetian-blind scrolling-up effect. With a laugh, Chaffee says, "But maybe I'm the only one in the world who thought Netscape's gif-rendering algorithm looked funny." now , see Chaffee's source code

/*

man.java

a test to do threaded animation

Copyright (c) 1996 Alex Chaffee, all rights reserved

Permission is granted to use this source code for

instructional purposes as long as copyright information

remains intact.

@Author: Alex Chaffee

*/

import java.awt.*;

import java.util.*;

import java.applet.*;

import java.lang.Math;

/**

* Animator

* This class implements the thread that draws the image to the screen

* every 500 msec.

* @author Alex Chaffee

**/

class Animator implements Runnable {

static int delay = 500;

private Applet applet;

private Vector images;

private Thread thread = null;

private int frame;

public Animator(Applet appletIn, Vector imagesIn) {

applet = appletIn;

images = imagesIn;

}

public void start() {

if (thread == null) {

thread = new Thread(this);

thread.start();

}

}

public void stop() {

System.out.println("Animator stopped");

thread.stop();

thread = null;

}

public void run() {

thread.setPriority(Thread.MAX_PRIORITY-1);

while (thread != null) {

applet.repaint();

try {

thread.sleep(delay);

} catch (InterruptedException e) {};

// Increment frame

// Must use synchronized on the off chance that update gets called

// between the frame++ and the frame=0

synchronized (this) {

frame++;

if (frame images.size()) {

frame = 0;

}

}

}

}

public void paint(Graphics g) {

if (frame span = 4 / (K^mag)

* @see kZoom

*/

int mag;

/**

* point in virtual space around which we're drawing the thing

* (i.e. where in virtual space is (0,0) in the window

*/

float originX = 0;

float originY = 0;

public Mandelbrot(Image image, int mag, float originX, float originY) {

super(image);

this.mag = mag;

this.originX = originX;

this.originY = originY;

}

static float MagToSpan(int mag) {

return 4 / (float)(java.lang.Math.pow(kZoom, mag));

}

/**

* Render this image progressively.

*/

public void run() {

/**

* The graphics port of the image we're drawing to

*/

Graphics g;

g = image.getGraphics();

int width, height;

width = image.getWidth(null);

height = image.getHeight(null);

/**

* The width of the window on the virtual space

* @see mag

*/

float span = MagToSpan(mag);

/**

* current resolution (how big each pixel is)

*/

int resolution;

/**

* how far in do we go before we're rendering by pixel

*/

int resolutionMax = 10; // should calculate based on image size

/**

* current increment (based on resolution)

*/

float inc;

resolution = 1;

int widthPixel, heightPixel;

do {

// the resolution determines which power of two we're dividing

// the span by -- so it fills in by squares

float scale = 1 / (float)(java.lang.Math.pow(2, resolution));

inc = span * scale;

// pre-calculate the width and height of each "pixel" in the image

widthPixel = (int)(scale * width) + 1;

heightPixel = (int)(scale * height) + 1;

spew("resolution " + resolution + " pixel=(" + widthPixel + ", " +

heightPixel + ")");

// Mandelbrot function

Color c;

int maxiterations = 100;

int i;

float temp, r, x, y;

float minX = originX + inc/2;

float maxX = originX + span;

float minY = originY + inc/2;

float maxY = originY + span;

for (float c1= minX; c1 1 || heightPixel 1);

spew("stopped");

} // method run

void spew(String str) {

System.out.println("(" + originX + "," + originY + ")x" + mag + ":" + str);

}

public static int iterate(float c1, float c2) {

int maxiterations = 100;

// Nitty gritty, merci Benoit

float r, x, y, temp;

int i;

r = 0;

x = 0;

y = 0;

i = 0;

while ((i0)

zoom(mag-1);

return true;

default:

return false;

} // switch

} // keyDown

void zoom(int magNew) {

float spanOld = Mandelbrot.MagToSpan(mag);

float centerX = originX + spanOld/2;

float centerY = originY + spanOld/2;

mag = magNew;

float spanNew = Mandelbrot.MagToSpan(mag);

originX = centerX - spanNew/2;

originY = centerY - spanNew/2;

stopThreads();

startThreads();

}

public boolean mouseDown(Event evt, int x, int y) {

System.out.println(evt.toString());

stopThreads();

animator = null;

float span = Mandelbrot.MagToSpan(mag);

originX = x * span / size().width + originX - span/2;

originY = y * span / size().height + originY - span/2;

System.out.println("New origin: " + x + (new Dimension(x,y)).toString()

+ " - "

+ originX + "," + originY );

start();

return true;

}

static float convert(int x, int width, int mag, float origin) {

float span = Mandelbrot.MagToSpan(mag);

float z = x * span/width + origin;

return z;

}

public boolean mouseMove(Event evt, int x, int y) {

float vx = convert(x, size().width, mag, originX);

float vy = convert(y, size().height, mag, originY);

int i = Mandelbrot.iterate(vx, vy);

String str = "i=" + (new Integer(i)).toString();

str = str + " (" + vx + ", " + vy + ")";

getAppletContext().showStatus(str);

// System.out.println((new Integer(i)).toString());

return true;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有