2012-12-29 52 views
0

我正在嘗試編寫一個簡單的Java Applet/Application,它將根據它的大小自行調整大小。除了當我在JFrame grpahics(jframe.getGraphics())上繪製某些東西時,我已經基本上工作了,在0,0點,它似乎開始於窗口邊界外的左上角。窗口頂部欄下方的前30個像素。此外,jframe.getWidth()和jframe.getHeight()向我返回窗口的寬度和高度,而不是窗口內的可見部分。Java繪製窗口邊界下的圖形

這是代碼的摘錄,我使用演示我的問題:

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.image.BufferedImage; 

public class ImageHandler { 
    private boolean debug; 
    private int rememberWidth, rememberHeight, contractedImageX, 
      contractedImageY, imageTranslationX, imageTranslationY, realWidth, 
      realHeight, fixedWidth, fixedHeight; 
    private BufferedImage bImage1, bImage2; 
    private Graphics2D bufferedGraphics1, bufferedGraphics2; 
    private boolean resizeNext, isApplet; 

    public ImageHandler(ObjectHandler objH) { 
     debug = objH.isDebug(); 
     objH.setImageHandler(this); 
     objectHandler = objH; 
     isApplet = objectHandler.isApplet(); 
     fixedWidth = objectHandler.getScreenWidth(); 
     fixedHeight = objectHandler.getScreenHeight(); 
    } 

    public void paint(Paintable p) { 
     /* 
     * If the screen is not the same size at remembered, then re-run the 
     * image transformations 
     */ 
     realWidth = getRealWidth(); 
     realHeight = getRealHeight(); 
     if (objectHandler.isFocused() == true 
       || objectHandler.isApplet() == true) { 
      if (!(rememberWidth == realWidth && rememberHeight == realHeight) 
        || resizeNext) { 
       resizeNext = false; 
       if (debug) 
        System.out.println("Re-sizing"); 
       /* Create New Images */ 
       bImage1 = new BufferedImage(realWidth, realHeight, 
         BufferedImage.TYPE_INT_ARGB); 
       bImage2 = new BufferedImage(fixedWidth, fixedHeight, 
         BufferedImage.TYPE_INT_ARGB); 
       bufferedGraphics1 = (Graphics2D) bImage1.getGraphics(); 
       bufferedGraphics1.setColor(Color.black); 
       bufferedGraphics1.fillRect(0, 0, realWidth, realHeight); 
       bufferedGraphics2 = (Graphics2D) bImage2.getGraphics(); 
       /* 
       * Remember The current Height and width, so that it can check 
       * if the height has changed before running this again 
       */ 
       rememberWidth = realWidth; 
       rememberHeight = realHeight; 
       /* 
       * Define contractedImageX and y depending on the height of the 
       * screen 
       */ 
       contractedImageY = realHeight; 
       contractedImageX = (int) ((double) contractedImageY 
         /(double) fixedHeight * fixedWidth); 
       /* 
       * If the graphics defined by using the height make it go off 
       * the sides of the screen, redefine with the width 
       */ 
       if (debug) { 
        System.out.println("1Real Height:" + realHeight + " Width:" 
          + realWidth + " contractedHeight:" 
          + contractedImageY + " contractedWidth:" 
          + contractedImageX); 
       } 
       if (contractedImageX > realWidth) { 
        contractedImageX = realWidth; 
        contractedImageY = (int) ((double) contractedImageX 
          /(double) fixedWidth * fixedHeight); 
       } 
       if (debug) { 
        System.out.println("2Real Height:" + realHeight + " Width:" 
          + realWidth + " contractedHeight:" 
          + contractedImageY + " contractedWidth:" 
          + contractedImageX); 
       } 
       /* 
       * Re Calculate Image Translations so that they position the 
       * image correctly 
       */ 
       imageTranslationX = (realWidth - contractedImageX)/2; 
       imageTranslationY = (realHeight - contractedImageY)/2; 
       if (debug) { 
        System.out.println("X: " + imageTranslationX + " Y: " 
          + imageTranslationY); 
       } 

      } 
      // clears the screen 
      bufferedGraphics2.setColor(Color.black); 
      bufferedGraphics2.fillRect(0, 0, fixedWidth, fixedHeight); 
      p.paint(bufferedGraphics2); 
      bufferedGraphics1.setColor(Color.black); 
      bufferedGraphics1.fillRect(0, 0, realWidth, realHeight); 
      bufferedGraphics1.drawImage(bImage2, imageTranslationX, 
        imageTranslationY, contractedImageX + imageTranslationX, 
        contractedImageY + imageTranslationY, 0, 0, fixedWidth, 
        fixedHeight, null); 
      drawFinalImage(bImage1); 
     } 
    } 

    private void drawFinalImage(Image img) { 
     if (isApplet) { 
      if (objectHandler.getjApplet() != null) { 
       objectHandler.getjApplet().getGraphics() 
         .drawImage(img, 0, 0, null); 
      } 
     } else { 
      if (objectHandler.getjFrame() != null) { 
       objectHandler.getjFrame().getGraphics() 
         .drawImage(img, 0, 0, null); 
      } 
     } 
    } 

    private int getRealWidth() { 
     if (objectHandler.isApplet()) { 
      if (objectHandler.getjApplet() != null) { 
       Integer w = objectHandler.getjApplet().getWidth(); 
       if (w != null && w > 0) { 
        return w; 
       } 
      } 
     } else { 
      if (objectHandler.getjFrame() != null) { 
       Integer w = objectHandler.getjFrame().getWidth(); 
       if (w != null && w > 0) { 
        return w; 
       } 
      } 
     } 
     return fixedWidth; 
    } 

    private int getRealHeight() { 
     if (objectHandler.isApplet()) { 
      if (objectHandler.getjApplet() != null) { 
       Integer h = objectHandler.getjApplet().getHeight(); 
       if (h != null && h > 0) { 
        return h; 
       } 
      } 
     } else { 
      if (objectHandler.getjFrame() != null) { 
       Integer h = objectHandler.getjFrame().getHeight(); 
       if (h != null && h > 0) { 
        return h; 
       } 
      } 
     } 
     return fixedHeight; 
    } 
} 

該代碼是不是我的整個程序,只是重新大小的窗口和繪製圖形的一部分。 Paintable是我創建的具有paint方法的接口(Graphcis g)。

我正在尋找一種方法來查找可見部分的寬度和高度,以及我需要找到可見部分左上角的點的偏移量。

回答

2

您必須添加自定義JPanel並覆蓋getPreferredSize,因爲JFrame會將裝飾(標題欄等)考慮在內。

private class MyPan extends JPanel { 
    public Dimension getPreferredSize() { return new Dimension(600, 600); } 
    public void paintComponent(Graphics g) { /* do stuff */ } 
} 

然後只需將面板添加到框架和pack();框架。

+0

謝謝!這工作!我沒有完全做到這一點,但JPanel做了我所需要做的,謝謝! – daboross