How to draw a rectangle at the top of an application in java

How to draw a rectangle at the top of an application in java? Usually the drawRect method goes to the bottom, I tried to use a negative number, but that didn't work

Graphics g = p.getGraphics();
g.fillRect(50, 200,50,100);
+5
source share
3 answers

In the rectangles, the X and Y coordinates are the upper left corner. Length and width are then distracted from the defining point. Your example draws a rectangle with an upper left corner of 50,200 and a width of 50 and a height of 100, both of which are in the positive direction. If you need a rectangle with 50,200 representing the bottom left corner, just subtract the height from this y coordinate (200) and use this as the beginning of y:

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

, - ( , ):

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);

50 , 50 , - y = 200.

+9

Java Graphics , (0, 0) , (1, 1) (0, 0). , , (1, 1) (0, 0).

, Java . , , .

, , y Java Graphics. y - , ( x - , ).

drawRect fillRect:

  • -
  • y

Zoe , , , , , drawRect .

+2

Run it. Drag the mouse in any direction in the applet window .. and see what happens ... I hope you get some idea from it ....

//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);
        }
    }

}
0
source

All Articles