-2

When I draw lines in my program the position is perfect, but when I use those same coordinates for a square or oval they are way off the mark. My code is:

g2d.drawRect(one1, one2, two1, two2);
g2d.drawOval(one1, one2, two1, two2);

And the points are gather by:

one1 = (int)e.getX();
one2 = (int)e.getY();

This is a follow on to a question I previously asked.

Community
  • 1
  • 1
James MV
  • 8,569
  • 17
  • 65
  • 96
  • 2
    An [SSCCE](http://sscce.org) would help greatly here. Otherwise we're playing the "guess what's in my code" game, a game that most of us don't play well. – Hovercraft Full Of Eels Dec 13 '11 at 20:14
  • 1
    Can you tell us what your expectations are? Give us more of an idea of what you want and what you are getting. – jbranchaud Dec 13 '11 at 20:14
  • Theres nothing else to say, I use a MouseListener to grab the first click, then the second, these are assigned as one1 one2 for the first clicks x and y and then the same happens for click two. After click two it runs that line of code to draw a rect or an oval and it is drawn in a JLabel. I wish my program was a little smaller and I could show it all, but its huge. This particular element is tiny thou. Any general ideas as what could cause coords that work for a line but not for a square or oval would be appreciated. – James MV Dec 13 '11 at 20:19
  • I'm not asking you to say anything. I'm asking for a short compilable and runnable program that shows code that reproduces the problem. Otherwise all you're going to get are guesses. We're not asking to see your huge program -- we're asking to see the problem. **Please read the link that I've provided for more on this.** Again, it's [SSCCE](http://sscce.org). Someone may get lucky and guess right, but that's not the best way to solve these things usually. – Hovercraft Full Of Eels Dec 13 '11 at 20:28
  • possible duplicate of [Lines aren't drawing exactly where I clicked?](http://stackoverflow.com/questions/8479432/lines-arent-drawing-exactly-where-i-clicked) – Jonas Dec 13 '11 at 20:31
  • 2
    I recommend to read the documentation for the methods you use, so you understand what they do. – Jonas Dec 13 '11 at 20:33
  • @Jonas: amen. I'll bet he's using the methods wrong, but without more code, who the héll knows. – Hovercraft Full Of Eels Dec 13 '11 at 21:11
  • Once again, you didn't post an SSCCE. Why do you have to be asked every time???? Make an effort to ask a proper question with complete details – camickr Dec 14 '11 at 02:09
  • 1
    I don't see what the fuss is about, I've showed what I think was relevant and people have been able to point out what was wrong. Job done. – James MV Dec 14 '11 at 12:25

3 Answers3

3

Alright, I got what your problem is. If you see the below image, the way that the parameters taken by oval and sqaure are different from the line.

To draw a line --> You will have to specify the starting point and the ending point. Just passing them directly to the Graphics object would do the job for you. However for a Square or Oval, it is different. You first click will grab a point and then you should do some manipulation on what should be the output when you do the second click. The second click should not be considered as a co-ordinate into the drawOval() or drawRect() methods directly.

Because the Parameter for these methods are

x, y, width, height

Whereas you are getting

x1, y1  and x2, y2

enter image description here

package sof;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class DrawTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MyComponent());
        frame.setSize(260, 280);
        frame.setVisible(true);
    }
}

class MyComponent extends JComponent {
    public void paint(Graphics g) {
        int height = 120;
        int width = 120;
        g.setColor(Color.black);
        g.drawOval(60, 60, width, height);
        g.drawRect(60, 60, width, height);
        g.drawLine(0,0,50,50);
    }
}
bragboy
  • 34,892
  • 30
  • 114
  • 171
1

I think I understand what your issue is. You are having the user click in two different spots on the canvas and then you want to draw a rectangle/oval using those points. So if the user clicks at 10,10 and then clicks at 20,20, then you want a rectangle whose top left corner is at 10,10 and whose bottom right corner is at 20,20.

If this is in fact what you are asking, then here is my proposed solution:

Event e1 = (the first click)
Event e2 = (the second click)

// Figure out where the user clicked
int x1 = (int)e1.getX();
int y1 = (int)e1.getY();

int x2 = (int)e2.getX();
int y2 = (int)e2.getY();

int xCoord;
int yCoord;

// Figure out the coordinates
if(x1 < x2)
    xCoord = x1;
else
    xCoord = x2;

if(y1 < y2)
    yCoord = y1;
else
    yCoord = y2;

// Figure out the size of the object
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);

// Finally draw your objects
g2d.drawRect(xCoord, yCoord, width, height);
g2d.drawOval(xCoord, yCoord, width, height);

That should work for you based on my understanding of your question.

jbranchaud
  • 5,909
  • 9
  • 45
  • 70
  • Thanks Treebranch, sorry I can't mark two answers as correct as this is just as helpful as the marked answer. – James MV Dec 13 '11 at 22:09
0

My guess is that you're capturing the X & Y coordinates for a container (say 200,200) then creating the oval/rect within a NEW container; you state a JLabel above in your comment.

If you're capturing X & Y to be 200,200 for a JPanel, then creating a JLabel and assigning the component an X/Y of 200,200 it is the coordinate within its new component, not the parent in which you captured the X/Y.

Can you post the code where the MouseListener is initiated as well as when the JLabel is created if this is wrong? As others have said we need more code to work from. An image example would be equally beneficial!

Grambot
  • 4,370
  • 5
  • 28
  • 43