Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ca.lambtoncollege.hauntedhouse.client; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; /** * * @author alpenliebe */ public class Canvas extends JFrame { //private int x=50,y=50; private int localTeam; private int remoteTeam; private BasesController bc; private RootPanel rp; private Mask mask; private boolean runnable; private ChannelHandlerContext ctx; /** * Creates new form Client */ public Canvas() { //initComponents(); bc = null; runnable = false; } public Canvas(ChannelHandlerContext ctx) { this(); this.ctx = ctx; } public RootPanel getRp() { return rp; } public ChannelHandlerContext getCtx() { return ctx; } public boolean isRunnable() { return runnable; } public void setLocalTeam(int localTeam) { this.localTeam = localTeam; } public void setRemoteTeam(int remoteTeam) { this.remoteTeam = remoteTeam; } public void setTeams(int localTeam) { setLocalTeam(localTeam); setRemoteTeam(localTeam == PropertyMgr.TEAMA ? PropertyMgr.TEAMB : PropertyMgr.TEAMA); } public boolean isLocal(int team) { return team == localTeam; } public int getLocalTeam() { return localTeam; } public int getRemoteTeam() { return remoteTeam; } public void click() { String str = String.format("%d,%d,%d,%d,%s", PropertyMgr.DURING_THE_GAME, PropertyMgr.FRAME_ID, PropertyMgr.CLICK, PropertyMgr.CLICK, ctx.channel().remoteAddress()); writeAndFlush(str); } public void unregisterLocally() { unregister(localTeam); click(); } public void unregisterRemotely() { unregister(remoteTeam); } public void unregister(int team) { if (runnable) { System.out.println("Frame click"); if (bc != null) bc.unregister(team); } } public boolean isEnemy(int team) { return team == getRemoteTeam(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 631, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 380, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents @Override public void paint(Graphics g) { super.paint(g); // Color c = g.getColor(); // g.setColor(Color.WHITE); // g.fillOval(x, y, 30, 30); // g.setColor(c); // y+=5; // if(y>350)y=0; //bc.drawPaths(g); //this.getContentPane().paint(g); } @Override public void paintComponents(Graphics g) { super.paintComponents(g); //To change body of generated methods, choose Tools | Templates. //bc.drawPaths(g); //bc.drawPaths(g); } //@Override //public void update(Graphics g) { //super.update(g); // if(screen==null) screen = this.createImage(FRAME_WIDTH, FRAME_HEIGHT); // Graphics screenG = screen.getGraphics(); // Color c = screenG.getColor(); // screenG.setColor(Color.orange); // screenG.fillRect(x, y, FRAME_WIDTH, FRAME_HEIGHT); // screenG.setColor(c); // bc.drawPaths(screenG); // paint(screenG); // g.drawImage(screen, x, y, null); //} public BasesController getBc() { return bc; } public void writeAndFlush(String str) { ChannelFuture lastWriteFuture = null; try { lastWriteFuture = ctx.channel().writeAndFlush(str + "\n"); if (lastWriteFuture != null) { lastWriteFuture.sync(); } } catch (InterruptedException ex) { Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex); ctx.close(); System.exit(0); } } public void waiting(String str) { mask = new Mask(); mask.setBounds(0, 0, this.getWidth(), this.getHeight()); mask.setText(str); this.getLayeredPane().add(mask); this.getLayeredPane().setComponentZOrder(mask, 0); repaint(); } public void close() { try { String str = String.format("%d,,%s", PropertyMgr.BYE, ctx.channel().remoteAddress()); writeAndFlush(str); ctx.channel().closeFuture().sync(); } catch (InterruptedException ex) { Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex); } finally { // The connection is closed automatically on shutdown. ctx.close(); System.exit(0); } } public void stop(String str) { //setVisible(false); this.getLayeredPane().remove(mask); repaint(); runnable = false; waiting(str); } public void start() { this.getLayeredPane().remove(mask); repaint(); runnable = true; } public void lanchFrame() { //this.getContentPane().setBackground(Color.orange); rp = new RootPanel(); //rp.setBounds(0, 0, this.getWidth(), this.getHeight()); setContentPane(rp); pack(); setLocation(100, 100); setTitle(PropertyMgr.GAME_TITLE); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); setVisible(true); bc = new BasesController(this); waiting(PropertyMgr.STR_WAITING_FOR_ANOTHER_PLAYER); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mousePressed(e); //To change body of generated methods, choose Tools | Templates. unregisterLocally(); } }); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); close(); } }); } /** * @param args the command line arguments */ // public static void main(String args[]) { // /* Set the Nimbus look and feel */ // //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. // * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html // */ // try { // for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { // if ("Nimbus".equals(info.getName())) { // UIManager.setLookAndFeel(info.getClassName()); // break; // } // } // } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { // Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex); // } // //</editor-fold> // // /* Create and display the form */ // java.awt.EventQueue.invokeLater(new Runnable() { // public void run() { // Canvas canvas = new Canvas(); // canvas.lanchFrame(); // //canvas.waiting(); // } // }); // // } private class Mask extends JPanel { private JLabel label; private AlphaComposite composite; public Mask() { label = new JLabel(); composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f); add(label); } public void setText(String str) { label.setText(str); } @Override public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setComposite(composite); //SwingUtil.setLabelTop(label,label.getText() , getWidth(), getHeight()); super.paint(g2); } } // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables }