无法看到图形添加到JPanel

时间:2017-02-15 12:14:14

标签: java swing

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

我的代码显示如下,ClockFrame new a 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);


  }
}

1 个答案:

答案 0 :(得分:2)

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

  1. 它未添加到panel
  2. panel没有布局,所以它的大小为0 x 0。
  3. 正如评论中所述,请坚持使用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(); } } } } 。请仔细查看其他代码注释以获取其他提示。

    void Roster::readStudentRecord(string file)
    {
        ifstream in;
        string studentID;
        string line;
        int ola, cla, quiz, homework, exam, bonus, total, final = 0;
    
        in.open(file.c_str());
    
        getline(in, line);
        while (in >> studentID) {
            in >> cla >> ola >> quiz >> homework >> exam >> bonus >> total >> final;
            m_students[m_studentNum].Student::setID(studentID);
            m_students[m_studentNum].Student::changeScore(Student::CLA, cla);
            m_students[m_studentNum].Student::changeScore(Student::OLA, ola);
            m_students[m_studentNum].Student::changeScore(Student::QUIZ, quiz);
            m_students[m_studentNum].Student::changeScore(Student::HOMEWORK, homework);
            m_students[m_studentNum].Student::changeScore(Student::EXAM, exam);
            m_students[m_studentNum].Student::changeScore(Student::BONUS, bonus);
            total = cla + ola + quiz + homework + exam + bonus;
            m_students[m_studentNum].Student::setTotal(total);
            if (total >= 90) {
                m_students[m_studentNum].Student::setLetterGrade('A');
            }
            else if (total >= 80 && total < 90) {
                m_students[m_studentNum].Student::setLetterGrade('B');
            }
            else if (total >= 70 && total < 80) {
                m_students[m_studentNum].Student::setLetterGrade('C');
            }
            else if (total >= 60 && total < 70) {
                m_students[m_studentNum].Student::setLetterGrade('D');
            }
            else {
                m_students[m_studentNum].Student::setLetterGrade('F');
            }
            m_studentNum++;
        }
    }