How can I draw a rectangle towards the top of the application in java
Asked Answered
P

3

6

How can I draw a rectangle towards the top of the application in java ? Normally the drawRect method draws towards the bottom I tried to use a negative number but this would not work

Graphics g = p.getGraphics();
g.fillRect(50, 200,50,100);
Pires answered 19/9, 2012 at 22:42 Comment(3)
Show us an image of what you are getting and what you want.Ovi
Post a small runnable example and you might get an answer that actually helps you.Convertible
g.fillRect(0,0,10,10) will put the rectangle in the upper left corner.Uzial
U
10

In rectangles, the X and Y coordinates represent the top left corner. 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. 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:

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.

Undercast answered 19/9, 2012 at 22:48 Comment(3)
I should mention, the edges can only be positive, you can't use a negative number (as you have found). You have to perform the subtraction before the rectangle can be drawn.Undercast
so you there is no build in possibility of rendering the rectangle to the top instead of the bottom. Coz I assumed this would work : g.fillRect(50, 100, 50, -100);Pires
That's about right. The sides must be positive. Width and Height specify the length of the line, not a vector (direction + distance), and a negative line length doesn't really make sense.Undercast
T
2

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

Also, Java doesn't allow you to use negative values for widths and heights. 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. Positive y is down, not up (though positive x is still right, like standard Cartesian plots).

That being said, the fields for drawRect and fillRect are:

  1. The x-coordinate of the top-left corner of the rectangle
  2. The y-coordinate of the top-left corner of the rectangle
  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."

Tips answered 19/9, 2012 at 23:4 Comment(0)
C
1

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....

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

}
Creath answered 4/2, 2017 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.