Esempio n. 1
0
  public void render(Graphics2D g) {
    do {
      do {
        g = (Graphics2D) strategy.getDrawGraphics();
        g.setRenderingHints(renderingHints);
        vi = createVolatileImage(getWidth(), getHeight());
        bi =
            GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()
                .getDefaultConfiguration()
                .createCompatibleImage(getWidth(), getHeight());
        vi.getGraphics().fillRect(0, 0, vi.getWidth(), vi.getHeight());

        for (Renderable renderable : Window.objects) {
          if (renderable.getShouldDestroy()) Window.objects.remove(renderable);
          if (renderable.isVisible()) renderable.render((Graphics2D) vi.getGraphics());
        }

        bi.getGraphics().drawImage(vi.getSnapshot(), 0, 0, getWidth(), getHeight(), null);
        bi = vi.getSnapshot();
        // gaussian.filter(bi, bi);
        // glow.filter(bi, bi);
        // motionBlur.filter(bi, bi);
        g.drawImage(bi, 0, 0, getWidth(), getHeight(), null);
        g.dispose();
        Toolkit.getDefaultToolkit().sync();
      } while (strategy.contentsRestored());
      strategy.show();
    } while (strategy.contentsLost());
  }
Esempio n. 2
0
 // updates Display
 public void update() {
   Window w = vc.getFullScreenWindow();
   if (w != null) {
     BufferStrategy s = w.getBufferStrategy();
     if (!s.contentsLost()) {
       s.show();
     }
   }
 }
Esempio n. 3
0
 /** En metode som viser grafikken som er blitt tegnet på canvaset. */
 public void update() {
   Window window = getWindow();
   if (window != null) {
     BufferStrategy strategy = window.getBufferStrategy();
     if (!strategy.contentsLost()) {
       strategy.show();
     }
   }
   Toolkit.getDefaultToolkit().sync();
 }
  public void doit() throws Exception {

    Image image = (Image) this.pullInput(0);
    Rectangle bounds = mainFrame.getBounds();
    Graphics g = bufferStrategy.getDrawGraphics();
    if (!bufferStrategy.contentsLost()) {
      g.drawImage(image, 0, 0, null);
      // g.drawImage(image, 0, 0, bounds.width, bounds.height, null);
      // g.fillRect(0, 0, bounds.width, bounds.height);
      bufferStrategy.show();
      g.dispose();
    }
  }
Esempio n. 5
0
 private void animateNextFrame() {
   // Animate next frame on buffer
   Graphics2D g = (Graphics2D) buffer.getDrawGraphics();
   drawFrame(g);
   // Display the buffer
   if (!buffer.contentsLost()) {
     buffer.show();
   }
   g.dispose();
   // Repainting solves the problem of the applet not updating properly
   // on the Red Hat Linux computers.
   repaint();
 }
Esempio n. 6
0
  /** Actualiza la pantalla con el doble buffer e intenta sincronizar la imagen. */
  public void update() {
    Window window = device.getFullScreenWindow();

    if (window != null) {
      BufferStrategy strategy = window.getBufferStrategy();
      if (!strategy.contentsLost()) {
        strategy.show();
      }
    }
    // Sync the display on some systems.
    // (on Linux, this fixes event queue problems)
    Toolkit.getDefaultToolkit().sync();
  }
Esempio n. 7
0
  public void render() {
    if (bufferStrategy != null) {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.drawImage(
          screen,
          0,
          0,
          Configuration.FRAME_DIMENSION.width,
          Configuration.FRAME_DIMENSION.height,
          this);
      g.dispose();

      if (!bufferStrategy.contentsLost()) {
        bufferStrategy.show();
      }
    }
  }
Esempio n. 8
0
  @Override
  public void render() {
    if (!window.isFocused()) {
      return;
    }
    // Render single frame
    do {
      // The following loop ensures that the contents of the drawing buffer
      // are consistent in case the underlying surface was recreated
      do {
        // Get a new graphics context every time through the loop
        // to make sure the strategy is validated
        Graphics2D g2 = (Graphics2D) strategy.getDrawGraphics();

        // Clear rect
        g2.clearRect(0, 0, window.getWidth(), window.getHeight());
        // Render to graphics
        gsm.render(g2);
        // ...

        if (SHOW_INFO) {
          g2.setPaint(Color.WHITE);
          g2.drawString(renderThread.report(), 900, 18);
          g2.drawString(updateThread.report(), 1100, 18);
        }

        // Dispose the graphics
        g2.dispose();

        // Repeat the rendering if the drawing buffer contents
        // were restored
      } while (strategy.contentsRestored());

      // Display the buffer
      strategy.show();

      // Repeat the rendering if the drawing buffer was lost
    } while (strategy.contentsLost());
  }
Esempio n. 9
0
 public MultiBufferTest(int numBuffers, GraphicsDevice device) {
   try {
     GraphicsConfiguration gc = device.getDefaultConfiguration();
     mainFrame = new Frame(gc);
     mainFrame.setUndecorated(true);
     mainFrame.setIgnoreRepaint(true);
     device.setFullScreenWindow(mainFrame);
     //            if (device.isDisplayChangeSupported()) {
     //                chooseBestDisplayMode(device);
     //            }
     Rectangle bounds = mainFrame.getBounds();
     mainFrame.createBufferStrategy(numBuffers);
     BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();
     for (float lag = 2000.0f; lag > 0.00000006f; lag = lag / 1.33f) {
       System.out.println("lag = " + lag);
       for (int i = 0; i < numBuffers; i++) {
         Graphics g = bufferStrategy.getDrawGraphics();
         if (!bufferStrategy.contentsLost()) {
           g.setColor(COLORS[i]);
           //                        g.fillRect(0,0,bounds.width, bounds.height);
           g.fillRect(0, 0, 20, 20);
           bufferStrategy.show();
           g.dispose();
         }
         try {
           Thread.sleep((int) lag);
         } catch (InterruptedException e) {
         }
       }
     }
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   } finally {
     device.setFullScreenWindow(null);
   }
 }
Esempio n. 10
0
  @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
  }