|
Java example source code file (CLSFractal.java)
The CLSFractal.java Java example source code
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This source code is provided to illustrate the usage of a given feature
* or technique and has been deliberately simplified. Additional steps
* required for a production-quality application, such as security checks,
* input validation and proper error handling, might not be present in
* this sample code.
*/
import java.awt.Graphics;
import java.util.Stack;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
/**
* A (not-yet) Context sensitive L-System Fractal applet class.
*
* The rules for the Context L-system are read from the java.applet.Applet's
* attributes and then the system is iteratively applied for the
* given number of levels, possibly drawing each generation as it
* is generated. Note that the ContextLSystem class does not yet
* handle the lContext and rContext attributes, although this
* class is already designed to parse the '[' and ']' characters
* typically used in Context sensitive L-Systems.
*
* @author Jim Graham
*/
@SuppressWarnings("serial")
public class CLSFractal
extends java.applet.Applet
implements Runnable, MouseListener {
Thread kicker;
ContextLSystem cls;
int fractLevel = 1;
int repaintDelay = 50;
boolean incrementalUpdates;
float startAngle = 0;
float rotAngle = 45;
float Xmin;
float Xmax;
float Ymin;
float Ymax;
int border;
boolean normalizescaling;
@Override
public void init() {
String s;
cls = new ContextLSystem(this);
s = getParameter("level");
if (s != null) {
fractLevel = Integer.parseInt(s);
}
s = getParameter("incremental");
if (s != null) {
incrementalUpdates = s.equalsIgnoreCase("true");
}
s = getParameter("delay");
if (s != null) {
repaintDelay = Integer.parseInt(s);
}
s = getParameter("startAngle");
if (s != null) {
startAngle = Float.valueOf(s).floatValue();
}
s = getParameter("rotAngle");
if (s != null) {
rotAngle = Float.valueOf(s).floatValue();
}
rotAngle = rotAngle / 360 * 2 * 3.14159265358f;
s = getParameter("border");
if (s != null) {
border = Integer.parseInt(s);
}
s = getParameter("normalizescale");
if (s != null) {
normalizescaling = s.equalsIgnoreCase("true");
}
addMouseListener(this);
}
@Override
public void destroy() {
removeMouseListener(this);
}
@Override
public void run() {
Thread me = Thread.currentThread();
boolean needsRepaint = false;
while (kicker == me && cls.getLevel() < fractLevel) {
cls.generate();
if (kicker == me && incrementalUpdates) {
repaint();
try {
Thread.sleep(repaintDelay);
} catch (InterruptedException ignored) {
}
} else {
needsRepaint = true;
}
}
if (kicker == me) {
kicker = null;
if (needsRepaint) {
repaint();
}
}
}
@Override
public void start() {
kicker = new Thread(this);
kicker.start();
}
@Override
public void stop() {
kicker = null;
}
/*1.1 event handling */
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
cls = new ContextLSystem(this);
savedPath = null;
start();
e.consume();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
String savedPath;
@Override
public void paint(Graphics g) {
String fractalPath = cls.getPath();
if (fractalPath == null) {
super.paint(g);
return;
}
if (savedPath == null || !savedPath.equals(fractalPath)) {
savedPath = fractalPath;
render(null, fractalPath);
}
for (int i = 0; i < border; i++) {
g.draw3DRect(i, i, getSize().width - i * 2, getSize().height - i * 2,
false);
}
render(g, fractalPath);
}
void render(Graphics g, String path) {
Stack<CLSTurtle> turtleStack = new Stack
Other Java examples (source code examples)Here is a short list of links related to this Java CLSFractal.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.