📄 java3dapplet.java
字号:
// Coupled background and fog color
private boolean coupledBackgroundOnOff = true;
private CheckboxMenuItem coupledBackgroundOnOffMenu = null;
// Image menu choices
private NameValue[] images = {
new NameValue( "无背景", null ),
new NameValue( "星空", "stars2.jpg" ),
new NameValue( "红云", "oddclouds.jpg" ),
new NameValue( "白云", "clouds.jpg" ),
new NameValue( "火焰", "fire.jpg" ),
};
private int currentImage = 0;
private ImageComponent2D[] imageComponents = null;
private CheckboxMenu imageMenu = null;
// Color menu choices
private NameValue[] colors = {
new NameValue("白色", White),
new NameValue("灰色", Gray),
new NameValue("深灰色", DarkGray),
new NameValue("黑色", Black),
new NameValue("红色", Red),
new NameValue("深红", DarkRed),
new NameValue("绿色", Green),
new NameValue("深绿色", DarkGreen),
new NameValue("蓝色", Blue),
new NameValue("深蓝色", DarkBlue),
};
private int currentColor = 0;
private int currentBackgroundColor = currentColor;
private CheckboxMenu colorMenu = null;
private CheckboxMenu backgroundColorMenu = null;
private CheckboxMenuItem moveSceneMenuItem = null;
private CheckboxMenuItem backgroundsoundMenuItem = null;
private CheckboxMenuItem objectmoveMenuItem = null;
private CheckboxMenuItem AmbientLightMenuItem = null;
private CheckboxMenuItem DirectionalLightMenuItem = null;
private CheckboxMenuItem PointLightMenuItem = null;
private CheckboxMenuItem SpotLightMenuItem = null;
// Density menu choices
private NameValue[] densities = {
new NameValue("无雾效果", new Float(0.0f)),
new NameValue("朦胧 (0.5)", new Float(0.5f)),
new NameValue("稀薄的雾 (1.0)", new Float(1.0f)),
new NameValue("浓雾 (2.0)", new Float(2.0f)),
};
private int currentDensity = 0;
private CheckboxMenu densityMenu = null;
//
// Initialize the GUI (application and applet)
//
// On/off choices
private boolean ambientOnOff = false;
private boolean brightAmbientOnOff = false;
private boolean redDirectionalOnOff = false;
private boolean yellowDirectionalOnOff = false;
private boolean orangePointOnOff = false;
private CheckboxMenuItem ambientOnOffMenu;
private CheckboxMenuItem brightAmbientOnOffMenu;
private CheckboxMenuItem redDirectionalOnOffMenu;
private CheckboxMenuItem yellowDirectionalOnOffMenu;
private CheckboxMenuItem orangePointOnOffMenu;
public void initialize(String[] args) {
// Initialize the window, menubar, etc.
super.initialize(args);
exampleFrame.setTitle("Java3D 小场景");
//
// Add a menubar menu to change node parameters
// Coupled background color
// Background color -->
// Fog color -->
// Fog density -->
//
Menu m = new Menu("指数雾");
coupledBackgroundOnOffMenu = new CheckboxMenuItem(
"双背景颜色", coupledBackgroundOnOff);
coupledBackgroundOnOffMenu.addItemListener(this);
m.add(coupledBackgroundOnOffMenu);
backgroundColorMenu = new CheckboxMenu("背景色", colors,
currentBackgroundColor, this);
m.add(backgroundColorMenu);
backgroundColorMenu.setEnabled(!coupledBackgroundOnOff);
colorMenu = new CheckboxMenu("雾颜色", colors,
currentColor, this);
m.add(colorMenu);
densityMenu = new CheckboxMenu("雾浓度", densities,
currentDensity, this);
m.add(densityMenu);
exampleMenuBar.add(m);
// 加入改变背景的菜单
Menu m1 = new Menu( "纹理背景" );
imageMenu = new CheckboxMenu( "背景图片", images,
currentImage, this );
m1.add( imageMenu );
exampleMenuBar.add( m1 );
Menu m2 = new Menu( "场景操作" );
moveSceneMenuItem = new CheckboxMenuItem("场景自动移动");
moveSceneMenuItem.addItemListener(this);
m2.add(moveSceneMenuItem);
backgroundsoundMenuItem = new CheckboxMenuItem("背景音乐暂停");
backgroundsoundMenuItem.addItemListener(this);
m2.add(backgroundsoundMenuItem);
objectmoveMenuItem = new CheckboxMenuItem("物体转动");
objectmoveMenuItem.addItemListener(this);
m2.add(objectmoveMenuItem);
exampleMenuBar.add( m2 );
Menu m3 = new Menu( "灯光" );
ambientOnOffMenu = new CheckboxMenuItem(
"环境光(普通)", ambientOnOff );
ambientOnOffMenu.addItemListener( this );
m3.add( ambientOnOffMenu );
brightAmbientOnOffMenu = new CheckboxMenuItem(
"环境光(强烈)", brightAmbientOnOff );
brightAmbientOnOffMenu.addItemListener( this );
m3.add( brightAmbientOnOffMenu );
redDirectionalOnOffMenu = new CheckboxMenuItem(
"直射光(红)", redDirectionalOnOff );
redDirectionalOnOffMenu.addItemListener( this );
m3.add( redDirectionalOnOffMenu );
yellowDirectionalOnOffMenu = new CheckboxMenuItem(
"直射光(黄)", yellowDirectionalOnOff );
yellowDirectionalOnOffMenu.addItemListener( this );
m3.add( yellowDirectionalOnOffMenu );
orangePointOnOffMenu = new CheckboxMenuItem(
"点光(橙)", orangePointOnOff );
orangePointOnOffMenu.addItemListener( this );
m3.add( orangePointOnOffMenu );
exampleMenuBar.add( m3 );
// Preload the background images
// Use the texture loading utility to read in the image
// files and process them into an ImageComponent2D
// for use in the Background node.
if ( debug ) System.err.println( " background images..." );
TextureLoader texLoader = null;
String value = null;
imageComponents = new ImageComponent2D[images.length];
for ( int i = 0; i < images.length; i++ )
{
value = (String)images[i].value;
if ( value == null )
{
imageComponents[i] = null;
continue;
}
texLoader = new TextureLoader( value, this );
imageComponents[i] = texLoader.getImage( );
// Component could be null if image couldn't be loaded
}
}
//
// Handle checkboxes and menu choices
//
public void checkboxChanged(CheckboxMenu menu, int check) {
if (menu == backgroundColorMenu) {
// Change the background color
currentBackgroundColor = check;
Color3f color = (Color3f) colors[check].value;
background.setColor(color);
return;
}
if (menu == colorMenu) {
// Change the fog color
currentColor = check;
Color3f color = (Color3f) colors[check].value;
fog.setColor(color);
// If background is coupled, set the background color
if (coupledBackgroundOnOff) {
currentBackgroundColor = currentColor;
backgroundColorMenu.setCurrent(check);
background.setColor(color);
}
return;
}
// 当背景改变时
if (menu == imageMenu) {
// Change the background image
currentImage = check;
ImageComponent2D image = imageComponents[check];
background.setImage(image);
return;
}
if (menu == densityMenu) {
// Change the fog density
currentDensity = check;
float density = ( (Float) densities[currentDensity].value).floatValue();
fog.setDensity(density);
return;
}
// Handle all other checkboxes
super.checkboxChanged(menu, check);
}
public void itemStateChanged(ItemEvent event) {
Object src = event.getSource();
// Check if it is the coupled background choice
boolean state = false ;
if(src == moveSceneMenuItem)
{
state = moveSceneMenuItem.getState();
translator.setEnable(state);
}
if(src == backgroundsoundMenuItem)
{
state = backgroundsoundMenuItem.getState();
backgroundSound.setPause(state);
}
if(src == objectmoveMenuItem)
{
state = objectmoveMenuItem.getState();
spinner.setEnable(state);
}
if (src == coupledBackgroundOnOffMenu) {
coupledBackgroundOnOff = coupledBackgroundOnOffMenu.getState();
if (coupledBackgroundOnOff) {
currentBackgroundColor = currentColor;
backgroundColorMenu.setCurrent(currentColor);
Color3f color = (Color3f) colors[currentColor].value;
background.setColor(color);
backgroundColorMenu.setEnabled(false);
}
else {
backgroundColorMenu.setEnabled(true);
}
}
if ( src == ambientOnOffMenu )
{
ambientOnOff = ambientOnOffMenu.getState( );
ambient.setEnable( ambientOnOff );
return;
}
if ( src == brightAmbientOnOffMenu )
{
brightAmbientOnOff = brightAmbientOnOffMenu.getState( );
brightAmbient.setEnable( brightAmbientOnOff );
return;
}
if ( src == redDirectionalOnOffMenu )
{
redDirectionalOnOff = redDirectionalOnOffMenu.getState( );
redDirectional.setEnable( redDirectionalOnOff );
return;
}
if ( src == yellowDirectionalOnOffMenu )
{
yellowDirectionalOnOff = yellowDirectionalOnOffMenu.getState( );
yellowDirectional.setEnable( yellowDirectionalOnOff );
return;
}
if ( src == orangePointOnOffMenu )
{
orangePointOnOff = orangePointOnOffMenu.getState( );
orangePoint.setEnable( orangePointOnOff );
return;
}
// Handle all other checkboxes
super.itemStateChanged(event);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -