// must be called on AWT event thread private void setWindowVisible(boolean visible) { if (visible) { if (entity != null) { throw new IllegalStateException("Entity already created"); } // create the image Canvas canvas = JmeClientMain.getFrame().getCanvas(); synchronized (this) { entity = new StereoViewEntity(canvas.getWidth(), canvas.getHeight()); frame = new StereoViewFrame(createImage(entity, canvas.getWidth(), canvas.getHeight())); } entity.setDistance(getDistance()); entity.setAngle(getAngle()); entity.setEnabled(true); frame.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { LOGGER.warning("Stereo window closing"); // uncheck the checkbox, which should also dispose of // the frame if (stereoMI != null) { stereoMI.setSelected(false); } if (entity != null) { setWindowVisible(false); } } }); frame.setSize(JmeClientMain.getFrame().getFrame().getSize()); frame.setVisible(true); } else { if (entity == null) { // already disposed return; } frame.dispose(); entity.dispose(); synchronized (this) { frame = null; entity = null; } } }
@Override public void render() { BufferStrategy bs = canvas.getBufferStrategy(); if (bs == null) { canvas.createBufferStrategy(3); return; } screen.clear(); int xScroll = (int) player.x() - screen.getWidth() / 2; int yScroll = (int) player.y() - screen.getHeight() / 2; level.render(xScroll, yScroll, screen); for (int i = 0; i < pixels.length; i++) { pixels[i] = screen.getPixels()[i]; } Graphics g = bs.getDrawGraphics(); g.drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight(), null); g.setColor(Color.WHITE); g.setFont(new Font("Verdana", 0, 50)); window.setTitle( TITLE + " | " + getFps() + " fps, " + getUps() + " update" + (getUps() > 1 ? " catchup" : "") + " per render"); Toolkit.getDefaultToolkit().sync(); g.dispose(); bs.show(); }
public void initCFD(CFDProber cfd) { System.out.println("initCFD"); FluidRegion fr = new FluidRegion(cfd); fr.setX(50); fr.setY(50); fr.setW(canvas.getWidth() - 100); fr.setH(canvas.getHeight() - 100); gLoop.registerPaintable(fr); }
/** * This method creates a new Image, from the current picture. The resulting width and height will * be less or equal than the given maximum width and height. The scale is maintained. Thus the * width or height may be inferior than the given values. * * @param canvas The canvas on which the picture will be displayed. * @param shadow True if the pictureFileData should store this picture. False if the * pictureFileData instance should not store this picture. Store this picture avoid * calculating the image each time the user selects it in the file panel. * @return The rescaled image. * @throws JUploadException Encapsulation of the Exception, if any would occurs. */ public Image getImage(Canvas canvas, boolean shadow) throws JUploadException { Image localImage = null; if (canvas == null) { throw new JUploadException("canvas null in PictureFileData.getImage"); } int canvasWidth = canvas.getWidth(); int canvasHeight = canvas.getHeight(); if (canvasWidth <= 0 || canvasHeight <= 0) { this.uploadPolicy.displayDebug( "canvas width and/or height null in PictureFileData.getImage()", 1); } else if (shadow && this.offscreenImage != null) { // We take and return the previous calculated image for this // PictureFileData. localImage = this.offscreenImage; } else if (this.isPicture) { try { // First: load the picture. ImageReaderWriterHelper irwh = new ImageReaderWriterHelper((PictureUploadPolicy) uploadPolicy, this); BufferedImage sourceImage = irwh.readImage(0); irwh.dispose(); irwh = null; ImageHelper ih = new ImageHelper( (PictureUploadPolicy) this.uploadPolicy, this, canvasWidth, canvasHeight, this.quarterRotation); localImage = ih.getBufferedImage( ((PictureUploadPolicy) this.uploadPolicy).getHighQualityPreview(), sourceImage); // We free memory ASAP. sourceImage.flush(); sourceImage = null; } catch (OutOfMemoryError e) { // Too bad localImage = null; tooBigPicture(); } } // If isPicture // We store it, if asked to. if (shadow) { this.offscreenImage = localImage; } freeMemory("end of " + this.getClass().getName() + ".getImage()"); // The picture is now loaded. We clear the progressBar this.uploadPolicy.getApplet().getUploadPanel().getProgressBar().setValue(0); return localImage; } // getImage
/** * @return this method will return the height of the Display window. * <p>If running in fullscreen mode it will return the height of the current set DisplayMode. * If Display.setParent(Canvas parent) is being used, the height of the parent will be * returned. * <p>This value will be updated after a call to Display.update(). */ public static int getHeight() { if (Display.isFullscreen()) { return Display.getDisplayMode().getHeight(); } if (parent != null) { return parent.getHeight(); } return height; }
private static DisplayMode getEffectiveMode() { return !isFullscreen() && parent != null ? new DisplayMode(parent.getWidth(), parent.getHeight()) : current_mode; }
@Override public void run() { // set the timer MainTimer.Set(); // frame counter for fps float Frames = 0; float Milliseconds = 0; // L-System test LSystem Dragon = new LSystem(); List<GameLine> DragonLines = null; try { Dragon.AddVariable('X'); Dragon.AddVariable('Y'); Dragon.AddConstant('+'); Dragon.AddConstant('-'); Dragon.AddConstant('F'); Dragon.DefineDrawingRule('F', true, false, 0); Dragon.DefineDrawingRule('+', false, true, 90); Dragon.DefineDrawingRule('-', false, true, -90); Dragon.AddRule(new ProductionRule("X", "X+YF")); Dragon.AddRule(new ProductionRule("Y", "FX-Y")); String Seed = "FX"; for (int i = 0; i < 11; i++) { Seed = Dragon.ApplyRules(Seed); } // DragonLines = Dragon.ApplyDraw(Seed, 20, 200, 10,0); // System.out.println(Seed); } catch (Exception ex) { System.out.println(ex); } LSystem Sierpinski = new LSystem(); List<GameLine> SierpinskiLines = null; try { Sierpinski.AddVariable('A'); Sierpinski.AddVariable('B'); Sierpinski.AddConstant('-'); Sierpinski.AddConstant('+'); Sierpinski.AddRule(new ProductionRule("A", "B-A-B")); Sierpinski.AddRule(new ProductionRule("B", "A+B+A")); Sierpinski.DefineDrawingRule('A', true, false, 0); Sierpinski.DefineDrawingRule('B', true, false, 0); Sierpinski.DefineDrawingRule('+', false, true, 60); Sierpinski.DefineDrawingRule('-', false, true, -60); String Seed = "A"; for (int i = 0; i < 6; i++) { Seed = Sierpinski.ApplyRules(Seed); } SierpinskiLines = Sierpinski.ApplyDraw(Seed, 20, 200, 10, 0); // System.out.println(Seed); } catch (Exception ex) { System.out.println(ex); } LSystem Plant = new LSystem(); List<GameLine> PlantLines = null; try { Plant.AddVariable('X'); Plant.AddVariable('F'); Plant.AddConstant('-'); Plant.AddConstant('+'); Plant.AddConstant('['); Plant.AddConstant(']'); Plant.AddRule(new ProductionRule("X", "F-[[X]+X]+F[+FX]-X")); Plant.AddRule(new ProductionRule("F", "FF")); Plant.DefineDrawingRule('F', true, false, 0); Plant.DefineDrawingRule('-', false, true, -25); Plant.DefineDrawingRule('+', false, true, 25); Plant.DefineStackRule('[', true); Plant.DefineStackRule(']', false); String Seed = "X"; for (int i = 0; i < 5; i++) { Seed = Plant.ApplyRules(Seed); } PlantLines = Plant.ApplyDraw(Seed, 200, 480, 10, 90); System.out.println(Seed); } catch (Exception ex) { System.out.println(ex); } int count = 0; for (GameLine line : PlantLines) { // Environment.AddLine(line); // count++; // if(count > 5) break; } // testing // GameRectangle rect1 = new GameRectangle(50,50,100,100); // GameRectangle rect2 = new GameRectangle(200,50,100,100); // GameLine line1 = new GameLine(50,50,150,150); // Environment.AddRectangle(rect1); // Environment.AddRectangle(rect2); // Environment.AddLine(line1); while (Run) { if (doRender) { // check to see if enough time has elapsed for the next frame if (MainTimer.Tick() >= ((float) 1 / DesiredFPS) * 1000) { // reset the frame timer MainTimer.Set(); // increase frame count Frames++; // fps timer details Milliseconds += MainTimer.FPSTick(); if (Milliseconds >= 1000) { ActualFPS = Frames / ((float) Milliseconds / 1000f); Frames = 0; Milliseconds = 0; } // do the actual rendering here try { // setting up for drawring MainImage = MainGraphicsConfiguration.createCompatibleImage( MainCanvas.getWidth(), MainCanvas.getHeight()); // clear back buffer... G2D = MainImage.createGraphics(); G2D.setColor(Color.WHITE); G2D.fillRect(0, 0, MainCanvas.getWidth(), MainCanvas.getHeight()); // input handling float xDelta = 0; float yDelta = 0; if (MainInputManager.checkKey('w')) yDelta += 1; if (MainInputManager.checkKey('s')) yDelta -= 1; if (MainInputManager.checkKey('a')) xDelta += 1; if (MainInputManager.checkKey('d')) xDelta -= 1; BigDecimal MouseWheelDelta = new BigDecimal("0.025"); MouseWheelDelta = MouseWheelDelta.setScale(10, RoundingMode.HALF_UP); MouseWheelDelta = MouseWheelDelta.multiply( new BigDecimal(Integer.toString(MainInputManager.GetMouseWheelDelta()))); // mouse checking if (MainInputManager.GetMouseButton(1)) { float x = MainInputManager.GetMousePosition()[0]; float y = MainInputManager.GetMousePosition()[1]; Point2D p = new Point2D.Float(x, y); // if(rect1.contains(p)) System.out.println("Inside dat box"); System.out.println(x + " " + y); } // check on button status if (!MainInputManager.CheckButton()) { System.out.println("Checking Button"); String LSystem = MainInputManager.HandleButton(); int level = MainInputManager.GetSliderValue(); System.out.println(LSystem + " Handled at level: " + level); if (LSystem == "Dragon") { String Seed = "FX"; for (int i = 0; i < level; i++) { Seed = Dragon.ApplyRules(Seed); } DragonLines = Dragon.ApplyDraw(Seed, 450, 300, 10, 90); Environment.ClearLines(); for (GameLine l : DragonLines) Environment.AddLine(l); } if (LSystem == "Sierpinski") { String Seed = "A"; for (int i = 0; i < level; i++) { Seed = Sierpinski.ApplyRules(Seed); } SierpinskiLines = Sierpinski.ApplyDraw(Seed, 100, 200, 10, 0); Environment.ClearLines(); for (GameLine l : SierpinskiLines) Environment.AddLine(l); } if (LSystem == "Plant") { String Seed = "X"; for (int i = 0; i < level; i++) { Seed = Plant.ApplyRules(Seed); } PlantLines = Plant.ApplyDraw(Seed, 350, 480, 10, 90); Environment.ClearLines(); for (GameLine l : PlantLines) Environment.AddLine(l); } } // here goes the bulk of the render code float CenterX = MainCanvas.getWidth() / 2f; float CenterY = MainCanvas.getHeight() / 2f; Point2D Center = new Point2D.Float(CenterX, CenterY); if (MouseWheelDelta.compareTo(new BigDecimal("0")) != 0) { Environment.Zoom(MouseWheelDelta.multiply(new BigDecimal(-1)), Center); } if (xDelta != 0 || yDelta != 0) { Environment.Shift(xDelta, yDelta, Center); } G2D.setColor(Color.black); Environment.Draw(G2D); // display frames per second... G2D.setFont(new Font("Courier New", Font.PLAIN, 12)); G2D.setColor(Color.DARK_GRAY); G2D.drawString(String.format("FPS: %s", ActualFPS), 20, 20); // Blit image and flip... MainGraphics = MainBuffer.getDrawGraphics(); MainGraphics.drawImage(MainImage, 0, 0, null); if (!MainBuffer.contentsLost()) MainBuffer.show(); } catch (Exception ex) { System.out.println(ex.toString()); } finally { // release resources if (MainGraphics != null) MainGraphics.dispose(); if (G2D != null) G2D.dispose(); } // finally // Let the OS have a little time... Thread.yield(); } // if enough time has elapsed since the last frame } // if we want to render } // while we're running }