Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to use JPanel.createImage() method, how to create a GraphicsD object

843806Dec 5 2007 — edited Dec 5 2007
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;

public class GUIDriver implements ActionListener {
//static JTextArea output;
private JLabel help;
static JScrollPane scrollPane;
ActionListener addaction = null;
private static JFrame mainFrame;
private ArrayList alist;
private static Canvas canvas;
/**
* Factory method to Create the MenuBar.
*/
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
/**
* Create the menu bar.
*/

menuBar = new JMenuBar();

//Build the first menu.
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menu.getAccessibleContext().setAccessibleDescription("File Option");
menuBar.add(menu);

//Build the options
menuItem = new JMenuItem("Open",KeyEvent.VK_O);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("Open a file");
menuItem.setActionCommand("open");
menuItem.addActionListener(this);
menu.add(menuItem);
ImageIcon icon = createImageIcon("go_but.gif");
menuItem = new JMenuItem("Save", icon);
menuItem.setMnemonic(KeyEvent.VK_S);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
menuItem.setActionCommand("save");
menu.add(menuItem);
menuItem.addActionListener(this);

menu.addSeparator();

menuItem = new JMenuItem("Exit",icon);
menuItem.setMnemonic(KeyEvent.VK_D);
menuItem.setActionCommand("exit");
menu.add(menuItem);
menuItem.addActionListener(this);

//Build the second menu
menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_E);
//Build the add object submenu;
menuItem = new JMenuItem("Add", icon);
menuItem.setMnemonic(KeyEvent.VK_A);
menuItem.setActionCommand("ADD");
menu.add(menuItem);
menuItem.addActionListener(this);
//Build the Remove submenu; 
submenu = new JMenu("Remove");
submenu.setMnemonic(KeyEvent.VK_R);
submenu.setActionCommand("remove");
menu.add(submenu);
//Build the options;
menuItem = new JMenuItem("All");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK));
menuItem.setActionCommand("removeall");
submenu.add(menuItem);
menuItem.addActionListener(this);
menuItem = new JMenuItem("By index");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.ALT_MASK));
menuItem.setActionCommand("removebyindex");
submenu.add(menuItem);
menuItem.addActionListener(this);
menu.add(submenu);
menu.addActionListener(this);

//Build the list submenu;
submenu = new JMenu("List");
submenu.setMnemonic(KeyEvent.VK_L);
submenu.setActionCommand("list");
//Build the options;
menuItem = new JMenuItem("All");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK));
menuItem.setActionCommand("listall");
submenu.add(menuItem);
menuItem.addActionListener(this);
menuItem = new JMenuItem("By index");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.ALT_MASK));
menuItem.setActionCommand("listbyindex");
submenu.add(menuItem);
menuItem.addActionListener(this);

menu.add(submenu);

menu.addActionListener(this);
menuBar.add(menu);
//Build second menu in the menu bar.
menu = new JMenu("Help");
menu.setMnemonic(KeyEvent.VK_H);
menu.getAccessibleContext().setAccessibleDescription("Help context");
menu.addActionListener(this);
menu.setActionCommand("help");
menuBar.add(menu);
// menuBar.addMouseListener(this);

return menuBar;
}

public Container createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);

//Create a scrolled text area.
help = new JLabel();
scrollPane = new JScrollPane(help);
scrollPane.setForeground(Color.red);
JPanel temp = new JPanel();
temp.setSize(contentPane.getWidth(), 15);
temp.setLayout(new BorderLayout());
temp.add(scrollPane,BorderLayout.SOUTH);
temp.setForeground(Color.green);
scrollPane.setSize(contentPane.getWidth(), 15);
//Create the canvas
JPanel mainPanel, canvasPanel ;
Graphics2D graphic;
Color backgroundColor, foregroundColor ;
Image canvasImage;
BufferedImage bufferImage;
mainPanel = new JPanel();
canvasPanel = new JPanel();
backgroundColor=Color.white;
foregroundColor = Color.black;
canvasPanel = new JPanel();
mainPanel.add(canvasPanel);


Dimension size = mainPanel.getSize();

canvasImage = canvasPanel.createImage(size.width, size.height);

graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(Color.red);
graphic.fillRect(0, 0, 400, 400);
graphic.setColor(Color.black);
contentPane.add(canvas,BorderLayout.CENTER);

//Add the text area to the content pane.
contentPane.add(temp, BorderLayout.SOUTH);

return contentPane;
}
if i run it, the eclipse reminded me below

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUIDriver.createContentPane(GUIDriver.java:175)//this line:
graphic = (Graphics2D)canvasImage.getGraphics();
at GUIDriver.createAndShowGUI(GUIDriver.java:210)
at GUIDriver.access$0(GUIDriver.java:201)
at GUIDriver$1.run(GUIDriver.java:225)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


i create the jpanel but why i can not create the Image object.
I want to use that image to obtain the graphics object to draw some other things.

Edited by: flapjack on Dec 5, 2007 3:34 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 2 2008
Added on Dec 5 2007
4 comments
232 views