Reputation: 532
I intend to implement a clock with secondhand, but I cannot display second hand properly.
My code shows bellow, ClockFrame new a SecondHand and pass panel to SecondHand. SecondHand is expected to get seconds and update itself. I think g2.drawImage(bImage, 0, 0, null); show my bitmap, but not at all. I am really confused. Thanks in advance.
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);
}
}
Upvotes: 0
Views: 141
Reputation: 168815
The SecondHand
component was not appearing for two reasons:
panel
.panel
has no layout, so it would have been shown at a size of 0 x 0.As mentioned in comments, do nut use getGraphics()
, but instead override the paintComponent(Graphics)
method. To trigger that method, use a Swing based Timer
to call repaint()
.
For better help sooner, post a Minimal, Complete, and Verifiable example like as seen below. One way to get image(s) for an example is to hot link to images seen in this Q&A. Below I just create a new buffered image.
Other problems I see in the source. Every JComponent
is an ImageObserver
so use it instead of null
when drawing an image. Look at the other code comments carefully for other tips.
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();
}
}
}
}
Upvotes: 2