Results 1 to 7 of 7
  1. #1
    polmos is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Unwanted image fragment

    Hi everyone!
    I'm writing Microsoft's paint. The problem is that on bigger pictures (even on 800x600 pic) fragment of this pic appears unwanted in - for me - random places on bitmap. It is uneditable, you cannot draw on it. It covers old content. Additionally, it uses the same data source as my pic, so editing bitmap causes edit of this fragment (but not inversely). Don't know where did it come from and how to remove it. I'm attaching a screenshot which describes what i'm talking about. If someone is interested in helping me, please write something down and I'll give you necessary code.
    Unwanted image fragment-asd.jpg

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Unwanted image fragment

    1. What does your question have to do with Java?
    2. Recommended reading: How to ask questions the smart way

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    polmos is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Unwanted image fragment

    Well, I expect that someone will tell me how to fix my code, coz bug isn't placed anywhere else, is it? Shall I put down my code here or your not interested?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Unwanted image fragment

    Everywhere in your code where you do a Graphics.drawImage( ... ), add a System.out.println( ... ) statement that prints the dimension (and location) of the drawn image. You should be able to spot the funny part(s) in your code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    polmos is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Unwanted image fragment

    Every time app shows the same, correct dimensions and locations...

    Code that's realising the drawing. Where might have i made a mistake?

    Java Code:
    package painting;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.awt.image.WritableRaster;
    import java.util.LinkedList;
    import javax.swing.*;
    import painting.tool.*;
    class Wall extends JPanel implements MouseListener, MouseMotionListener,ActionListener{
        private JButton modeDraw,modeFill,modeMix,shapeOval,shapeRect,shapeRoundRect;
        private JButton increaseBrush,decreaseBrush;
        private JToolBar toolbar;
        private Tool activeTool;
        private PaintProperties properties;
        private int x1,x2,y1,y2;
        private boolean firstTime;
        private volatile BufferedImage bim;
        private WritableRaster raster;
        private Dimension size;  
        private LinkedList<WritableRaster> prevRaster,nextRaster;
        private final int BRUSH_LIMIT=100;
        private final int ERASER_LIMIT=40;
        private final int LINE_LIMIT=30;
        private final int UNDO_REDO_LIMIT=100;
        private Painting painting;
        Wall(Dimension size,JToolBar toolbar,Painting x) {
            super();
            painting=x;
            setBackground(Color.RED);
            setPreferredSize(size);
            setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
            addMouseListener(this);
            addMouseMotionListener(this);
            this.size=size;
            firstTime=true;
            properties=new PaintProperties(Painting.DEFAULT_FIRST_COLOR,Painting.DEFAULT_SECOND_COLOR, RenderingHints.VALUE_ANTIALIAS_ON);  
            //toolbar-----------
                this.toolbar=toolbar;
                this.toolbar.setLayout(new GridLayout(1, 3));
                this.toolbar.setBorderPainted(true);
            //------------------
            bim=new BufferedImage(size.width,size.height,BufferedImage.TYPE_INT_RGB);
            setDoubleBuffered(true);
            PenTool pen=new PenTool();
            activeTool=pen;
            setToolbarAbilities();
            x1=x2=y1=y2=-1000;
            prevRaster=new LinkedList<>();
            nextRaster=new LinkedList<>();
            
        }
        @Override
        public void paintComponent(Graphics g) {
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(0,0,this.getWidth(),this.getHeight());
            drawBim((Graphics2D)bim.getGraphics());
            g.drawImage(bim, 0, 0, null);
            g.dispose();
        }
       private void drawBim(Graphics2D g) {
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, properties.getAntialiasing());
            
            if(firstTime){
                g.setClip(0, 0, size.width, size.height);
                g.setColor(Color.WHITE);
                g.fillRect(0, 0, size.width, size.height);
                firstTime=false;
            }
            if((activeTool instanceof ShapeTool || activeTool instanceof LineTool)&& !firstTime){	
                g.setColor(Color.WHITE);
                g.fillRect(0, 0, this.getWidth(), this.getWidth());
                bim.setData(raster);
    	}else raster=(WritableRaster)bim.getData();   
            
            g.setColor(properties.getFirstColor());
            if(activeTool instanceof TraceTool) {
                TraceTool tool=(TraceTool) activeTool;
                if(tool.getCap()==BasicStroke.CAP_ROUND) g.setStroke(new BasicStroke(tool.getWeight(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                else g.setStroke(new BasicStroke(tool.getWeight(), BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
                if(activeTool instanceof EraserTool)  g.setColor(properties.getSecondColor());
                g.drawLine(x1, y1, x2, y2);
                if(!(activeTool instanceof LineTool)) {
                    x1=x2; y1=y2;
                }
            } 
            else if(activeTool instanceof ShapeTool) {
                ShapeTool shape=(ShapeTool) activeTool;
                g.setStroke(new BasicStroke(shape.getWeight(), BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
                Rectangle dragShape=new Rectangle(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
                int paintingMode=shape.getMode();
                switch(shape.getKind()) {
                    case ShapeTool.KIND_OVAL:
                        if(paintingMode==ShapeTool.MODE_FILL || paintingMode==ShapeTool.MODE_MIXED) g.fillOval(dragShape.x,dragShape.y,dragShape.width,dragShape.height);
                        if(paintingMode==ShapeTool.MODE_MIXED) g.setColor(properties.getSecondColor());
                        if(paintingMode==ShapeTool.MODE_DRAW || paintingMode==ShapeTool.MODE_MIXED) g.drawOval(dragShape.x,dragShape.y,dragShape.width,dragShape.height);
                    break;
                    case ShapeTool.KIND_RECT:
                        if(paintingMode==ShapeTool.MODE_FILL || paintingMode==ShapeTool.MODE_MIXED) g.fillRect(dragShape.x,dragShape.y,dragShape.width,dragShape.height);
                        if(paintingMode==ShapeTool.MODE_MIXED) g.setColor(properties.getSecondColor());
                        if(paintingMode==ShapeTool.MODE_DRAW || paintingMode==ShapeTool.MODE_MIXED) g.drawRect(dragShape.x,dragShape.y,dragShape.width,dragShape.height);
                    break;
                    case ShapeTool.KIND_ROUNDRECT:
                        if(paintingMode==ShapeTool.MODE_FILL || paintingMode==ShapeTool.MODE_MIXED) g.fillRoundRect(dragShape.x,dragShape.y,dragShape.width,dragShape.height,(int)(dragShape.width*ShapeTool.ROUNDRECT_ARCH_FACTOR),(int)(dragShape.height*ShapeTool.ROUNDRECT_ARCH_FACTOR));
                        if(paintingMode==ShapeTool.MODE_MIXED) g.setColor(properties.getSecondColor());
                        if(paintingMode==ShapeTool.MODE_DRAW || paintingMode==ShapeTool.MODE_MIXED) g.drawRoundRect(dragShape.x,dragShape.y,dragShape.width,dragShape.height,(int)(dragShape.width*ShapeTool.ROUNDRECT_ARCH_FACTOR),(int)(dragShape.height*ShapeTool.ROUNDRECT_ARCH_FACTOR));
                    break;
                }
            }
            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {  }
        }
        //--------------------------------------------------------------------
        public BufferedImage getImage() {   return bim;   }
        public Tool getActiveTool() {   return activeTool;   }
        public void setActiveTool(Tool tool) { 
            activeTool=tool; 
            x1=x2=-100;
            setToolbarAbilities();
            makeEraserCursor();
        }
        public void makeEraserCursor() {
            Cursor crs;
            if(activeTool instanceof EraserTool)
            { 
                 BufferedImage cursor = new BufferedImage(EraserTool.DEFAULT_ERASER_SIZE,EraserTool.DEFAULT_ERASER_SIZE,BufferedImage.TYPE_INT_RGB);
                 Graphics cursorGraph=cursor.getGraphics();
                 cursorGraph.setColor(Color.WHITE);
                 cursorGraph.fillRect(0, 0, EraserTool.DEFAULT_ERASER_SIZE-1, EraserTool.DEFAULT_ERASER_SIZE-1);
                 cursorGraph.setColor(Color.BLACK);
                 cursorGraph.drawRect(0, 0, EraserTool.DEFAULT_ERASER_SIZE-1, EraserTool.DEFAULT_ERASER_SIZE-1);
                 crs = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(EraserTool.DEFAULT_ERASER_SIZE/2,EraserTool.DEFAULT_ERASER_SIZE/2), "Gumka");	
            }
            else crs=new Cursor(Cursor.CROSSHAIR_CURSOR);
            setCursor(crs);
        }
        public PaintProperties getPaintProperties() {
            return properties;
        }
        public void clearScreen() {
            bim=new BufferedImage(size.width,size.height,BufferedImage.TYPE_INT_RGB);
            bim.getGraphics().setColor(Color.WHITE);
            bim.getGraphics().fillRect(0, 0, bim.getWidth(), bim.getHeight());
            bim.getGraphics().dispose();
            raster=(WritableRaster)bim.getData();
            repaint();
        }
        public boolean changeBrush(int i) {
            return changeBrush(i, 0);
        }
        public boolean changeBrush(int i, int limit) {
            Tool tool= activeTool;
            if((i<0 && tool.getWeight()>1)) {
                increaseBrush.setEnabled(true);
                tool.setWeight(tool.getWeight()-1);
                if(tool.getWeight()==1) decreaseBrush.setEnabled(false);
                return true;
            } 
            else if(i>0 && tool.getWeight()<limit) {
                decreaseBrush.setEnabled(true);
                tool.setWeight(tool.getWeight()+1);
                if(tool.getWeight()==limit) increaseBrush.setEnabled(false);
                return true;
            }
            return false;
        }
        private void addMenuItem(AbstractButton item,Container c, String tooltip) {
            item.addActionListener(this);
            c.add(item);
            item.setToolTipText(tooltip);
            item.setFocusPainted(false);
            item.setContentAreaFilled(true);
        }
        public void addBitmap(BufferedImage b,Component c) {
            bim=b;
            size=new Dimension(b.getWidth(), b.getHeight());
            setPreferredSize(size);
            raster=(WritableRaster)bim.getData();
            Graphics g =bim.getGraphics();
            g.setClip(0, 0, size.width, size.height);
            g.dispose();
            if(prevRaster.size()==UNDO_REDO_LIMIT) prevRaster.removeLast();
            prevRaster.addFirst((WritableRaster)bim.getData());
            refresh();
        }
        public void newBitmap(Dimension dim,Component c) {
            size=new Dimension(dim.width, dim.height);
            setPreferredSize(size);
            bim=new BufferedImage(size.width,size.height,BufferedImage.TYPE_INT_RGB);
            bim.getGraphics().setClip(0, 0, size.width, size.height);
            bim.getGraphics().dispose();
            //------------------------------------------------- clear lists
            if(prevRaster.size()==UNDO_REDO_LIMIT) prevRaster.removeLast();
            prevRaster.addFirst((WritableRaster)bim.getData());
            
            //-------------------------------------
            firstTime=true;
            refresh();
        }
        private void setToolbarAbilities() {      
            toolbar.removeAll();
            if((activeTool instanceof PenTool) || activeTool instanceof EraserTool || activeTool instanceof LineTool) {
                TraceTool tool=(TraceTool) activeTool;
                JPanel panel=new JPanel(new GridLayout(1,2));
                increaseBrush=new JButton("+ ["+(int)tool.getWeight()+"]");
                addMenuItem(increaseBrush,panel, "Większy rozmiar");
                decreaseBrush=new JButton("- ["+(int)tool.getWeight()+"]");
                if((activeTool instanceof LineTool || activeTool instanceof PenTool) && activeTool.getWeight()==1) decreaseBrush.setEnabled(false);
                addMenuItem(decreaseBrush,panel, "Mniejszy rozmiar");
                toolbar.add(panel);
            }
            else if(activeTool instanceof ShapeTool) {
                ShapeTool tool=(ShapeTool)activeTool;
                JPanel panel1=new JPanel(new GridLayout(1, 8));
                ImageIcon[] buff=painting.getBuffer();
                modeDraw=new JButton(buff[4]);
                addMenuItem(modeDraw,panel1,"Kontur");
                modeFill=new JButton(buff[5]);
                addMenuItem(modeFill,panel1,"Wypełnienie");
                modeMix=new JButton(buff[6]);
                addMenuItem(modeMix,panel1,"Mieszane");
                panel1.add(new JSeparator(SwingConstants.VERTICAL));
                shapeOval=new JButton(buff[7]);
                addMenuItem(shapeOval,panel1,"Elipsa");
                shapeRect=new JButton(buff[8]);
                addMenuItem(shapeRect,panel1,"Prostokąt");
                shapeRoundRect=new JButton(buff[9]);
                addMenuItem(shapeRoundRect,panel1,"Zaokrąglony prostokąt");
                increaseBrush=new JButton("+ ["+(int)tool.getWeight()+"]");
                addMenuItem(increaseBrush,panel1, "Grubsza krawędź");
                decreaseBrush=new JButton("- ["+(int)tool.getWeight()+"]");
                decreaseBrush.setEnabled(false);
                addMenuItem(decreaseBrush,panel1, "Cieńsza krawędź");
                toolbar.add(panel1);
            } 
            toolbar.revalidate();
            toolbar.repaint();
        }
        public void clearUndoRedoStatements() {
            prevRaster.clear();
            nextRaster.clear();
        }
        public void undo() {
            if(prevRaster.size()==0) return;
            if(nextRaster.size()==UNDO_REDO_LIMIT) nextRaster.removeLast();
            nextRaster.addFirst(raster);
            raster=prevRaster.removeFirst();
            bim.setData(raster);
            refresh();
            
           
        }
        public void redo() {
            if(nextRaster.size()==0) return;
            if(prevRaster.size()==UNDO_REDO_LIMIT) prevRaster.removeLast();
            prevRaster.addFirst(raster);
            raster=nextRaster.removeFirst();
            bim.setData(raster);
            refresh();
            
        }
        
        public void refresh() { painting.revalidate();
            painting.getContentPane().revalidate();
           
            revalidate();
            repaint();
            
        }
        
        /* --ACTION LISTENER------------------------------------------------------------------------ */
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==modeDraw) {
                ShapeTool tool=(ShapeTool) activeTool;
                tool.setMode(ShapeTool.MODE_DRAW);
            }
            else if(e.getSource()==modeFill) {
                ShapeTool tool=(ShapeTool) activeTool;
                tool.setMode(ShapeTool.MODE_FILL);
            }
            else if(e.getSource()==modeMix) {
                ShapeTool tool=(ShapeTool) activeTool;
                tool.setMode(ShapeTool.MODE_MIXED);
            }
            else if(e.getSource()==shapeOval) {
                ShapeTool tool=(ShapeTool) activeTool;
                tool.setKind(ShapeTool.KIND_OVAL);
            }
            else if(e.getSource()==shapeRect) {
                ShapeTool tool=(ShapeTool) activeTool;
                tool.setKind(ShapeTool.KIND_RECT);
            }
            else if(e.getSource()==shapeRoundRect) {
                ShapeTool tool=(ShapeTool) activeTool;
                tool.setKind(ShapeTool.KIND_ROUNDRECT);
            }
            else if(e.getSource()==increaseBrush) {
                Tool tool=activeTool;
                int limit;
                if(activeTool instanceof EraserTool) limit=ERASER_LIMIT;
                else if(activeTool instanceof LineTool || activeTool instanceof ShapeTool) limit=LINE_LIMIT;
                else limit=BRUSH_LIMIT;
                if(changeBrush(1,limit)) {
                    decreaseBrush.setEnabled(true);
                    increaseBrush.setText("+ ["+(int)tool.getWeight()+"]");
                    decreaseBrush.setText("- ["+(int)tool.getWeight()+"]");
                }
                else increaseBrush.setEnabled(false); 
            }
            else if(e.getSource()==decreaseBrush) {
                Tool tool=activeTool;
                if(changeBrush(-1)) {
                    increaseBrush.setEnabled(true);
                    increaseBrush.setText("+ ["+(int)tool.getWeight()+"]");
                    decreaseBrush.setText("- ["+(int)tool.getWeight()+"]");
                }
                else decreaseBrush.setEnabled(false);
            }
            makeEraserCursor();
        }
        //---------MOUSE LISTENER ---------------------------------------------------------------
        @Override
        public void mouseDragged(MouseEvent e) {
            x2=e.getX();
            y2=e.getY();
            if(activeTool instanceof ShapeTool) {
              if(e.isShiftDown()) {
                    int absX=Math.abs(x1-x2);
                    int absY=Math.abs(y1-y2);
                    int max=Math.max(absX, absY);
                    if(x1>x2) {
                        if(y1>y2) {
                            x2=x1-max;
                            y2=y1-max;  
                        }
                        else {
                            x2=x1-max;
                            y2=y1+max;
                        }
                    }
                    else {
                        if(y1>y2) {
                            x2=x1+max;
                            y2=y1-max; 
                        }
                        else {
                            x2=x1+max;
                            y2=y1+max;
                        }
                    }
                }
            }
            synchronized(this) {
                revalidate();
                repaint();
            }
            e.consume();
        }
        @Override
        public void mousePressed(MouseEvent e) {
            x1=x2=e.getX();
            y1=y2=e.getY();       
            
            synchronized(this) {
                revalidate();
                repaint();
            }
            
            if(prevRaster.size()==UNDO_REDO_LIMIT) prevRaster.removeLast();
            prevRaster.addFirst((WritableRaster)bim.getData());
            e.consume();
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            x1=x2=-100;
            y1=y2=-100;
            raster=(WritableRaster)bim.getData();
            e.consume();
        }
        @Override
        public void mouseEntered(MouseEvent e) {}
        @Override
        public void mouseExited(MouseEvent e) {}
        @Override
        public void mouseMoved(MouseEvent e) { 
            synchronized(this) {
                revalidate();
                repaint(); 
            }
            
        }
        @Override
        public void mouseClicked(MouseEvent e) {  }
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Unwanted image fragment

    That code shows no debug logging at all...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Unwanted image fragment

    Quote Originally Posted by polmos View Post
    Well, I expect that someone will tell me how to fix my code, coz bug isn't placed anywhere else, is it? Shall I put down my code here or your not interested?
    That's a bit rude. First of all you didn't post any code from the start, so how can one just "fix your code"?

    More on topic, have you tried

    Java Code:
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(0,0,this.getWidth(),this.getHeight());
            drawBim((Graphics2D)bim.getGraphics());
            g.drawImage(bim, 0, 0, null);
            g.dispose();
        }
    /Z!

Similar Threads

  1. Replies: 8
    Last Post: 03-01-2012, 10:06 AM
  2. unwanted return to paint
    By sir.logic209 in forum New To Java
    Replies: 6
    Last Post: 12-16-2010, 03:45 AM
  3. Unwanted Java update
    By Hummingbird in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 10:18 AM
  4. unwanted bug. JMenu
    By ocean in forum New To Java
    Replies: 7
    Last Post: 11-20-2009, 09:29 PM
  5. Repositioning An unwanted JInternalFrame
    By marco.c84 in forum AWT / Swing
    Replies: 8
    Last Post: 03-18-2009, 09:42 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
  •