gpt4 book ai didi

Java 在窗口边界下绘制图形

转载 作者:行者123 更新时间:2023-12-02 07:22:06 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的 Java Applet/应用程序,它将根据它的大小调整自身大小。我基本上可以正常工作,除了当我在 JFrame grpahics (jframe.getGraphics()) 上绘制某些内容时,在点 0,0 处,它似乎从窗口边界之外的左上角开始。前 30 个像素左右位于窗口顶栏下方。 jframe.getWidth() 和 jframe.getHeight() 返回给我的是窗口的宽度和高度,而不是窗口内的可见部分。

这是我正在使用的代码的摘录,它演示了我的问题:

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 是我创建的一个接口(interface),它具有 Paint(Graphcis g) 方法。

我正在寻找一种方法来查找可见部分的宽度和高度,以及offset 我需要找到可见部分左上角的点。

最佳答案

您必须添加自定义 JPanel 并覆盖 getPreferredSize,因为 JFrame 会考虑装饰(标题栏等)。

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

然后只需将面板添加到框架中并 pack();框架。

关于Java 在窗口边界下绘制图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14086745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com