Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ? 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);
like image 574
user1035654 Avatar asked Sep 19 '12 22:09

user1035654


People also ask

How do you draw a rectangle in Java?

Similarly, we will draw a rectangle on Java applet by two ways . By using the drawRect(int x, int y, int width, int height) or by drawing four lines joining the edges . Examples: We will draw a rectangle of height 200 and width 200 and At a position 100,100 on the applet.

Which class is used to draw a rectangle on the frame?

There we declare two classes, one class is MyPanel and other Class is Test. In class MyPanel we use drawRect( ) & fillRect( ) mathods to draw rectangle and fill Color in it. We set the color by setColor(Color.


3 Answers

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.

like image 83
Zoe Avatar answered Oct 04 '22 16:10

Zoe


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

like image 40
Brian Avatar answered Oct 04 '22 17:10

Brian


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

}
like image 23
Rajalaxmi Mishra Avatar answered Oct 04 '22 18:10

Rajalaxmi Mishra