【问题标题】:Add graphics on a Jpanel Object with Thread使用 Thread 在 Jpanel 对象上添加图形
【发布时间】:2016-11-29 16:52:32
【问题描述】:

我想在这个程序中添加图形。图形将绘制在“drawPanel”对象上。我必须在这里使用线程。

不知道用线程绘制 Jpanel 对象。在该 Jpanel 对象上绘制图形的高效方法是什么?

线程和paintComponent() 将如何交互。谢谢。

代码:

public class LinearSearch extends JPanel{

    private final Font LABEL_FONT = new Font("courier", Font.BOLD, 30);

    private JPanel mainPanel;
    private JPanel centerPanel;
    private JPanel inputPanel;
    private JPanel drawPanel;

    private JLabel lblTitle;
    private Button btnBack;

    public LinearSearch(JPanel mainPanel) {
        this.mainPanel = mainPanel;

        setBackground(Color.WHITE);

        initialize_All();
    }

    private void initialize_All() {
        lblTitle = new JLabel("\"Linear Search\"", SwingConstants.CENTER);
        lblTitle.setFont(LABEL_FONT);

        ///Center Panel
        centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout());

        ///Input Panel
        inputPanel = new JPanel();
        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));


        ///Draw Panel
        drawPanel = new JPanel();
        drawPanel.setLayout(new FlowLayout());

        /// I want to add graphics on this drawPanel Jpanel

        btnBack = new Button("Back");
        btnBack.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                setVisible(false);
                removeAll();

                mainPanel.add(new CatagoriesMenu(mainPanel));
            }
        });


        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        setLayout(new BorderLayout(10, 10));

        add(lblTitle, BorderLayout.PAGE_START);
        add(centerPanel, BorderLayout.CENTER);
        add(btnBack, BorderLayout.PAGE_END);

        centerPanel.add(inputPanel, BorderLayout.PAGE_START);
        centerPanel.add(drawPanel, BorderLayout.CENTER);

    }
}

【问题讨论】:

  • 应用程序在什么时候以及如何执行。做一个“线性搜索”?我希望动画进度的最合适方法是绘制进度条的功能等效项。例如。一个有边框的矩形,横跨大部分可视区域,填充了背景颜色。然后在该边界内填充一个较小(较窄)的矩形,表示该时刻的进度。因为搜索了 150 条记录中的 50 条,它会填满三分之一。 100人中有50人,一半。至于使用线程来做到这一点,这在很大程度上取决于您如何使用线程进行搜索..
  • 如需尽快获得更好的帮助,请发帖minimal reproducible exampleShort, Self Contained, Correct Example
  • 保持您的问题无噪音,因此不要添加诸如“已添加答案”之类的内容。

标签: java swing jpanel java-threads


【解决方案1】:

我必须在“drawPanel”Jpanel 对象上添加图形。

然后您需要扩展JPanel 并覆盖paintComponent(...) 方法来进行自定义绘画。

阅读 Custom Painting 上的 Swing 教程部分,了解更多信息和工作示例,以帮助您入门。

【讨论】:

    【解决方案2】:

    只需要在 add() 方法中从另一个类中添加这个类对象。

    这将工作(测试):

    public class LinearSearchSimulation extends JPanel implements Runnable{
    
        private final int DELAY = 50;
        private JPanel mainPanel;
    
        int x=0, y=0;
    
        private Thread thread;
        private boolean threadFlag = false;
    
            public LinearSearchSimulation(JPanel mainPanel){
                this.mainPanel = mainPanel;
                setBackground(Color.WHITE);
    
            }
    
            public void start() {
                if (threadFlag) {
                    return;
                }
    
                threadFlag = true;
                thread = new Thread(this);
                thread.start();
            }
    
            public void stop() {
                if (!threadFlag) {
                    return;
                }
    
                threadFlag = false;
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.fillOval(x,y, 55, 55);
                g.dispose();
            }
    
            public void update() {
    
                x += 1;
                y += 1;
            }
    
            @Override
            public void run() {
                long beforeTime, timeDiff, sleep;
    
                beforeTime = System.currentTimeMillis();
    
                while (threadFlag) {
    
                    update();
                    repaint();
    
                    timeDiff = System.currentTimeMillis() - beforeTime;
                    sleep = DELAY - timeDiff;
    
                    if (sleep < 0)
                        sleep = 2;
                    try {
                        Thread.sleep(sleep);
                    } catch (InterruptedException e) {
                        System.out.println("interrupted");
                    }
    
                    beforeTime = System.currentTimeMillis();
                }
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多