Graphics2D Uhr refreshen

p4ul

Mitglied
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

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)
 

Marco13

Top Contributor
Wenn er eine NullPointerException wirft, wo er bei "Graphics2D.drawString" sagt: String is null, dann liegt das wohl daran, dass bei Graphics2D.drawString ein String übergeben wurde, der null ist.

Mit
private String sday = "";
private String shour = "";
private String sminute = "";
private String ssecond = "";
sind die zu zeichnenden Strings am Anfang NICHT null, dann sollte da auch nichts mehr fliegen....
 

p4ul

Mitglied
Vielen Dank ,
es hat zusammen mit "setOpaque(false)" jetzt endlich funktioniert.

Java:
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.Timer;

import javax.swing.JPanel;

public class MyClock
extends JPanel implements ActionListener
{
	private int sizex = 150;
	private int sizey = 50;
	
	private Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();

	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 MyClock()
	{
		
		int delay = 1000;
		timer = new Timer(delay, this);
		timer.start();
		
		int locationx = new Integer((int) (screensize.getWidth()-screensize.getWidth()));
		int locationy = new Integer((int) (screensize.getHeight()-sizey));

		setSize(sizex, sizey);
		setLocation(locationx, locationy);
		
		setOpaque(false);
		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);

    }

	@Override
	public void actionPerformed(ActionEvent e) {
		
		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);
		
		ssecond = new Integer(second).toString();
		shour = new Integer(hour).toString();
		sminute = new Integer(minute).toString();

  		super.repaint();
		
	}
}
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
M Graphics2D Objekt mir der Maus erstellen Java Basics - Anfänger-Themen 5
K Zwei Fragen zu Graphics/Graphics2D Java Basics - Anfänger-Themen 5
Mr. Pink Graphics2D in anderer Methode aufrufen Java Basics - Anfänger-Themen 6
B Erste Schritte Drehen in Graphics2d Java Basics - Anfänger-Themen 2
T Threads Thread & Graphics2D Java Basics - Anfänger-Themen 8
C Graphics2D - DrawRect - Warum Linien teilweise gefüllt? Java Basics - Anfänger-Themen 12
D Graphics2D (Welle darstellen) Java Basics - Anfänger-Themen 2
F Graphics2D nützlich für R³? Java Basics - Anfänger-Themen 11
C Graphics2D auf anderes Graphics2D übertragen Java Basics - Anfänger-Themen 5
K Graphics2D und BufferedImage Java Basics - Anfänger-Themen 3
P Graphics2D.drawString: Zahlen rechtsbündig ausrichten? Java Basics - Anfänger-Themen 2
Z Farbwechsel JButton mit Graphics2D Java Basics - Anfänger-Themen 7
T Anfänge mit Graphics2D Java Basics - Anfänger-Themen 15
X Kreize zeichnen in Graphics2D Java Basics - Anfänger-Themen 2
D Graphics2D.drawImage(Image, int, int, ?); Java Basics - Anfänger-Themen 2
R Programm aktualisieren/refreshen Java Basics - Anfänger-Themen 7
R txt. Datei mit Einträgen refreshen Java Basics - Anfänger-Themen 5
Schandro "refreshen" einer GUI mit Listenern Java Basics - Anfänger-Themen 2
T Konsole refreshen Java Basics - Anfänger-Themen 6
J JTable refreshen Java Basics - Anfänger-Themen 5
J JTable refreshen Java Basics - Anfänger-Themen 8

Ähnliche Java Themen


Oben