gpt4 book ai didi

java - 无法看到添加到 JPanel 的图形

转载 作者:行者123 更新时间:2023-12-02 03:02:34 24 4
gpt4 key购买 nike

我打算用二手实现一个时钟,但我无法正确显示秒针。

我的代码如下所示,ClockFrame 新创建了 SecondHand 并将面板传递给 SecondHand。 SecondHand 预计会获得秒数并自行更新。我认为 g2.drawImage(bImage, 0, 0, null);显示我的位图,但根本不显示。我真的很困惑。提前致谢。

时钟.java

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Clock {
public static void main(String[] a) {
ClockFrame c=new ClockFrame();
}
}

二手.java

import java.awt.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.Calendar;
import java.io.*;
import java.awt.image.*;

public class SecondHand extends JComponent implements Runnable{

private JPanel fpanel;
private BufferedImage bImage;
private Thread secThread;
private Point center;
double radPerSec = Math.PI/648000;
public SecondHand(String path,Point p,Object o){
try {
bImage = ImageIO.read(new File(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//setLocation((int)p.getX(),(int)p.getY());
setLocation(0,0);
center=p;
((JPanel)o).add(this);
fpanel=(JPanel)o;

secThread=new Thread(this);
secThread.start();

}

public void run() {
while(true) {
try{
Thread.sleep(1000);
Graphics2D g2 = (Graphics2D) getGraphics();
g2.drawImage(bImage, 0, 0, null);
//fpanel.repaint();

} catch (InterruptedException e) {

e.printStackTrace();
}
}

}


}

时钟框架.java

import java.awt.*;
import javax.swing.*;

public class ClockFrame extends JFrame {

public ClockFrame(){
JPanel panel = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
setSize(1000, 1000);
panel.setSize(getWidth(), getHeight());
panel.setLayout(null);//!important
setVisible(true);
setResizable(false);
panel.setBackground(Color.BLACK);
SecondHand sHand=new SecondHand("./res/icon/sechand.png",new Point(450,300),panel);


}
}

最佳答案

SecondHand 组件没有出现有两个原因:

  1. 它未添加到面板
  2. 面板没有布局,因此它会以 0 x 0 的大小显示。

如评论中所述,请勿使用 getGraphics(),而是重写 paintComponent(Graphics) 方法。要触发该方法,请使用基于 Swing 的 Timer 来调用 repaint()。为了更快地获得更好的帮助,请发布 Minimal, Complete, and Verifiable example如下所示。获取示例图像的一种方法是热链接到 this Q&A 中看到的图像。 。下面我只是创建一个新的缓冲图像。

我在源代码中看到的其他问题。每个 JComponent 都是一个 ImageObserver,因此在绘制图像时使用它而不是 null。仔细查看其他代码注释以获得其他提示。

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class ClockFrame extends JFrame {

public ClockFrame() {
JPanel panel = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
setSize(1000, 1000);
panel.setSize(getWidth(), getHeight());
// this is NOT the way to go
//panel.setLayout(null);//!important
panel.setLayout(new GridLayout());
setResizable(false);
panel.setBackground(Color.RED);
SecondHand sHand = new SecondHand("./res/icon/sechand.png", new Point(450, 300), panel);
panel.add(sHand); // THIS is impoertant
// this should be done after all components are added!
setVisible(true);
}

public static void main(String[] a) {
ClockFrame c = new ClockFrame();
}
}

class SecondHand extends JComponent implements Runnable {

private JPanel fpanel;
private BufferedImage bImage;
private Thread secThread;
private Point center;
double radPerSec = Math.PI / 648000;

public SecondHand(String path, Point p, Object o) {
try {
bImage
= new BufferedImage(900, 600, BufferedImage.TYPE_INT_RGB);
//ImageIO.read(new File(path));
} catch (Exception e) {
e.printStackTrace();
}
//setLocation((int)p.getX(),(int)p.getY());
setLocation(0, 0);
center = p;
((JPanel) o).add(this);
fpanel = (JPanel) o;

secThread = new Thread(this);
secThread.start();
}

public void run() {
while (true) {
try {
Thread.sleep(1000);
Graphics2D g2 = (Graphics2D) getGraphics();
g2.drawImage(bImage, 0, 0, null);
//fpanel.repaint();

} catch (InterruptedException e) {

e.printStackTrace();
}
}
}
}

关于java - 无法看到添加到 JPanel 的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42249124/

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