import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.text.DecimalFormat; import java.util.ArrayList; public class makine extends JApplet{ private int counter = 0; private ArrayList<String> liste; private String[] yazi; private JButton one,two,three,four,five, six,seven,eight,nine,zero, arti,eksi,bolu,carpi,CE,esit; private JTextField ekran; private JLabel yazar; private DecimalFormat dec; public String ToString(Double d) { return d.toString(); } //@override public void init() { yazi = new String[11]; liste = new ArrayList<String>(); for(int i = 0;i<11;i++){ yazi[i] = ""; } ActionHandler handler = new ActionHandler(); Container container = this.getContentPane(); container.setLayout(new FlowLayout(1,8,2)); dec = new DecimalFormat("0.000"); one = new JButton(); one.setText("1"); one.setSize(20, 20); one.setLocation(40, 40); container.add(one); one.addActionListener(handler); two = new JButton(); two.setText("2"); two.setSize(20, 20); two.setLocation(40, 40); container.add(two); two.addActionListener(handler); three = new JButton(); three.setText("3"); three.setSize(20, 20); three.setLocation(70, 40); container.add(three); three.addActionListener(handler); arti = new JButton(); arti.setText("+"); arti.setSize(20, 20); arti.setLocation(100, 40); container.add(arti); arti.addActionListener(handler); four = new JButton(); four.setText("4"); four.setSize(20, 20); four.setLocation(10, 70); container.add(four); four.addActionListener(handler); five = new JButton(); five.setText("5"); five.setSize(20, 20); five.setLocation(40, 70); container.add(five); five.addActionListener(handler); six = new JButton(); six.setText("6"); six.setSize(20, 20); six.setLocation(70, 70); container.add(six); six.addActionListener(handler); eksi = new JButton(); eksi.setText("-"); eksi.setSize(20, 20); eksi.setLocation(10, 70); container.add(eksi); eksi.addActionListener(handler); seven = new JButton(); seven.setText("7"); seven.setSize(20, 20); seven.setLocation(10, 100); container.add(seven); seven.addActionListener(handler); eight = new JButton(); eight.setText("8"); eight.setSize(20, 20); eight.setLocation(40, 100); container.add(eight); eight.addActionListener(handler); nine = new JButton(); nine.setText("9"); nine.setSize(20, 20); nine.setLocation(70, 100); container.add(nine); nine.addActionListener(handler); carpi = new JButton(); carpi.setText("x"); carpi.setSize(20, 20); carpi.setLocation(100, 130); container.add(carpi); carpi.addActionListener(handler); CE = new JButton(); CE.setText("CE"); CE.setSize(20, 20); CE.setLocation(10, 100); container.add(CE); CE.addActionListener(handler); zero = new JButton(); zero.setText("0"); zero.setSize(20, 20); zero.setLocation(40, 130); container.add(zero); zero.addActionListener(handler); bolu = new JButton(); bolu.setText("/"); bolu.setSize(20, 20); bolu.setLocation(100, 100); container.add(bolu); bolu.addActionListener(handler); esit = new JButton(); esit.setText("="); esit.setSize(20, 20); esit.setLocation(70, 100); container.add(esit); esit.addActionListener(handler); ekran = new JTextField(15); ekran.setText(" "); ekran.setEditable(false); ekran.setDisabledTextColor(Color.BLUE ); ekran.setVisible(true); ekran.setAlignmentY(JTextField.RIGHT_ALIGNMENT); ekran.setHorizontalAlignment(JTextField.RIGHT); ekran.setLocation(10,10); container.add(ekran); ekran.addActionListener(handler); yazar = new JLabel("Tested and Programmed by BY"); container.add(yazar); } //inner class private class ActionHandler implements ActionListener { public void actionPerformed(ActionEvent event) { if(event.getSource() == one){ if(counter<11){ yazi[counter]+="1"; ekran.setText(yazi[counter]); }else ; }if(event.getSource() == two){ if(counter<11){ yazi[counter]+="2"; ekran.setText(yazi[counter]); }else ; }if(event.getSource() == three){ if(counter<11){ yazi[counter]+="3"; ekran.setText(yazi[counter]); }else; }if(event.getSource() == four){ if(counter<11){ yazi[counter]+= "4"; ekran.setText(yazi[counter]); }else; }if(event.getSource() == five){ if(counter<11){ yazi[counter] += "5"; ekran.setText(yazi[counter]); }else; }if(event.getSource() == six){ if(counter<11){ yazi[counter] += "6"; ekran.setText(yazi[counter]); }else; }if(event.getSource() == seven){ if(counter<11){ yazi[counter] += "7"; ekran.setText(yazi[counter]); }else; }if(event.getSource() == eight){ if(counter<11){ yazi[counter] += "8"; ekran.setText(yazi[counter]); }else; }if(event.getSource() == nine){ if(counter<11){ yazi[counter] += "9"; ekran.setText(yazi[counter]); }else; }if(event.getSource() == zero){ if(counter<11){ yazi[counter] += "0"; ekran.setText(yazi[counter]); }else; }if(event.getSource() == arti){ try{ if(Double.parseDouble(yazi[counter])>=0){ counter++; yazi[counter] = "+"; counter++; }else; }catch(Exception e){ ekran.setText("Syntax Error"); } }if(event.getSource() == eksi){ try{ if(Double.parseDouble(yazi[counter])>=0){ counter++; yazi[counter] = "-"; counter++; }else; }catch(Exception e){ ekran.setText("Syntax Error"); } }if(event.getSource() == bolu){ try{ if(Double.parseDouble(yazi[counter])>=0){ counter++; yazi[counter] = "/"; counter++; }else; }catch(Exception e){ ekran.setText("Syntax Error"); } }if(event.getSource() == carpi){ try{ if(Double.parseDouble(yazi[counter])>=0){ counter++; yazi[counter] = "*"; counter++; } }catch(Exception e){ ekran.setText("Syntax Error"); } }if(event.getSource() == CE){ for(int i = 0;i<11;i++) yazi[i] = ""; ekran.setText(""); counter = 0; }if(event.getSource() == esit){ //try{ for(int i = 0;i<11;i++){ liste.add(i,yazi[i]); } for(int i = 0;i<11;i++){ try{ if(yazi[i]=="/"){ yazi[i] = ToString(Double.parseDouble(yazi[i-1])/Double.parseDouble(yazi[i+1])); yazi[i-1] = yazi[i]; yazi[i+1] = yazi[i]; liste.remove(0); liste.add(0,yazi[i]); } }catch(Exception e){ ekran.setText("Syntax Error"); counter = 0; } } for(int i = 0;i<11;i++){ try{ if(yazi[i]=="*"){ yazi[i] = ToString(Double.parseDouble(yazi[i-1])*Double.parseDouble(yazi[i+1])); yazi[i-1] = yazi[i]; yazi[i+1] = yazi[i]; liste.remove(0); liste.add(0,yazi[i]); } } catch(Exception e){ ekran.setText("Syntax Error"); counter = 0; } } for(int i = 0;i<11;i++){ try{ if(yazi[i]=="+"){ yazi[i] = ToString(Double.parseDouble(yazi[i-1])+Double.parseDouble(yazi[i+1])); yazi[i-1] = yazi[i]; yazi[i+1] = yazi[i]; liste.remove(0); liste.add(0,yazi[i]); } } catch(Exception e){ ekran.setText("Syntax Error"); counter = 0; } } for(int i = 0;i<11;i++){ try{ if(yazi[i]=="-"){ yazi[i] = ToString(Double.parseDouble(yazi[i-1])-Double.parseDouble(yazi[i+1])); yazi[i-1] = yazi[i]; yazi[i+1] = yazi[i]; liste.remove(0); liste.add(0,yazi[i]); } } catch(Exception e){ ekran.setText("Syntax Error"); counter = 0; } } if(liste.isEmpty()){ ekran.setText("Syntax Error"); counter = 0;} else{ if(liste.get(0)!="") ekran.setText(liste.get(0).toString()); } counter = 0; for(int i = 0;i<11;i++) yazi[i] = ""; if(!liste.isEmpty()) liste.clear(); /*}catch(Exception e){ ekran.setText("Syntax Error"); }*/ } } } }
Showing posts with label actionlistener. Show all posts
Showing posts with label actionlistener. Show all posts
Saturday, 11 August 2012
ActionListener Example
Etiketler:
actionlistener,
Calc,
calculator,
class,
container,
DecimalFormat,
Java
Saturday, 9 June 2012
PingPong Game in Java
Board.java
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.Timer; public class Board extends JPanel implements ActionListener{ private final int width = 600; private final int height = 600; private int delay = 30;//time delay to update private int board_x;//board x location private int board_y;//board y location private int ball_x;//ball x location private int ball_y;//ball y location private float ball_xv;//ball x violation private float ball_yv;//ball y violation //conditions private boolean running = true; private boolean contin= true; private boolean tryagain = false; //for keyboard private boolean pause = false; private boolean left = false; private boolean right = false; //for mouse motions private boolean dragged = false; private int howfarx = 0; //how far mouse click x coordinate from object's x coordinate private Image ball; private Image board; private Timer timer; private Image background; private String msg = "presss"; public Board() { addKeyListener(new TAdapter()); addMouseMotionListener(new MAdapter()); addMouseListener(new MAdapter()); setBackground(Color.white); load(); setFocusable(true); init(); } public void load() { board = new ImageIcon("C:\\javapics\\board.png").getImage(); ball = new ImageIcon("C:\\javapics\\ae.png").getImage(); background = new ImageIcon("C:\\javapics\\backgroud.png").getImage(); } public void actionPerformed(ActionEvent e) { if(running) { if(contin) { ballmoving(); boardmove(); lose(); } } repaint(); } public void start() { ball_xv = -0.4f; ball_yv = -0.3f; board_x = 240; board_y = 580; ball_x = 300; ball_y = 240; } public void init() { start(); timer = new Timer(delay,this); timer.start(); } public void paint(Graphics g) { super.paint(g); if(contin) { g.drawImage(ball, ball_x, ball_y,this); g.drawImage(board, board_x, board_y,this); Toolkit.getDefaultToolkit().sync(); g.dispose(); } if(tryagain) { String msg2 = "Press Enter to try again"; Font small = new Font("Arial", Font.BOLD, 15); g.setColor(Color.black); g.setFont(small); g.drawString(msg2,200,300); } if(pause) { String msg2 = "Pause! Press Space to continue"; Font small = new Font("Helvetica", Font.BOLD, 15); g.setColor(Color.black); g.setFont(small); g.drawString(msg2,200,300); } if(!running) { g.drawImage(background, 0, 0,this); } } public void lose() { if(ball_y + 20 > height) { contin = false; tryagain = true; } } public void ballmoving() { if(ball_x < 1) ball_xv = Math.abs(ball_xv); else if((ball_x + 20) > (width-21)) ball_xv = -Math.abs(ball_xv); if(ball_y < 1) ball_yv = Math.abs(ball_yv); else if((ball_y+20) > (height-21)) if(ball_x >= board_x && ball_x <= (board_x+100) ) ball_yv = -Math.abs(ball_yv); update(delay); } public void update(int timedelay)//to update ball location on the screen { ball_x = (ball_x + (int)(ball_xv*delay)); ball_y = (ball_y + (int)(ball_yv*delay)); } public void boardmove()//to play with keyboard { if(left){ board_x -= 40; left = false; } if(right){ board_x += 40; right = false; } } private class MAdapter extends MouseAdapter { public void mousePressed(MouseEvent e) { int mousex = e.getX(); int mousey = e.getY(); if(mousex >= board_x && mousex <= (board_x+ 100)) if(mousey >= board_y && mousey <= (board_y+100)){ dragged = true; howfarx = mousex - board_x; }else { dragged = false; } } public void mouseDragged(MouseEvent e) { if(dragged){ board_x = e.getX() - howfarx; board_x = Math.max(0, board_x);//to prevent overflow from sides board_x = Math.min(board_x, (width-120));///////////////////// } } public void mouseExited(MouseEvent e) { dragged = false; howfarx = 0; } public void mouseMoved (MouseEvent e) {} // ignore these events public void mouseEntered (MouseEvent e) {} // ignore these events public void mouseClicked (MouseEvent e) {} // ignore these events public void mouseReleased(MouseEvent e) {} // ignore these events } private class TAdapter extends KeyAdapter { public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if(key == KeyEvent.VK_Z) { msg = "pressed z"; if((board_x - 40) >= 0)//to prevent overflow from left side from left = true; else left = false;//nothing } if(key == KeyEvent.VK_X) { msg = "pressed x"; if((board_x + 150) <= width)//to prevent overflow form right side of game area right = true; else right = false;//nothing } if(key == KeyEvent.VK_ENTER){ start(); contin = true; tryagain = false; pause = false; } if(key == KeyEvent.VK_SPACE){ if(!pause){ contin = false; pause = true; tryagain = false; }else{ contin = true; pause = false; tryagain = false; } } if(key == KeyEvent.VK_ESCAPE){ running = false; contin = false; pause = false; tryagain = false; } } } }
import javax.swing.*; public class pinpong extends JApplet { /** * @param args */ public pinpong() { this.setContentPane(new Board()); } public static void main(String[] args) { // TODO Auto-generated method stub JFrame window = new JFrame(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(586, 630); window.setLocationRelativeTo(null); window.setTitle("pinpong"); window.setContentPane(new Board()); window.setResizable(false); window.setVisible(true); } }
Subscribe to:
Posts (Atom)