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

  • Home
  • SEARCH
  • 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 4549062
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:18:25+00:00 2026-05-21T16:18:25+00:00

Hi I am attempting a paint application and trying to figure how an inner

  • 0

Hi I am attempting a paint application and trying to figure how an inner class can get access to the main graphic2D function to implement a freehand draw choice? or am I barking up the wrong tree?

 import javax.swing.*; // For JPanel, etc.
   import java.awt.*; // For Graphics, etc.
   import java.awt.geom.*; // For Ellipse2D, etc.
   import java.awt.event.*;
   import java.util.ArrayList;
   import java.awt.Shape;
   import java.awt.Graphics2D;
   import java.lang.Math;
   import javax.swing.event.ChangeListener; 
   import javax.swing.event.ChangeEvent; 

   public class DrawingPanel extends JPanel 
   {
      private double x1=0;
      private double x2=0;
      private double y1=0;
      private double y2=0;
        private double x3=0;
      private double x4=0;
      private double y3=0;
      private double y4=0;
      private double tx=0;
      private double ty=0;
      private double tz=0;
      double width = Math.abs(x1 -x2);
      double height = Math.abs(y1-y2);

      private Point start, end;
      private ArrayList<Shape> myArr = new ArrayList<Shape>();
      private ArrayList<Shape> myArr2 = new ArrayList<Shape>();
      ButtonPanel buttonPress;

      protected void paintComponent(Graphics g) { 
         super.paintComponent(g);//  let panel draw itself 
         Graphics2D g2d = (Graphics2D)g; 
         g2d.setPaint(Color.blue);
         g2d.setStroke(new BasicStroke(4));
         for (Shape i : myArr) 
         { 
            g2d.draw(i); 
         } 
         for(int j = 0;j<myArr2.size();j++)
         {
            //g2d.setColor(shapeTransColor.get(i));// get the colour from the colour array
            g2d.fill(myArr2.get(j));// fill the shape from the shape array                                  
         }

      }     
        //inner class

      class Listener1 extends MouseAdapter
      {
                     public void mousePressed(MouseEvent e)
         {

            x1=e.getX();
            y1=e.getY();
            System.out.println("Mouse Pressed");
                if (buttonPress.buttonType.equals("Clear"))
            {                        
               System.out.println("ArrayList Size :"+myArr.size());
               System.out.println("ArrayList2 Size :"+myArr2.size());                   
               myArr.clear();
               myArr2.clear(); // clears all elements from arraylists 
               System.out.println("ArrayList Size :"+myArr.size()); 
               System.out.println("ArrayList2 Size :"+myArr2.size());
               repaint();    
            } 
         }

         public void mouseReleased(MouseEvent e)
         {
            x2=e.getX();
            y2=e.getY();
            Shape shape = null;
            if (buttonPress.buttonType.equals("Rectangle"))
            {
            // Rectangles cannot have a zero width or height
               if (x1 != x2 || y1 != y2)
               {
                  double width = Math.abs(x1 - x2);
                  double height = Math.abs(y1 - y2);
                  Rectangle2D.Double rect = new Rectangle2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height);
                  myArr.add(rect);
                  repaint();
               }
            } 
            if (buttonPress.buttonType.equals("Eclipse"))
            {
               double width = Math.abs(x1 - x2);
               double height = Math.abs(y1 - y2);
               Ellipse2D.Double elli = new Ellipse2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height);
               myArr.add(elli);
               repaint();
            } 
            if (buttonPress.buttonType.equals("Lines"))
            {
               Line2D.Double nuLine = new Line2D.Double(x1, y1, x2, y2);    
               myArr.add(nuLine);   
               repaint();        
            } 
            if (buttonPress.buttonType.equals("Triangle"))
            {/*

                *
                *
                *
                repaint();    */    
            }
            if (buttonPress.buttonType.equals("FillRectangle"))
            {               
               if (x1 != x2 || y1 != y2)
               {
                  double width = Math.abs(x1 - x2);
                  double height = Math.abs(y1 - y2);
                  Rectangle2D.Double fillRect = new      Rectangle2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height);
                  myArr2.add(fillRect); 
                  repaint();
               }
            }
            if (buttonPress.buttonType.equals("FillEclipse"))
            {               
               double width = Math.abs(x1 - x2);
               double height = Math.abs(y1 - y2);
               Ellipse2D.Double fillElli = new Ellipse2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height);
               myArr2.add(fillElli); 
               repaint();
            }    
        if (buttonPress.buttonType.equals("Freehand"))
            {    
                    System.out.println("test free");

               //*
                    //*
                    //*               
                    repaint();  
               //myArr2.add(nuLine2);                                                            
            }             

            if (shape != null)
            {
               myArr.add(shape);    
               myArr2.add(shape);
            }
            repaint();
         }                          
      }
    //end of inner class   
      public DrawingPanel(ButtonPanel reference)
      {
         buttonPress = reference;
         setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,2));
         addMouseListener(new Listener1());  
         repaint();      
      }             
   }
  • 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-05-21T16:18:26+00:00Added an answer on May 21, 2026 at 4:18 pm

    I’ve used two solutions to this problem:

    • One solution is to have the outer class hold as a class field a BufferedImage object and display it in its paintComponent method, and then the inner class can extract a Graphics or Graphcis2D object from the BufferedImage, draw to it, dispose the Graphics object, and call repaint on the outer class.
    • Another option is to draw in the outer class’s paintComponent method using class ArrayLists of Point (or other Shape) objects that are filled from within the inner class. In the latter situation the inner class has nothing to do with a Graphics object but rather fills the outer’s array lists and calls repaint.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our main application has both a asp.net and winforms component. There is a class
In my application I'm trying to create a function to print existing PDFs or
I am attempting to implement a collapsible table view in an iOS application. To
I am attempting to implement global error handling in my MVC application. I have
I am attempting to print something using a C# Winforms application. I can't seem
Attempting to get Spring internationalization working. I have used classpath:messages basename, created .properties files
Attempting to set up an automated texting service for customers, where people can text
Attempting to implement a poor man's test of whether a process is still running
I am attempting to visualize / understand my Rails application hierarchy on each page
Attempting to print the contents of a <rich:modalPanel /> in my application results in

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.