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