• Advertisement

Archived

This topic is now archived and is closed to further replies.

Problem with Java graphics

This topic is 5028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a problem when I try to make a retangle with the mouse on the screen, it works only from up-left to the down right, and doesn't work to another direction. Please help me. here's the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Retang extends JFrame{
  private int a,b,c,d,x1,y1,x2,y2;
  private boolean z;
  public Retang(){
    super("Criar Retangulo");
    addMouseListener(
      new MouseAdapter(){
        public void mousePressed(MouseEvent e){
          x1=e.getX();
          y1=e.getY();
          z=false;
        }
        public void mouseReleased(MouseEvent e){
          x2=e.getX();
          y2=e.getY();
          z=true;
          repaint();
        }
    });
    x1=y1=x2=y2=0;
    z=false;
    setSize(400,400);
    setLocation(200,100);
    setVisible(true);
  }
  public void paint(Graphics g){
    super.paint(g);
    a=x1;b=y1;c=x2;d=y2;
    if(z){
      if ((a>c) && (b>d)) {
        g.setColor(Color.black);
        g.drawRect(x1,y1,x2-x1,y2-y1);
        g.setColor(Color.white);
        g.fillRect(x1+1,y1+1,x2-x1-1,y2-y1-1);
      }
      else if ((a>c) && (b<d)) {
        g.setColor(Color.black);
        g.drawRect(x1,y1,y2-2*x1,y2-y1);
        g.setColor(Color.white);
        g.fillRect(x1+1,y1+1,x2-x1-1,y2-y1-1);
      }
      else if ((a<c) && (b>d)) {
        g.setColor(Color.black);
        g.drawRect(x1,y1,x2-x1,y2-y1);
        g.setColor(Color.white);
        g.fillRect(x1+1,y1+1,x2-x1-1,y2-y1-1);
      }
      else if ((a<c) && (b<d)) {
        g.setColor(Color.black);
        g.drawRect(x1,y1,x2-x1,y2-y1);
        g.setColor(Color.white);
        g.fillRect(x1+1,y1+1,x2-x1-1,y2-y1-1);
      }
    }
  }
  public static void main(String[]Arg){
    Retang app = new Retang();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}
I have no idea of what is wrong... Barnabé, The Adventurer [edited by - Barnabe on May 19, 2004 7:10:57 PM]

Share this post


Link to post
Share on other sites
Advertisement
You''re making a simple operation too complicated. Basically, you want the min x in x1 and max x in x2 (y1 and y2 also). So swap them and find the difference;


public void paint(Graphics g)
if(x1>x2) {
temp=x1;
x1=x2;
x2=temp;
}
if(y1>y2) {
temp=y1;
y1=y2;
y2=temp;
}
width=x2-x1;
height=y2-y1;
g.setColor();
g.fillRect(x1,y1,width,height);
}

Share this post


Link to post
Share on other sites
I really don't get it!
I mean.. you're right, I must to know max x and y but i need more than that.. coz I have to make the retangle...its weird to me that its not working, I know that I shouldn't make that ints abcd but.. I want to know where is the main mistake that makes the other retangles dont apear..can you send explain it better please?

[edited by - Barnabe on May 19, 2004 7:29:21 PM]

Share this post


Link to post
Share on other sites
Thanks dude,
now I understand it.
it really helped me.

_______________________
Barnabé, The Adventurer

Share this post


Link to post
Share on other sites

  • Advertisement