java:如何使用内部类实现手绘绘图

发布于 2024-11-03 05:17:36 字数 5659 浏览 1 评论 0原文

您好,我正在尝试一个绘画应用程序,并试图弄清楚内部类如何访问主要的graphic2D函数来实现徒手​​绘制选择?还是我找错了树?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-11-10 05:17:36

我使用了两种解决方案来解决这个问题:

  • 一种解决方案是让外部类将 BufferedImage 对象作为类字段保存并在其 PaintComponent 方法中显示它,然后内部类可以从 BufferedImage 中提取 Graphics 或 Graphcis2D 对象,绘制它,释放 Graphics 对象,并在外部类上调用重绘。
  • 另一种选择是使用从内部类中填充的 Point(或其他 Shape)对象的类 ArrayLists 在外部类的 PaintComponent 方法中进行绘制。在后一种情况下,内部类与 Graphics 对象无关,而是填充外部类的数组列表并调用重绘。

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.
ま柒月 2024-11-10 05:17:36

您可以将:

private Graphics2D g2d;

作为面板的类成员,并且在第一次调用 PaintComponent 后,它将拥有可由内部类访问的主图形对象。

You can put:

private Graphics2D g2d;

As a class member of the panel and after the first time paintComponent is called it will have the main graphics object accessible to inner classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文