User Tools

Site Tools


generic_starter_code

This is a Graphics Application that quits on a close window, and a KeyListener to react to keys typed on the keyboard.

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyGenericApplication extends JPanel implements KeyListener
{
    public static int WIDTH=800;
    public static int HEIGHT=600;
    private Font titleFont, regularFont;
    
	
    public MyGenericApplication()
    {
    	
    	//initialize variables here...
    	titleFont = new Font("Roman", Font.BOLD, 18);
    	regularFont = new Font("Helvetica", Font.PLAIN, 12);
    	
    }
	public static void main(String[] args) {
		MyGenericApplication app= new MyGenericApplication();
		JFrame window = new JFrame("My Generic Application");
		window.setSize(WIDTH, HEIGHT);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.getContentPane().add(app);
		window.addKeyListener(app);
		//window.pack();
		window.setVisible(true);
		
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(),getHeight());
		g.setColor(Color.BLUE);
		g.setFont(titleFont);
		g.drawString("My Generic Application", 20, 20);
		g.setColor(Color.BLACK);
		g.setFont(regularFont);
		g.drawString("Version 1.0", 20, 40);
		
		
	}
	// update is a workaround to cure Windows screen flicker problem
	public void update(Graphics g){
		paint(g);
	}

	// These 3 methods need to be declared to implement the KeyListener Interface
	@Override
	public void keyTyped(KeyEvent e) {}

	@Override
	public void keyPressed(KeyEvent e) {}

	@Override
	public void keyReleased(KeyEvent e) {}

}
generic_starter_code.txt · Last modified: 2019/05/22 10:06 by frchris