使用 Thread 在 Jpanel 对象上添加图形
Add graphics on a Jpanel Object with Thread
我想在这个程序中添加图形。图形将绘制在 "drawPanel" 对象上。我这里必须要用thread。
不知道用线程在 Jpanel 对象中绘图。在该 Jpanel 对象上绘制图形的有效方法是什么?
如何线程和 paintComponent()
去 interact.Thanks。
Code:
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);
}
}
I must need to add graphics on "drawPanel" Jpanel Object.
然后您需要扩展 JPanel
并重写 paintComponent(...)
方法来进行自定义绘制。
阅读 Custom Painting 上的 Swing 教程部分,了解更多信息和工作示例以帮助您入门。
只需要在 add() 方法中从另一个 class 添加这个 class 对象。
This will work (tested):
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();
}
}
}
我想在这个程序中添加图形。图形将绘制在 "drawPanel" 对象上。我这里必须要用thread。
不知道用线程在 Jpanel 对象中绘图。在该 Jpanel 对象上绘制图形的有效方法是什么?
如何线程和 paintComponent()
去 interact.Thanks。
Code:
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);
}
}
I must need to add graphics on "drawPanel" Jpanel Object.
然后您需要扩展 JPanel
并重写 paintComponent(...)
方法来进行自定义绘制。
阅读 Custom Painting 上的 Swing 教程部分,了解更多信息和工作示例以帮助您入门。
只需要在 add() 方法中从另一个 class 添加这个 class 对象。
This will work (tested):
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();
}
}
}