Mega Code Archive

 
Categories / Java / 2D Graphics GUI
 

Extends JComponent to exercise Graphics

/*    This program is a part of the companion code for Core Java 8th ed.    (http://horstmann.com/corejava)    This program is free software: you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation, either version 3 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program.  If not, see <http://www.gnu.org/licenses/>. */ import java.awt.EventQueue; import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.JFrame; /**  * @version 1.32 2007-06-12  * @author Cay Horstmann  */ public class NotHelloWorld {    public static void main(String[] args)    {       EventQueue.invokeLater(new Runnable()          {             public void run()             {                NotHelloWorldFrame frame = new NotHelloWorldFrame();                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                frame.setVisible(true);             }          });    } } /**  * A frame that contains a message panel  */ class NotHelloWorldFrame extends JFrame {    public NotHelloWorldFrame()    {       setTitle("NotHelloWorld");       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);       // add panel to frame       NotHelloWorldComponent comp = new NotHelloWorldComponent();       add(comp);    }    public static final int DEFAULT_WIDTH = 300;    public static final int DEFAULT_HEIGHT = 200; } /**  * A component that displays a message.  */ class NotHelloWorldComponent extends JComponent {    public void paintComponent(Graphics g)    {       g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y);    }    public static final int MESSAGE_X = 75;    public static final int MESSAGE_Y = 100; }