/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package paint1;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
/**
*
* @author Jeanine
*/
public class MyPanel extends JPanel {
private int startX,
startY,
endX,
endY;
public int flag;
BufferedImage grid;
Graphics2D gc;
public MyPanel()
{
startX = 0;
startY =0;
endX=100;
endY=100;
flag = 0; //DrawRect as default
addMouseListener(new MouseComp());
}
//end copy
public void setLayout() {
}
public void PaintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if(grid == null){
int w = this.getWidth();
int h = this.getHeight();
grid = (BufferedImage)(this.createImage(w,h));
gc = grid.createGraphics();
gc.setBackground(Color.RED);
}
repaint();
g2.drawImage(grid, null, 0, 0);
}
/* Reads the mouse for actions on the panel */
public class MouseComp implements MouseListener
{
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mousePressed(MouseEvent e)
{
startX = e.getX();
startY = e.getY();
}
/* Determines the start and end points of the drawing shape */
public void mouseReleased(MouseEvent e){
int tempX = e.getX();
int tempY = e.getY();
if (flag == 1 || flag == 2 || flag == 3 || flag == 4)
{
if ((startX < tempX) && (startY < tempY))
{
endX = e.getX() - startX;
endY = e.getY() - startY;
}
else if ((startX > tempX) && (startY < tempY))
{
endX = startX; // Swap starting position X with ending X
startX = tempX;
endX -= startX;
endY = e.getY() - startY;
}
else if ((startX < tempX) && (startY > tempY))
{
endX = e.getX() - startX;
endY = startY; // Swap starting position Y with ending Y
startY = tempY;
endY -= startY;
}
else
{
endX = startX; // Swap starting position X with ending X
startX = tempX;
endX -= startX;
endY = startY; // Swap starting position Y with ending Y
startY = tempY;
endY -= startY;
}
}
else {
endX = e.getX();
endY = e.getY();
}
draw();
}
public void mouseExited(MouseEvent e){}
}
/* Clears the screen of all drawing objects */
public void clear() {
grid = null;
}
/* Draws on screen, shape varying on the flag of the button pressed */
public void draw()
{
Graphics2D g2 = (Graphics2D)getGraphics();
if (flag == 0) { }
else if (flag == 1)
{
gc.fillRect(startX, startY, endX, endY);
}
else if (flag == 2)
{
gc.fillOval(startX, startY, endX, endY);
}
else if (flag == 3)
{
gc.drawRect(startX, startY, endX, endY);}
else if (flag == 4)
{
gc.drawOval(startX, startY, endX, endY);
}
else if (flag == 5) //line
{
gc.drawLine(startX, startY, endX, endY);
}
}
}
Hi- This is my code for the JPanel of a simple paint program I am working. It works fine until I try to add the bits about rebuffering.
This part to be exact:
public void PaintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if(grid == null){
int w = this.getWidth();
int h = this.getHeight();
grid = (BufferedImage)(this.createImage(w,h));
gc = grid.createGraphics();
gc.setBackground(Color.RED);
}
repaint();
g2.drawImage(grid, null, 0, 0);
}
I believe I don't need to call this method anywhere because it works whenever the screen is loaded, resized, etc. (I added the background color to the grid, just to see if it is loading and it does not look like it is loading). I can't seem to find what I'm doing wrong- any help would be greatly appreciated.
Edit: These are the errors:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at paint1.MyPanel.draw(MyPanel.java:143)
at paint1.MyPanel$MouseComp.mouseReleased(MyPanel.java:114)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I realize grid is null to begin with, but then I set it at-
grid = (BufferedImage)(this.createImage(w,h));