Saturday, July 13, 2013

How can I add JButton to the Window

This is my ScreenManager class which is used for getting the perfect DisplayModeimport java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.lang.reflect.InvocationTargetException; import javax.swing.JFrame; public class ScreenManager { private GraphicsDevice vc; //give vc access to monitor screen public ScreenManager() { GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); vc = e.getDefaultScreenDevice(); } //get all compatible DM public DisplayMode[] getCompatibleDisplayModes() { return vc.getDisplayModes(); } //compares DM passed into vc DMfor ( int x = 0; x < modes.length; x++) { for( int y = 0; y < goodModes.length; y++) { if(displayModesMatch(modes[x], goodModes[y])) { return modes[x]; } } } return null; } //get current DM public DisplayMode getCurrentDisplayMode() { return vc.getDisplayMode(); } //checks if two modes match each other public boolean displayModesMatch(DisplayMode m1, DisplayMode m2) { if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()) { return false; } if(m1.getBitDepth() != DisplayMode.BITDEPTHMULTI && m2.getBitDepth() != DisplayMode.BITDEPTHMULTI &} if(m1.getRefreshRate() != DisplayMode.REFRESHRATEUNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESHRATEUNKNOWN &} return true; } //make frame full screen public void setFullScreen(DisplayMode dm) { JFrame f = new JFrame(); f.setUndecorated(true); f.setIgnoreRepaint(true); f.setResizable(true); vc.setFullScreenWindow(f); if( dm != null} catch(Exception ex) { } } f.createBufferStrategy(2); } //we will set graphics object to this public Graphics2D getGraphics() { Window w = vc.getFullScreenWindow(); if(w != null) { BufferStrategy s = w.getBufferStrategy(); return (Graphics2D)s.getDrawGraphics(); } else { return null; } } //update displaye public void update() { Window w = vc.getFullScreenWindow(); if( w != null ) { BufferStrategy s = w.getBufferStrategy(); if(!s.contentsLost()) { s.show(); } } } //return full screen window public Window getFullScreenWindow() { return vc.getFullScreenWindow(); } //get width of window public int getWidth() { Window w = vc.getFullScreenWindow(); if( w != null) { return w.getWidth(); } else { return 0; } } //get height of window public int getHeight() { Window w = vc.getFullScreenWindow(); if( w != null) { return w.getHeight(); } else { return 0; } } //get out of full screen public void restoreScreen() { Window w =vc.getFullScreenWindow(); if( w != null) { w.dispose(); } vc.setFullScreenWindow(null); } //create image compatible with your moitor public BufferedImage createCompatibleImage(int w, int h, int t) { Window win = vc.getFullScreenWindow(); if( win != null) { GraphicsConfiguration gc = win.getGraphicsConfiguration(); return gc.createCompatibleImage(w, h, t); } else { return null; } } }



This is the Core class which I used for initializing the screen, looping mechanism, etc.




import java.awt.*; import javax.swing.*; public abstract class Core { private static DisplayMode modes[] = { new DisplayMode(800, 600, 32, 0), new DisplayMode(800, 600, 24, 0), new DisplayMode(800, 600, 16, 0), new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 24, 0), new DisplayMode(640, 480, 16, 0), }; private boolean running; protected ScreenManager s; //stop method public void stop() { running = false; } //call inintgameloop(); } finally { s.restoreScreen(); } } //set to full screen public void init() { s = new ScreenManager(); DisplayMode dm = s.findFirstCompatibleMode(modes); s.setFullScreen(dm); Window w = s.getFullScreenWindow(); w.setFont(new Font("Arial", Font.PLAIN, 20)); w.setBackground(Color.GREEN); w.setForeground(Color.WHITE); running = true; } //main gameloop public void gameloop() { long startTime = System.currentTimeMillis(); long cumTime = startTime; while(running) { long timePassed = System.currentTimeMillis() - cumTime; cumTime += timePassed; update(timePassed); Graphics2D g = s.getGraphics(); draw(g); g.dispose(); s.update(); try { Thread.sleep(20); } catch(Exception ex) { } } } //update animation public void update(long timePassed) { } //draws to the screen public abstract void draw(Graphics2D g); }



This is the actual Game class which I used for designing starting screen on window



import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Game extends Core implements ActionListener { public static void main(String []args) { new Game().run(); } private JButton b1, b2, b3; private JPanel p; private String mess = ""; public void init() { super.init(); JButton b1 = new JButton("Start"); JButton b2 = new JButton("Credits"); JButton b3 = new JButton("Exit"); p = new JPanel(); p.add(b1); p.add(b2); p.add(b3); Window w = s.getFullScreenWindow(); w.setFocusTraversalKeysEnabled(false); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); mess = "Press Exit to Close Application"; } //draw public synchronized void draw(Graphics2D g) { Window w = s.getFullScreenWindow(); g.setColor(w.getBackground()); g.fillRect(0, 0, s.getWidth(), s.getHeight()); g.setColor(w.getForeground()); g.drawString(mess, 30, 30); } //using action method public void actionPerformed(ActionEvent e) { if( e.getSource() == b3 ) { stop(); } else { mess = "Clicked : " + e.getSource(); } } }



How can I add JButton on screen & make them visible

I tried adding JPanel in Winodw

by using method but it does not work

I even tried setting window w.setVisible(true)

It also didn't worked
Full Post

No comments:

Post a Comment