Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
LightI3ulb
Oct 28, 2006

Standard pleasure model.
I hata Java. Can someone explain to me why Java keeps spitting out:
code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at Whiteboard.<init>(Whiteboard.java:16)
	at Whiteboard.createAndShowGUI(Whiteboard.java:82)
	at Whiteboard.access$0(Whiteboard.java:75)
	at Whiteboard$1.run(Whiteboard.java:21)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
with

code:
import javax.swing.*;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Whiteboard extends JPanel implements MouseListener {
	Drawspace drawSpace;
	JTextArea textArea;
	Graphics2D g = (Graphics2D) drawSpace.getGraphics();

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
    	}

    public Whiteboard() {
        super(new GridBagLayout());
        GridBagLayout gridbag = (GridBagLayout)getLayout();
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.VERTICAL;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.weightx = 1.0;
        c.weighty = 1.0;

        c.insets = new Insets(1, 1, 1, 1);
        drawSpace = new Drawspace(new Color(0.98f, 0.97f, 0.85f));
	c.anchor = GridBagConstraints.NORTHWEST;
        gridbag.setConstraints(drawSpace, c);
        add(drawSpace);

	JButton drawRectangle = new JButton("Rectangle");
	drawRectangle.setHorizontalAlignment(JButton.RIGHT);
	add(drawRectangle);


        setPreferredSize(new Dimension(450, 450));
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
	}
	
	public void mousePressed(MouseEvent e) {
			g.drawLine((e.getX()), (e.getY()), ((e.getX())+1), ((e.getY())+1));
			g.dispose();
	}
	
	public void mouseReleased(MouseEvent e) {
	
	}
	
	public void mouseEntered(MouseEvent e) {
	
	}
	
	public void mouseExited(MouseEvent e) {
	
	}
	
	public void mouseClicked(MouseEvent e) {
		g.drawLine((e.getX()), (e.getY()), ((e.getX())+1), ((e.getY())+1));
		g.dispose();
	}

    

    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);

        JFrame frame = new JFrame("Whiteboard");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   
        JComponent newContentPane = new Whiteboard();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

   
        frame.pack();
        frame.setVisible(true);
    }
}
code:
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Color;

public class Drawspace extends JLabel {
    Dimension minSize = new Dimension(100, 100);

    public Drawspace(Color color) {
        setBackground(color);
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.black));
    }

    public Dimension getMinimumSize() {
        return minSize;
    }

    public Dimension getPreferredSize() {
        return minSize;
    }
} 

Adbot
ADBOT LOVES YOU

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply