Hallo,
ich habe mir mit Graphics eine Digital Uhr gezeichnet, die secunden Ticken auch, aber jetzt spielt meine Oberfläche total verrückt, und ich bekomme eine Fehlermeldung.
Solange ich mit einem anderen Panel über die Uhr Wische siehts noch Ok aus. XD
lg Paul
ich habe mir mit Graphics eine Digital Uhr gezeichnet, die secunden Ticken auch, aber jetzt spielt meine Oberfläche total verrückt, und ich bekomme eine Fehlermeldung.
Solange ich mit einem anderen Panel über die Uhr Wische siehts noch Ok aus. XD
lg Paul
Java:
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.Timer;
import javax.swing.JPanel;
public class DateTime
extends JPanel
{
private int sizex = 150;
private int sizey = 50;
private int locationx = 800;
private int locationy = 500;
private String sday;
private String shour;
private String sminute;
private String ssecond;
private int day;
private int hour;
private int minute;
private int second;
private GregorianCalendar calendar;
private Timer timer;
private Graphics2D g2;
public DateTime()
{
calendar = new GregorianCalendar();
day = calendar.get(calendar.DAY_OF_WEEK);
switch(day)
{
case 0:
sday = "sat";
break;
case 1:
sday = "sun";
break;
case 2:
sday = "mon";
break;
case 3:
sday = "tue";
break;
case 4:
sday = "wed";
break;
case 5:
sday = "thu";
break;
case 6:
sday = "fry";
break;
}
hour = calendar.get(calendar.HOUR_OF_DAY);
minute = calendar.get(calendar.MINUTE);
second = calendar.get(calendar.SECOND);
int delay = 1000;
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
second++;
ssecond = new Integer(second).toString();
shour = new Integer(hour).toString();
sminute = new Integer(minute).toString();
repaint();
}
};
timer = new Timer(delay, taskPerformer);
timer.start();
setSize(sizex, sizey);
setLocation(locationx, locationy);
setOpaque(true);
setBackground(new Color(0,0,0,0));
setVisible(true);
}
public void paintComponent(Graphics g) {
g2 = (Graphics2D)g;
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
final BasicStroke dashed = new BasicStroke(2);
g2.setStroke(dashed);
super.paintComponent(g2);
g2.setColor(new Color(20,20,90));
g2.drawRoundRect(3, 3, getWidth()-6, getHeight()-6, 15, 15);
g2.setColor(new Color(250,180,180));
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, .4f));
g2.fillRoundRect(3, 3, getWidth()-7, getHeight()-7, 15, 15);
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, .9F));
g2.setColor(new Color(0,0,0));
g2.drawString(sday, 8, 17);
g2.drawString(shour, 35, 17);
g2.drawString(sminute, 55, 17);
g2.drawString(ssecond, 75, 17);
}
}
Java:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: String is null
at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2795)
at DateTime.paintComponent(DateTime.java:134)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: String is null
at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2795)
at DateTime.paintComponent(DateTime.java:134)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)