简体   繁体   English

基本Java图形和坐标无法正常工作

[英]Basic Java Graphics and coordinates not working properly

I've done quite a bit with the acm graphics library in the past and I've just begun working on the breakout problem that Stanford assigns. 我过去在acm图形库上做了很多,我刚刚开始研究斯坦福分配的突破问题。 One of the issues which I've had several times in the past with graphics is the coordinates do not accurately reflect where the shapes are actually appearing although I've double and triple checked them. 我过去曾经多次使用图形的问题之一是坐标不能准确地反映出形状实际出现的位置,尽管我已经对它们进行了双重和三重检查。 In the end, for those assignments I ended up eyeballing it and making modifications until it look kind of right. 最后,对于那些作业,我最终注视并进行修改,直到它看起来很正确。 So obvious I'm missing something here cause a simple graphics program should not be this difficult. 很明显我在这里遗漏了一些东西导致简单的图形程序不应该这么困难。

For example, using acm.graphics my code looks like this. 例如,使用acm.graphics我的代码看起来像这样。

public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;

public void setupBorder(){
    // **Sets Black Background
    GRect b = new GRect(0,0,400,600);  //** Sets Black Background
    fillObj(b,Color.black);
    add(b);

    //**Sets White Square so border is 5 pixels wide on each side
    b = new GRect(5,5,APPLICATION_WIDTH-10,APPLICATION_HEIGHT-10);
    fillObj(b,Color.white);
    add(b);
    }

So my thinking here, is its moved 5 pixels right and 5 pixels down I need to subtract those and then an extra 5 pixels to have a border 5 pixels on each side (basic 2b+x = 1 side, 2a+y= vertical side). 所以我的想法是,它向右移动5个像素,向下移动5个像素我需要减去那些然后额外的5个像素,每边有一个边框5个像素(基本2b + x = 1边,2a + y =垂直边)。 You would think this creates a black border 5 pixels on each side with a white square in the middle, but it doesn't. 您会认为这会在每侧创建一个黑色边框5像素,中间有一个白色方块,但事实并非如此。 It creates a border on the left, right, and top side of the window but not the bottom. 它在窗口的左侧,右侧和顶侧创建边框,但不在底部。 So with something this simple, what can possibly go wrong? 有了这么简单的事情,可能出现什么问题? I've double, triple, even quad checked the calculations on paper and it should be a white square in the middle with a 5 pixel black border, but it isn't. 我有双倍,三倍,甚至四倍检查纸上的计算,它应该是一个中间的白色正方形,有一个5像素的黑色边框,但事实并非如此。 I've tried changing pixels for percentages, with worse effect. 我已经尝试更改百分比的像素,效果更差。 Manually editing the box till it looks close at about -35 for the Yvalue although that's just a bit too far. 手动编辑框,直到它看起来接近-Y值约为-35,尽管这有点太过分了。 Anyone have an idea about what's going wrong? 任何人都知道出了什么问题?

APPWINDOW

Breakout with these changes made to run and other methods within class. 突破这些更改运行和类中的其他方法。

public void run() {
    /* You fill this in, along with any subsidiary methods */
    init();
}

public void init(){

    setupBorder();


}

public void setupBorder(){
    //**Black Background
    GRect b = new GRect(0,0,400,600);  //**Black Background
    fillObj(b,Color.black);
    add(b);

    b = new GRect(5,5,APPLICATION_WIDTH-10,APPLICATION_HEIGHT-10);
    fillObj(b,Color.white);
    add(b);
}

public void fillObj(GFillable a, Color argC)
{
    a.setFillColor(argC);
    a.setFilled(true);
}

It looks like you're confusing the size of the application window (excluding the window border but including the menu, and which is 400*600 in your case) with the size of your drawable area (excluding the menu), which means you're painting outside the drawable area (which is below the menu. 看起来你混淆了应用程序窗口的大小(不包括窗口边框,但包括菜单,在你的情况下 400 * 600)与可绘制区域的大小(不包括菜单),这意味着你'在可绘制区域(位于菜单下方)之外重新绘画。

Your window has: 你的窗口有:

  • a border supplied by the OS. 操作系统提供的边框。 It is 400x600 on the inside. 它内部是400x600。
  • a menu bar. 一个菜单栏。
  • a drawable area (canvas) below the the menu bar. 菜单栏下方的可绘制区域(画布)。 It is shorter than 400x600 by the menu bar width. 菜单栏宽度小于400x600。

You either 你要么

  • need to read the canvas size and use it instead of the window size: getCanvas().getHeight() 需要读取画布大小并使用它而不是窗口大小: getCanvas().getHeight()
  • need to set the canvas size instead of the window size and let the window size itself as needed. 需要设置画布大小而不是窗口大小,并根据需要调整窗口大小。 As GraphicsProgram is an Applet , this doesn't seem possible. 由于GraphicsProgram是一个Applet ,这似乎不可能。
  • need to find out the menu height and increase the window height by that amount (last resort). 需要找出菜单高度并增加窗口高度(最后的手段)。

The size of the window can be changed using the setSize() method. 可以使用setSize()方法更改窗口的大小。 Doing than, you are still facing a coordinates issue. 不然,你仍然面临着一个坐标问题。 When reading the size of the working area, it is constantly decreased by 16 for the width, and 58 for the heigth (probably due to the menu and W7 borders, as mentioned before). 当读取工作区域的大小时,宽度不断减少16,高度减少58(可能是由于菜单和W7边框,如前所述)。

You just have to take care of it when calling setSize(). 调用setSize()时只需要处理它。

[OFF]I'm also working on this breakout assignment and had the same issue. [OFF]我也正在进行这项突破性任务并遇到同样的问题。 Posted a few minutes ago on this site.[/OFF] 几分钟前发布在这个网站上。[/ OFF]

I've made it a habit to explicitly set application size @ the load-time of all my applets. 我养成了一个习惯,即明确设置应用程序大小@所有applet的加载时间。 This is how I usually set window size: 这就是我通常设置窗口大小的方法:

@Override

public void init(){
    resize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
}

Also, you don't need to call init() from run() ; 此外,您不需要从run()调用init () ; it's automatically called by your appletviewer at initialization time. 它在初始化时由appletviewer自动调用。

I think you should use: 我认为你应该使用:

b = new GRect(5,5,getWidth()-10,getHeight()-10); //gets window width & height

or::: 要么:::

b = new GRect(5,5,b.getWidth()-10,b.getHeight()-10); //gets former 'b' width & height

instead of: 代替:

b = new GRect(5,5,APPLICATION_WIDTH-10,APPLICATION_HEIGHT-10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM