我打算用二手时钟,但是我不能正确显示二手。

我的代码显示波纹管,ClockFrame新增了SecondHand并将面板传递给SecondHand。 SecondHand有望获得秒数并进行自我更新。我认为g2.drawImage(bImage,0,0,null);显示我的位图,但完全不显示。我真的很困惑。提前致谢。

Clock.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();
  }
}


SecondHand.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();
           }
    }

}


}


ClockFrame.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组件未出现的原因有两个:


它未添加到panel
panel没有布局,因此将以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();
            }
        }
    }
}

10-08 02:01