Java画家程序绘图形状

时间:2013-05-07 19:16:48

标签: java swing jpanel paintcomponent shapes

我在这个java代码中遇到了问题。 我想做一个画家程序,但每当我选择一个形状并绘制它 之前绘制的所有形状都与此形状相同。这是代码。 我知道问题来自paintComponent中的for语句,但是我可以用什么替换它?

class inner extends JPanel implements MouseListener,MouseMotionListener{
        private int oldx,oldy,newx,newy;
        private Point point1,point2;
        private Shape newRec,line1;
        Rectangle currRec;
        private Vector shape;
        private boolean status,status1;
        private int count=0;
        private Object line;
        inner(){
            point1=point2=new Point(0,0);
            shape = new Vector();
            addMouseListener(this);
            addMouseMotionListener(this);
        }

        public void mouseDragged(MouseEvent event) {

            point2=event.getPoint();
            newx = point2.x;
            newy = point2.y;
            if(Universal==7){
            line = new Object(oldx,oldy,newx,newy);
            status = true;
            repaint();
            }


            currRec = new Rectangle(Math.min(point1.x,point2.x),Math.min(point1.y,point2.y),Math.abs(point1.x-point2.x),Math.abs(point1.y-point2.y));   
            status = true;
            repaint();

        }

        @Override
        public void mouseMoved(MouseEvent arg0) {}
        public void mouseClicked(MouseEvent arg0) {}                
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}


        public void mousePressed(MouseEvent event) {

            point1=event.getPoint();
            oldx=event.getX();
            oldy=event.getY();
        }

        @Override
        public void mouseReleased(MouseEvent event) {

            point2=event.getPoint();
            newx=event.getX();
            newy=event.getY();
            //count++;


        if(Universal==7){
                line1 = new Shape(point1.x,point1.y,point2.x,point2.y);
                shape.add(line1);
                //Graphics g = getGraphics();
                //g.setColor(colour);
                //g.drawLine(point1.x,point1.y,point2.x,point2.y);

                count++;
                repaint();
            }
        else if(Universal==1||Universal==2||Universal==3||Universal==4||Universal==5||Universal==6){
                newRec = new Shape(Math.min(point1.x,point2.x),Math.min(point1.y,point2.y),Math.abs(point1.x-point2.x),Math.abs(point1.y-point2.y));
                shape.add(newRec);
                count++;
                repaint();  
            }
        }

    ///// the problem is in here    
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Shape c;
            g.setColor(colour);
            for(int i=0;i<shape.size();i++){
                c = (Shape) shape.get(i);

            if(Universal==1){

                g.drawRect(c.x, c.y, c.w, c.h);
                if(status){
                    g.drawRect(currRec.x, currRec.y, currRec.width, currRec.height);
                }
            }

            if(Universal==2){
                g.fillRect(c.x, c.y, c.w, c.h);

                if(status){
                    g.fillRect(currRec.x, currRec.y, currRec.width, currRec.height);

                }
            }

            if(Universal==3){
                g.drawRoundRect(c.x, c.y, c.w, c.h, c.w/4, c.h/4);

                if(status){
                    g.drawRoundRect(currRec.x, currRec.y, currRec.width, currRec.height,currRec.width/4,currRec.height/4);

                }
            }

            if(Universal==4){
                g.fillRoundRect(c.x, c.y, c.w, c.h, c.w/4, c.h/4);

                if(status){
                    g.fillRoundRect(currRec.x, currRec.y, currRec.width, currRec.height,currRec.width/4,currRec.height/4);

                }
            }

                if(Universal==5){

                    g.drawOval(c.x, c.y, c.w, c.h);

                    if(status){
                        g.drawOval(currRec.x, currRec.y, currRec.width, currRec.height);
                    }
                }

                if(Universal==6){

                    g.fillOval(c.x, c.y, c.w, c.h);

                    if(status){
                        g.fillOval(currRec.x, currRec.y, currRec.width, currRec.height);
                    }
                }

                if(Universal==7){

                    g.drawLine(c.x, c.y, c.w, c.h);

                    if(status){
                        g.drawLine(line1.x, line1.y, line1.w,line1.h);
                    }
                }

                if(Universal==8){
                    shape.clear();
                }
            }
        }

2 个答案:

答案 0 :(得分:1)

Universal在任何特定时间都只是一个给定的值。

油漆不是累积的,它们具有破坏性。

也就是说,每次调用paintComponent时,所有先前的内容都会被删除/擦除,并且您需要“重新绘制”内容。

您应该将Shape添加到某种List,而不是依赖于单个标记,并在调用paintComponent时重新绘制它们。同样,您只需将“类型”(int)添加到List并在每次重绘时处理该列表

请查看Painting in AWT in Swing以获取有关绘画过程的说明

答案 1 :(得分:1)

有两种不同的方法,请参阅Custom Painting Approaches

  1. 向List添加形状,然后每次调用paintComponent()时重新绘制列表中的所有Shapes。

  2. 将形状绘制到BufferedImage,然后在调用paintComponent()时重新绘制图像。

  3. 这两个例子都没有完全符合您的要求,它只向您展示方法。