简体   繁体   English

如何在java中向应用程序顶部绘制一个矩形

[英]How can I draw a rectangle towards the top of the application in java

How can I draw a rectangle towards the top of the application in java ? 如何在java中向应用程序顶部绘制一个矩形? Normally the drawRect method draws towards the bottom I tried to use a negative number but this would not work 通常drawRect方法向底部绘制我尝试使用负数但这不起作用

Graphics g = p.getGraphics();
g.fillRect(50, 200,50,100);

In rectangles, the X and Y coordinates represent the top left corner. 在矩形中,X和Y坐标代表左上角。 The length and width then draw away from the defining point. 然后长度和宽度从定义点开始。 Your example draws a rectangle with the top left corner at 50,200 and with a width of 50 and a hight of 100, both away from those points in a positive direction. 您的示例绘制一个矩形,其左上角为50,200,宽度为50,高度为100,两者都偏离正方向。 If you wanted a rectangle with 50,200 representing the lower left corner, simply subtract the height from that y coordinate (200), and use that as the starting y: 如果你想要一个50,200表示左下角的矩形,只需从该y坐标(200)中减去高度,并将其用作起始y:

Graphics g = p.getGraphics();
g.fillRect(50, 100, 50, 100);

To address your examples, try something like this (I'll just use rectangle objects rather than actually filling the graphics): 要解决你的例子,尝试这样的事情(我只使用矩形对象而不是实际填充图形):

int baseline = 200;
Rectangle rect1 = new Rectangle(50, baseline - 100, 50, 100);
Rectangle rect2 = new Rectangle(150, baseline - 50, 50, 50);
Rectangle rect3 = new Rectangle(250, baseline - 80, 50, 80);

After filling rectangles with these dimensions on the graphics object, you'll have three rectangles with a width of 50 each, spaced 50 apart, with the bottom all on the y=200 line. 在图形对象上填充具有这些尺寸的矩形后,您将拥有三个矩形,每个矩形宽度为50,间距为50,底部全部在y = 200线上。

Java's Graphics class assumes that the origin (0, 0) is the top-left corner of the frame, that is, that (1, 1) is down and to the right of (0, 0) . Java的Graphics类假定原点(0, 0)是帧的左上角,即(1, 1)是向下和向右(0, 0) This is contrary to mathematics, in which the origin in a standard Cartesian plane is the lower-left corner, and (1, 1) is above and to the right of (0, 0) . 这与数学相反,其中标准笛卡尔平面中的原点是左下角, (1, 1)(0, 0) (1, 1)的上方和右侧。

Also, Java doesn't allow you to use negative values for widths and heights. 此外,Java不允许您对宽度和高度使用负值。 This results in special logic that typically treats the rectangle differently than a normal, positive-dimension rectangle. 这导致特殊逻辑通常以不同于正常的正尺寸矩形来处理矩形。

To get what you want, first reverse your thinking about the y-coordinate in Java's Graphics coordinate system. 为了得到你想要的东西,首先要反思你对Java Graphics坐标系中y坐标的思考。 Positive y is down, not up (though positive x is still right, like standard Cartesian plots). 正y是向下的,而不是向上的(虽然正x仍然是正确的,就像标准的笛卡尔图一样)。

That being said, the fields for drawRect and fillRect are: 话虽这么说, drawRectfillRect的字段是:

  1. The x-coordinate of the top-left corner of the rectangle 矩形左上角的x坐标
  2. The y-coordinate of the top-left corner of the rectangle 矩形左上角的y坐标
  3. The positive width of the rectangle 矩形的正宽度
  4. The positive height of the rectangle 矩形的正高度

Zoe's answer shows a good example of how to get what you want, I just thought you might like a more thorough explanation of why "the drawRect method draws towards the bottom." 佐伊的答案显示了如何得到你想要的一个很好的例子,我只是想你可能想要更彻底地解释为什么“ drawRect方法走向最底层”。

Run it.. Drag and drop the mouse in any direction on the applet window..and see what's happening... Hope you will get some idea from it.... 运行它..在applet窗口的任何方向上拖放鼠标..看看发生了什么......希望你能从中得到一些想法....

//Simulation of the desktop screen

package raj_java;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class DesktopDemo extends Applet implements MouseListener, MouseMotionListener {

    int x1, y1, x2, y2;
    Image img;

    public void init() {
        setSize(1200, 700);
        setVisible(true);
        img = getImage(getCodeBase(), "Nature.jpg");
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void mouseEntered(MouseEvent me) {
        //blank
    }

    public void mouseExited(MouseEvent me) {
        //blank
    }

    public void mouseClicked(MouseEvent me) {
        //blank
    }

    public void mouseReleased(MouseEvent me) {
        Graphics g = getGraphics();
        g.drawImage(img, 0, 0, 1200, 700, this);
    }

    public void mouseMoved(MouseEvent me) {
        //blank
    }

    public void mousePressed(MouseEvent me) {
        x1 = me.getX();
        y1 = me.getY();
    }

    public void mouseDragged(MouseEvent me) {
        x2 = me.getX();
        y2 = me.getY();
        repaint();
    }

    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, 1200, 700, this);
        g.setColor(new Color(10, 99, 126));
        g.drawLine(x1, y1, x2, y1);
        g.drawLine(x2, y1, x2, y2);
        g.drawLine(x2, y2, x1, y2);
        g.drawLine(x1, y2, x1, y1);
        g.setColor(new Color(193, 214, 220, 70));
        int width = Math.abs(x2 - x1);
        int height = Math.abs(y2 - y1);
        if(x2 < x1) {
            g.fillRect(x2, y1, width, height);
        }else if(y2 < y1) {
            g.fillRect(x1, y2, width, height);
        }else {
            g.fillRect(x1, y1, width, height);
        }
    }

}

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

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