gpt4 book ai didi

java - paintComponent 在我的绘图之上绘制其他组件

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:36:00 25 4
gpt4 key购买 nike

我正在尝试构建一个简单的绘画工具。 mouseDrag 事件创建了一个新的椭圆并使我的 JPanel 变为 repaint()

到目前为止一切正常。但是,如果我在第一次触发 mouseDrag 事件之前按下任何按钮(或任何其他 UI 组件),该按钮将绘制在面板的左上角。

我已将代码隔离到此测试应用程序中:

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame
{
public Test()
{
final JPanel paintPanel = new JPanel(){
@Override
protected void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setPaintMode();

g2d.setStroke(new BasicStroke(1));
g2d.fillRect(100, 100, 10, 10);
}
};

paintPanel.setPreferredSize(new Dimension(300,300));
paintPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
paintPanel.repaint();
}
});

this.setLayout(new FlowLayout());

this.add(paintPanel);
this.add(new JButton("Dummy"));

this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public static void main(String... args)
{
new Test();
}
}

TestApp before clicking on the Panel TestApp after clicking on the Panel

A Screenshot for "seeing" the problem in my Main application

最佳答案

+1 @MadProgrammer 的回答。

  • 您应该将 super.paintComponent(..) 作为覆盖 paintComponent() 的第一个调用
  • 不要不必要地扩展JFrame
  • 通过 EDT 创建和最小化 Swing 组件
  • 不要调用 setPrefferedSize() 而是覆盖 getPrefferedSize()

这是一个结合了我的建议和@MadProgrammer 的建议的示例:

enter image description here

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

JFrame frame;

public Test() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final PaintPanel paintPanel = new PaintPanel();

paintPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
paintPanel.addRect(e.getX(), e.getY());
}
});

frame.setLayout(new FlowLayout());

frame.add(paintPanel);
frame.add(new JButton("Dummy"));

frame.pack();
frame.setVisible(true);
}

public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test();
}
});
}
}

class PaintPanel extends JPanel {

public PaintPanel() {
addRect(100, 100);
}
ArrayList<Rectangle> rects = new ArrayList<>();

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaintMode();

for (Rectangle r : rects) {
g2d.setStroke(new BasicStroke(1));
g2d.fillRect(r.x, r.y, r.width, r.height);
}
}

public void addRect(int x, int y) {
rects.add(new Rectangle(x, y, 10, 10));
repaint();
}

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

关于java - paintComponent 在我的绘图之上绘制其他组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13358658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com