java canvas 刷新,Java Canvas.getHeight方法代码示例

该代码示例展示了如何在Java中使用LWJGL库创建一个双Canvas显示,并实现了窗口关闭、全屏切换、空间切换、鼠标位置获取以及键盘事件监听(包括ESC退出、空格键切换鼠标捕获、F键切换全屏、4键切换Canvas显示)。
摘要由CSDN通过智能技术生成

import java.awt.Canvas; //导入方法依赖的package包/类

public DisplayParentTest() throws LWJGLException {

setTitle( "LWJGL Display Parent Test" );

setSize( 640, 320 );

setLayout( new GridLayout( 1, 2 ) );

final Canvas display_parent = new Canvas();

final Canvas display_parent_2 = new Canvas();

display_parent.setFocusable( true );

display_parent.setIgnoreRepaint( true );

add( display_parent );

display_parent_2.setFocusable( true );

display_parent_2.setIgnoreRepaint( true );

add( display_parent_2 );

addWindowListener( new WindowAdapter() {

public void windowClosing( WindowEvent e ) {

killswitch = true;

}

} );

setResizable( true );

setVisible( true );

Display.setParent( display_parent );

Display.setVSyncEnabled( true );

Display.create();

float angle = 0f;

while ( isVisible() && !killswitch ) {

angle += 1.0f;

int width;

int height;

if ( !Display.isFullscreen() ) {

width = display_parent.getWidth();

height = display_parent.getHeight();

}

else {

width = Display.getDisplayMode().getWidth();

height = Display.getDisplayMode().getHeight();

}

if ( width < 1 || height < 1 ) {

continue;

}

glViewport( 0, 0, width, height );

glClearColor( 0.0f, 1.0f, 0.0f, 1.0f );

glClear( GL_COLOR_BUFFER_BIT );

glMatrixMode( GL_PROJECTION );

glLoadIdentity();

gluOrtho2D( 0.0f, (float) width, 0.0f, (float) height );

glMatrixMode( GL_MODELVIEW );

glPushMatrix();

glTranslatef( width / 2.0f, height / 2.0f, 0.0f );

glRotatef( 2 * angle, 0f, 0f, -1.0f );

glRectf( -50.0f, -50.0f, 50.0f, 50.0f );

glPopMatrix();

Display.update();

while ( Keyboard.next() ) {

// closing on ESCAPE

if ( Keyboard.getEventKey() == Keyboard.KEY_ESCAPE && Keyboard.getEventKeyState() ) {

Display.destroy();

dispose();

break;

}

if ( Keyboard.getEventKey() == Keyboard.KEY_SPACE && Keyboard.getEventKeyState() ) {

Mouse.setGrabbed( !Mouse.isGrabbed() );

}

if ( Keyboard.getEventKey() == Keyboard.KEY_F && Keyboard.getEventKeyState() ) {

Display.setFullscreen( !Display.isFullscreen() );

}

if ( Keyboard.getEventKey() == Keyboard.KEY_4 && Keyboard.getEventKeyState() ) {

if ( Display.getParent() == display_parent ) {

Display.setParent( display_parent_2 );

}

else {

Display.setParent( display_parent );

}

}

}

while ( Mouse.next() ) {

System.out.println( "Mouse.getEventX() = " + Mouse.getEventX() + " | Mouse.getEventY() = " + Mouse.getEventY() );

}

}

Display.destroy();

dispose();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值