在 JPanel 的一侧绘制

Draw on one side of a JPanel

我想编写一个应用程序,让您可以通过在 JFrame 的左侧单击鼠标来绘制圆圈,并且所有的点都 "mirrored" 到右侧。我遇到的第一个问题是,当我尝试在我的框架中实现这个绘图机制时,没有出现圆圈。

public class Application{
  int x,y;
  private JPanel container;

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new Application().gui();
      }
    });  
  }

  public void gui()
  {
    int height = 250;
    int width = 700;
    JFrame jframe = new JFrame();
    container = new JPanel();
    container.setLayout(new BorderLayout());

    container.add(new DrawCircle(), BorderLayout.WEST);
    container.setVisible(true);
    jframe.add(container);
    //jframe.add(new DrawCircle());

    jframe.setSize(500,700);
    jframe.setVisible(true);
    jframe.setTitle("Title");
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setResizable(false);

  } 
}

当我使用 container.add(new DrawCircle) 时它(在整个框架上)有效,但如果我想添加约束,它就不会。

这是圆 class:

public class DrawCircle extends JPanel implements MouseListener
{
  ArrayList<Point> p = new ArrayList<Point>();

  public DrawCircle()
  {
    addMouseListener(this);
  }

  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    for(Point point : p)
    {
      g.fillOval(point.x,point.y,30,30);    
    }  
  }

  @Override
  public void mouseClicked(MouseEvent e) {
  }

  @Override
  public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub
  }

  @Override
  public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
  }

  @Override
  public void mousePressed(MouseEvent e) {
    p.add(new Point(e.getY(), e.getX()));
  }

  @Override
  public void mouseReleased(MouseEvent e) {
  }

  @Override
  public void mouseDragged(MouseEvent e) {
  }

  @Override
  public void mouseMoved(MouseEvent arg0) {
  }
}

让我们来解决您的问题:

The first problem I encountered was that when I try to implement this draw-mechanic in my frame, no circles appear.

这是因为您在单击 DrawCircle class 中的某处时忘记调用 JPanel#revalidate() and JPanel#repaint()

因此,您可以将 mousePressed() 方法更改为:

@Override
public void mousePressed(MouseEvent e) {
    p.add(new Point(e.getX(), e.getY()));
    revalidate();
    repaint();
}

请注意,我还更改了 e.getX()e.getY() 调用,因为它们位于错误的位置(除非您希望那样)。

这会让你的圆圈出现,但是,你的 DrawCircle 真的很瘦(我把你的 JFrame 的高度改成了 200 为这张照片,否则它会很高):

红色部分是您的 DrawCircle 面板。

要解决此问题,您需要覆盖其 getPreferredSize() 方法:

@Override
public Dimension getPreferredSize() {
    return new Dimension(width, height);
}

这将使你的 JPanel 到 return 大小的一半,widthheight 作为参数传递给构造函数,而你的 class DrawCircle 现在应该是这样的:

class DrawCircle extends JPanel implements MouseListener {
    ArrayList<Point> p = new ArrayList<Point>();
    
    int width = 0;
    int height = 0;
    
    public DrawCircle(int width, int height) {
        this.width = width;
        this.height = height;
        addMouseListener(this);
    }
    
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Point point : p) {
            g.fillOval(point.x, point.y, 30, 30);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mousePressed(MouseEvent e) {
        p.add(new Point(e.getX(), e.getY()));
        revalidate();
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }
}

输出会是这样的:

It works (on the whole frame) when I use container.add(new DrawCircle)

这是因为默认情况下 BorderLayout 将元素默认放置在 CENTER 区域,如果您在其余方向上没有其他内容(NORTHSOUTH, 等) 它将占用整个 space.

现在让我们继续解决您的问题:

我还对 Application class 进行了一些更改(在我的例子中,我将其重命名为 CustomPaintingInHalfFrame):

这些变化是:

  • WIDTHHEIGHT 属性创建最终常量。
  • 删除了不必要的JPanelBorderLayout布局,因为JFrame默认已经有这个布局,我简单地添加了我们的DrawClass
  • 绘制 DrawCircle 面板的边框(因为您不希望在 JFrame 的两个(左和右)部分之间进行划分,如 中所述你可以简单地删除它(我建议你在测试时把它留在那里,这样你就知道左面板在哪里结束,右面板从哪里开始。
  • 传递 WIDTH / 2HEIGHT 作为 DrawCircle 构造函数的参数,因此它可以 return 正确的 Dimension.

所以,我们的 class 现在应该是这样的:

public class CustomPaintingInHalfFrame {
    int x, y;
    public static final int WIDTH = 500;
    public static final int HEIGHT = 200;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CustomPaintingInHalfFrame().gui();
            }
        });
    }

    @SuppressWarnings("serial")
    public void gui() {
        JFrame jframe = new JFrame("Title");
        
        DrawCircle dc = new DrawCircle(WIDTH / 2, HEIGHT);
        
        dc.setBorder(BorderFactory.createLineBorder(Color.RED));
        
        jframe.add(dc, BorderLayout.WEST);
        
        jframe.setSize(WIDTH, HEIGHT);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setResizable(false);

    }
}

其他提示

  1. 我建议您将 DrawCircle 重命名为 Circle 或类似名称。作为convention名字应该是nouns

  2. 例如,
  3. gui() 方法重命名为 createGui(),因为作为 类 名称, 方法 名称应该是 动词