Results 1 to 3 of 3
  1. #1
    gameaddict123 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default Problem with drawing program

    I've been trying to code a java program that allows the user to draw simple lines and rectangles with mouse drags. When I run it, however, and try to draw a line or rectangle, the line or rectangle I drew previously changes position. Here is the code.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
    public class drawmyimg extends JPanel implements MouseListener, MouseMotionListener, ActionListener{
    int x1;
    int y1;
    int x2;
    int y2;
    Vector<Integer>x1s;
    Vector<Integer>x2s;
    Vector<Integer>y1s;
    Vector<Integer>y2s;
    Vector<Integer>rectx1s;
    Vector<Integer>rectx2s;
    Vector<Integer>recty1s;
    Vector<Integer>recty2s;
    int shape;
    JButton line;
    JButton rectangle;
    public drawmyimg(){
    setDoubleBuffered(true);
    setFocusable(true);
    setSize(400, 400);
    setBackground(Color.WHITE);
    x1=0;
    y1=0;
    x2=0;
    y2=0;
    x1s=new Vector<Integer>();
    x2s=new Vector<Integer>();
    y1s=new Vector<Integer>();
    y2s=new Vector<Integer>();
    rectx1s=new Vector<Integer>();
    rectx2s=new Vector<Integer>();
    recty1s=new Vector<Integer>();
    recty2s=new Vector<Integer>();
    line=new JButton("Line");
    rectangle=new JButton("Rectangle");
    shape=0;
    line.addActionListener(this);
    rectangle.addActionListener(this);
    add(rectangle);
    add(line);
    addMouseListener(this);
    addMouseMotionListener(this);
    }
    
    public Vector<Integer> getx1s(){
    return x1s;
    }
    public Vector <Integer> gety1s(){
    return y1s;
    }
    public Vector<Integer>  getx2s(){
    return x2s;
    }
    public Vector <Integer> gety2s(){
    return y2s;
    }
    
    public Vector<Integer> getrectx1s(){
    return rectx1s;
    }
    public Vector <Integer> getrecty1s(){
    return recty1s;
    }
    public Vector<Integer>  getrectx2s(){
    return rectx2s;
    }
    public Vector <Integer> getrecty2s(){
    return recty2s;
    }
    public int getSetShape(){
    return shape;
    }
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==rectangle){
    shape=1;
    }
    if(e.getSource()==line){
    shape=0;
    }
    }
    
    public void mousePressed(MouseEvent e){
    x1=e.getX();
    y1=e.getY();
    Vector<Integer>myx1=getx1s();
    Vector<Integer>myy1=gety1s();
    Vector<Integer>rectmyx1=getrectx1s();
    Vector<Integer>rectmyy1=getrecty1s();
    int myshape=getSetShape();
    if(myshape==0){
    myx1.add(x1);
    myy1.add(y1);
    }
    if(myshape==1){
    rectmyx1.add(x1);
    rectmyy1.add(y1);
    }
    }
    public void mouseDragged(MouseEvent e){
    x2=e.getX();
    y2=e.getY();
    repaint();
    }
    public void mouseEntered(MouseEvent e) {
        
        }
    	public void mouseMoved(MouseEvent e){
    	}
        
        public void mouseExited(MouseEvent e) {
            
        }
      public void mouseClicked(MouseEvent e) {
           
        }
    public void mouseReleased(MouseEvent e){
    x2=e.getX();
    y2=e.getX();
    Vector<Integer>myx2=getx2s();
    Vector<Integer>myy2=gety2s();
    Vector<Integer>myrectx2=getrectx2s();
    Vector<Integer>myrecty2=getrecty2s();
    int myshape2=getSetShape();
    if(myshape2==0){
    myx2.add(x2);
    myy2.add(y2);
    }
    if(myshape2==1){
    myrectx2.add(x2);
    myrecty2.add(y2);
    }
    
     }
    
    public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d=(Graphics2D)g;
    int getthisshape=getSetShape();
    if(getthisshape==0){
    g2d.drawLine(x1, y1, x2, y2);
    }
    if(getthisshape==1){
    int width=Math.abs(x2-x1);
    int height=Math.abs(y2-y1);
    g2d.drawRect(x1, y1, width, height);
    }
    Vector<Integer>drawx1s=getx1s();
    Vector<Integer>drawy1s=gety1s();
    Vector<Integer>drawx2s=getx2s();
    Vector<Integer>drawy2s=gety2s();
    for(int i=0;i<drawx1s.size() && i<drawy1s.size() && i<drawx2s.size() && i<drawy2s.size() ;i++){
    int thisx1=(Integer)drawx1s.get(i);
    int thisy1=(Integer)drawy1s.get(i);
    int thisx2=(Integer)drawx2s.get(i);
    int thisy2=(Integer)drawy2s.get(i);
    g2d.drawLine(thisx1, thisy1, thisx2, thisy2);
    }
    Vector<Integer>drawrectx1s=getrectx1s();
    Vector<Integer>drawrecty1s=getrecty1s();
    Vector<Integer>drawrectx2s=getrectx2s();
    Vector<Integer>drawrecty2s=getrecty2s();
    for(int i=0;i<drawrectx1s.size() && i<drawrecty1s.size() && i<drawrectx2s.size() && i<drawrecty2s.size() ;i++){
    int thisrectx1=(Integer)drawrectx1s.get(i);
    int thisrecty1=(Integer)drawrecty1s.get(i);
    int thisrectx2=(Integer)drawrectx2s.get(i);
    int thisrecty2=(Integer)drawrecty2s.get(i);
    int rectwidth=thisrectx2-thisrectx1;
    int rectheight=thisrecty2-thisrecty1;
    g2d.drawRect(thisrectx1, thisrecty1, rectwidth, rectheight);
    }
    
    }
    }
    Java Code:
    import javax.swing.JFrame;
    public class DrawFrame extends JFrame{
    public DrawFrame(){
    add(new drawmyimg());
    setTitle("Draw lines");
    setFocusable(true);
    setResizable(true);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
    }
    public static void main(String[] args){
    DrawFrame nn=new DrawFrame();
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Too long, could be arsed to read.

    What layout manager are you using?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,712
    Rep Power
    15

    Default

    Try adding some debugging code:

    Java Code:
    public void mouseReleased(MouseEvent e) {
    	x2 = e.getX();
    	y2 = e.getX();
    	Vector<Integer> myx2 = getx2s();
    	Vector<Integer> myy2 = gety2s();
    	Vector<Integer> myrectx2 = getrectx2s();
    	Vector<Integer> myrecty2 = getrecty2s();
    	int myshape2 = getSetShape();
    	if (myshape2 == 0) {
    		myx2.add(x2);
    		myy2.add(y2);
    	}
    	if (myshape2 == 1) {
    		[color=blue]System.out.println("Adding rectangle to " + x2 + "," + y2);[/color]
    		myrectx2.add(x2);
    		myrecty2.add(y2);
    	}
    }
    (I did the same where the rectangles where being drawn in an attempt to see why the end point of the rectangle "changed".)

    Run the program a few times and look at the end points (lower right) of the rectangles that are being drawn. Notice anything weird about their co-ordinates?
    Last edited by pbrockway2; 07-20-2011 at 03:58 AM.

Similar Threads

  1. Double drawing problem with 2D
    By leapinlizard in forum Java 2D
    Replies: 4
    Last Post: 02-12-2010, 02:02 AM
  2. Gui program problem..
    By mingming2009 in forum Advanced Java
    Replies: 7
    Last Post: 04-03-2009, 05:15 AM
  3. Problem with 3D drawing.
    By Supamagier in forum Advanced Java
    Replies: 0
    Last Post: 08-31-2008, 12:39 PM
  4. [SOLVED] Problem in Drawing(Repaint)
    By Preethi in forum New To Java
    Replies: 2
    Last Post: 07-16-2008, 07:28 AM
  5. program problem
    By amith in forum AWT / Swing
    Replies: 12
    Last Post: 05-16-2008, 08:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •