Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:24:50+00:00 2026-06-19T03:24:50+00:00

Language: Java. Hi, I need to prevent drawing over the same location of a

  • 0

Language: Java.

Hi, I need to prevent drawing over the same location of a Graphics2D more than once. For example, if the user draws using a BasicStroke with a round cap and a width of 10 over a certain area, that same area cannot be drawn on a second time.

The reason I want to do this is so that the user can draw (free-hand) translucent colours over an image without drawing over the same stroke (thus increasing the density of the colour and reducing its translucency).

I’ve tried storing the shapes of all the strokes made by the user (as Area objects that subtract the shape) and then clipping the Graphics2D by the intersection of all those Area objects.

This almost works, but the ‘shape’ obtained by the clip is not quite the same as the ‘shape’ drawn by the stroke – it is out by a couple of pixels.

Does anyone have any other ideas that might work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:24:51+00:00Added an answer on June 19, 2026 at 3:24 am

    The concept is relatively simple, you need to have multiple layers onto which you can render…

    There are multiple different ways to approach the problem. You could maintain a list of Points and on each paint cycle, render these points to a backing buffer, which you would then draw over the main content using a AlphaComposite.

    You could (as this example does) draw directly to the backing buffer and repaint the content, again, using a AlphaComposite to render the higher layer.

    You could have any number of layers…

    enter image description here

    import java.awt.AlphaComposite;
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Line2D;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    
    public class PaintOver {
    
        public static void main(String[] args) {
            new PaintOver();
        }
    
        public PaintOver() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new MapPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                }
            });
        }
    
        public class MapPane extends JPanel {
    
            private BufferedImage background;
            private BufferedImage foreground;
    
            public MapPane() {
                try {
                    background = ImageIO.read(getClass().getResource("/TreasureMap.png"));
                    foreground = new BufferedImage(background.getWidth(), background.getHeight(), BufferedImage.TYPE_INT_ARGB);
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                MouseAdapter mouseHandler = new MouseAdapter() {
                    private Point startPoint;
    
                    @Override
                    public void mousePressed(MouseEvent e) {
                        startPoint = e.getPoint();
                    }
    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        startPoint = null;
                    }
    
                    @Override
                    public void mouseDragged(MouseEvent e) {
                        Point endPoint = e.getPoint();
                        Graphics2D g2d = foreground.createGraphics();
    
                        Point offset = getOffset();
    
                        Point from = new Point(startPoint);
                        from.translate(-offset.x, -offset.y);
                        Point to = new Point(endPoint);
                        to.translate(-offset.x, -offset.y);
                        g2d.setColor(Color.RED);
                        g2d.setStroke(new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                        g2d.draw(new Line2D.Float(from, to));
                        g2d.dispose();
                        startPoint = endPoint;
                        repaint();
                    }
                };
    
                addMouseListener(mouseHandler);
                addMouseMotionListener(mouseHandler);
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
            }
    
            protected Point getOffset() {
                Point p = new Point();
                if (background != null) {
                    p.x = (getWidth() - background.getWidth()) / 2;
                    p.y = (getHeight() - background.getHeight()) / 2;
                }
                return p;
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (background != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    Point offset = getOffset();
    
                    g2d.drawImage(background, offset.x, offset.y, this);
                    g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
                    g2d.drawImage(foreground, offset.x, offset.y, this);
                    g2d.dispose();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Scala/Java dual language project where I need to pass a Scala
i need an example in Developer defined package in java i cant find any
I need to write a parser for Java programming language. I've seen some implementations
Other than the Java language itself, you have to learn the java framework. Similiar
Programming language: Java Ok, so I want to have a BufferedImage that keeps rotating
i am a beginner at java language and i use text pad. i have
i am still new to the Java language and libraries... i often use this
Based on my understanding of the Java language, static variables can be initialized in
I was searching a bit for another platform independent language like Java. Are there
As you maybe know, as Java language perspective all method in C# are final

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.