carookee - group communication for you
Home / JavaForum / Swing + AWT
Infos   |   Features   |   Gold-Edition   |   Kundenservice   
java
  Übersicht
  Forum
Beginner
Java allgemein
JDBC
JNI
Networking
Online-Ressourcen
Swing + AWT
XML
Meckerecke
  Mitglieder
LOGIN





· Passwort vergessen
· Kostenlos anmelden
  Information
  Demo
  Features
  Im Vergleich
  Anmeldung
SUCHE
Beiträge, Foren oder Verfasser finden:
  Kundenservice
  Impressum
  Datenschutz
  AGB
Status
5.509 User online
1 User eingeloggt
 

Beiträge
   Antworten     Neuer Beitrag    

Beitrag 27 von 1460 (2%) |<   <   >   >|

Autor Juli747
Datum 16.01.06, 15:14
Betreff eventhandling / Problemmeldung Bitte schaut euch den Code an


Hallo, habe fehlermeldung bei diesem Listing das ich nicht wegkriege. Bin Fuer jede Hilfe dankbar. Auf knopfdruck soll ein text codiert werden .bei der methode action gibt netbeans eine Nullpointerexception an.


/*
* MorseApp.java
*
* Created on 15. Januar 2006, 02:24
*
  • To change this template, choose Tools | Options and locate the template under
  • the Source Creation and Management node. Right-click the template and choose
  • Open. You can then make changes to the template in the Source Editor.
*/


package abi;

import java.awt.event.*;
import java.io.*;
import java.util.regex.*;
import java.awt.*;
import java.applet.*;
import sun.audio.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.util.StringTokenizer;



/**
*
* @author karaali
*/

public class MorseApp extends Frame{
    
    class WAdapter extends WindowAdapter
    {
        public void windowClosing( WindowEvent e )
        {
            System.exit(0);
        }
    }
    
    private Button playButton;
    private Button transButton;
    private Button DateiImport;
    private TextArea input;
    private TextArea output;
    private String morse;
    private String text;
    private int wpm = 30;
    private Choice wpmChoice;
    byte [] sample = null;
    AudioData playable = null;
    int sPos;
    int timeunit;
    
    final static String CODES[][]={
        {"A",".-"},
        {"B","-..."},
        {"C","-.-."},
        {"D","-.."},
        {"E","."},
        {"F","..-."},
        {"G","--."},
        {"H","...."},
        {"I",".."},
        {"J",".---"},
        {"K","-.-"},
        {"L",".-.."},
        {"M","--"},
        {"N","-."},
        {"O","---"},
        {"P",".--."},
        {"Q","--.-"},
        {"R",".-."},
        {"S","..."},
        {"T","-"},
        {"U","..-"},
        {"V","...-"},
        {"W",".--"},
        {"X","-..-"},
        {"Y","-.--"},
        {"Z","--.."},
        {"0","----"},
        {"1",".----"},
        {"2","..---"},
        {"3","...--"},
        {"4","....-"},
        {"5","....."},
        {"6","-...."},
        {"7","--..."},
        {"8","---.."},
        {"9","----."}
        
    };
    
    
    public MorseApp () {
        
        WAdapter wa = new WAdapter();
        addWindowListener( wa );
        
        Font butFont = new Font("Helvetica", Font.PLAIN, 10);
        
        GridBagLayout gbag = new GridBagLayout();
        setLayout(gbag);
        GridBagConstraints con = new GridBagConstraints();
        
        
        Label l4 = new Label("Morse Code Übersetzer", Label.CENTER);
        l4.setFont(new Font("Helvetica", Font.BOLD, 16));
        con.gridx = 0;
        con.gridy = 0;
        con.gridwidth = 6;
        con.anchor = GridBagConstraints.CENTER;
        //con.fill = GridBagConstraints.HORIZONTAL;
        con.weightx = 1.0;
        gbag.setConstraints(l4, con);
        add(l4);
        
        Label l2 = new Label("TEXTEINGABE", Label.CENTER);
        l2.setFont(new Font("Helvetica", Font.BOLD, 12));
        con.gridx = 0;
        con.gridy = 1;
        gbag.setConstraints(l2, con);
        add(l2);
        
        TextArea input = new TextArea("",5, 90,
        TextArea.SCROLLBARS_VERTICAL_ONLY);
        input.setEditable(true);
        input.setFont(new Font("Courier", Font.PLAIN, 10));
        con.gridx = 0;
        con.gridy = 2;
        gbag.setConstraints(input, con);
        add(input);
        
        
        Label l3 = new Label("MORSECODE", Label.CENTER);
        l3.setFont(new Font("Helvetica", Font.BOLD, 12));
        con.gridx = 0;
        con.gridy = 3;
        gbag.setConstraints(l3, con);
        add(l3);
        
        TextArea output = new TextArea("",5, 90,
        TextArea.SCROLLBARS_VERTICAL_ONLY);
        output.setEditable(true);
        output.setFont(new Font("Courier", Font.PLAIN, 10));
        con.gridx = 0;
        con.gridy = 4;
        gbag.setConstraints(output, con);
        add(output);
        
        transButton = new Button("Übersetze");
        transButton.setFont(butFont);
        transButton.setForeground(Color.black);
        transButton.setBackground(Color.lightGray);
        con.gridx = 0;
        con.gridy = 5;
        con.gridwidth = 3;
        con.ipadx = 2;
        con.ipady = 2;
        con.insets = new Insets(6,0,0,0);
        gbag.setConstraints(transButton, con);
        add(transButton);
        
        
        
        
        Panel p1 = new Panel();
        GridBagLayout gbag1 = new GridBagLayout();
        p1.setLayout(gbag1);
        
        con.gridx = 3;
        con.gridy = 5;
        con.gridwidth = 3;
        con.ipadx = 0;
        con.ipady = 0;
        gbag.setConstraints(p1, con);
        add(p1);
        
        //sub panel
        playButton = new Button("Play");
        playButton.setFont(butFont);
        playButton.setForeground(Color.black);
        playButton.setBackground(Color.lightGray);
        con.gridx = 0;
        con.gridy = 0;
        con.gridwidth = 1;
        con.ipadx = 2;
        con.ipady = 2;
        con.insets = new Insets(0,0,0,0);
        gbag1.setConstraints(playButton, con);
        p1.add(playButton);
        
        p1.add(playButton);Label l5 = new Label("mit");
        l5.setFont(butFont);
        con.gridx = 1;
        con.gridy = 0;
        con.ipadx = 0;
        con.ipady = 0;
        gbag1.setConstraints(l5, con);
        p1.add(l5);
        
        wpmChoice = new Choice();
        wpmChoice.setFont(butFont);
        wpmChoice.addItem("30");
        wpmChoice.addItem("40");
        wpmChoice.addItem("50");
        wpmChoice.addItem("60");
        wpmChoice.addItem("70");
        wpmChoice.addItem("80");
        wpmChoice.addItem("90");
        wpmChoice.addItem("100");
        wpmChoice.addItem("110");
        wpmChoice.addItem("120");
        wpmChoice.addItem("130");
        wpmChoice.addItem("140");
        wpmChoice.addItem("150");
        wpmChoice.addItem("160");
        wpmChoice.addItem("170");
        wpmChoice.addItem("180");
        wpmChoice.addItem("190");
        wpmChoice.addItem("200");
        wpmChoice.select(3);
        wpmChoice.setForeground(Color.black);
        wpmChoice.setBackground(Color.lightGray);
        con.gridx = 2;
        con.gridy = 0;
        gbag1.setConstraints(wpmChoice, con);
        p1.add(wpmChoice);
        
        Label l1 = new Label("WPM");
        l1.setFont(butFont);
        con.gridx = 3;
        con.gridy = 0;
        gbag1.setConstraints(l1, con);
        p1.add(l1);
        
        DateiImport = new Button("DateiImport");
        playButton.setFont(butFont);
        playButton.setForeground(Color.black);
        playButton.setBackground(Color.lightGray);
        con.gridx = 0;
        con.gridy = 6;
        con.gridwidth = 6;
        con.anchor = GridBagConstraints.CENTER;
        gbag.setConstraints(DateiImport, con);
        add(DateiImport);
        
        
    }
    
    public boolean action(Event event, Object arg) {
        if (event.target == transButton) {
            if (isText(input.getText())) {
                text = input.getText();
                morse = getWord(text);
                output.setText(morse);
                } else {
                morse = input.getText();
                text = getMorseCode(morse);
                output.setText(text);
            }
            return true;
        }
        if (event.target == playButton) {
            //Applet.this.showStatus(status);
            if (isText(input.getText())) {
                text = input.getText();
                morse = getWord(text);
                output.setText(morse);
                makeSample(morse);
                } else {
                morse = input.getText();
                text = getMorseCode(morse);
                output.setText(text);
                makeSample(input.getText()); //using the processed morse
            }
            playSample();
            return true;
        }
        else if (event.target == wpmChoice) {
            if (arg.equals("30")) wpm = 30;
            else if (arg.equals("40")) wpm = 40;
            else if (arg.equals("50")) wpm = 50;
            else if (arg.equals("60")) wpm = 60;
            else if (arg.equals("70")) wpm = 70;
            else if (arg.equals("80")) wpm = 80;
            else if (arg.equals("90")) wpm = 90;
            else if (arg.equals("100")) wpm = 100;
            else if (arg.equals("110")) wpm = 110;
            else if (arg.equals("120")) wpm = 120;
            else if (arg.equals("130")) wpm = 130;
            else if (arg.equals("140")) wpm = 140;
            else if (arg.equals("150")) wpm = 150;
            else if (arg.equals("160")) wpm = 160;
            else if (arg.equals("170")) wpm = 170;
            else if (arg.equals("180")) wpm = 180;
            else if (arg.equals("190")) wpm = 190;
            else if (arg.equals("200")) wpm = 200;
            //wpm = (event.target.getSelectedIndex() + 1) * 5;
            return true;
        }
        else return super.action(event, arg);
    }
    /********* TEXT TEXT TEXT TEXT TEXT TEXT **/
    
    public boolean isText (String sText) {
        boolean matches = false;
        boolean matches1 = true ;
        String pattern = ("^[._/ \\|-]*$");
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(sText);
        
        if (m.find()) {
            System.out.println("MorseCode ");
            return matches;
            
            }else {
        System.out.println("Text");}
        return matches1;
    }
    
    
    public String getMorseCode(String text)
    {
        StringBuffer codeWord = new StringBuffer();
        String foo= text.toUpperCase();
        StringTokenizer st = new StringTokenizer(foo," ", true);
        while (st.hasMoreTokens())
        {
            String tStr= st.nextToken();
            if (tStr.compareTo(" ")==0)
            {
                codeWord.append(" ");
            }
            else
            {
                String z=null;
                for(int x=0; x<tStr.length(); x++)
                {
                    z = tStr.substring(x,x+1);
                    int row=0;
                    for(; (row<CODES.length)&&(z.compareTo(CODES[row][0])!=0); row++);
                    if (row==CODES.length) {System.out.println("Cannot find Code for : " +z); }
                    else {codeWord.append(CODES[row][1]+" "); }
                }
                
                z=null;
            }
        }
        
        return codeWord.toString().trim();
    }
    
    public String getWord(String t)
    {
        StringBuffer codeWord = new StringBuffer();
        StringTokenizer st = new StringTokenizer(t," ", true);
        while (st.hasMoreTokens())
        {
            String tStr= st.nextToken();
            
            if (tStr.compareTo(" ")==0)
            {
                //find the next token , if it is also space read the next two tokens
                // else append a space and make the curToken equal to next token
                String nextTok = st.nextToken();
                if (" ".compareTo(nextTok)==0) { codeWord.append(" ");st.nextToken();tStr=null;tStr=st.nextToken();}
                else {tStr=null; tStr=nextTok; }
            }
            if (tStr.compareTo(" ")!=0)
            {
                
                
                int row=0,col=0;
                for(row=0; row<CODES.length; row++)
                {
                    for(col=0;col<CODES[0].length;col++)
                    {
                        if (tStr.compareTo(CODES[row][col])==0)
                        {
                            
                            codeWord.append(CODES[row][0]+" ");
                        }
                    }
                }
                
                
                tStr=null;
            }
            
        }
        
        return codeWord.toString();
        
    }
    
    /********** SOUND SOUND SOUND SOUND SOUND */
    
    public void playSample() {
        AudioDataStream stream = new AudioDataStream(playable);
        AudioPlayer.player.start(stream);
    }
    
    public void makeSample(String morse) {
        char c;
        int dits=0, dahs=0, spcs=0, slashes=0;
        int dahLen = 3;
        int pause1 = 1;
        int pause2 = 3;
        int pause3 = 7;
        int wordLen = 50;
        int ditLen = (int)1000*60/(wpm*wordLen);
        //thousandths of a sec per dit, or 8-byte frames per dit
        
        for (int i = 0; i < morse.length(); i++) {
            c = morse.charAt(i);
            //status += c;
            switch (c) {
                case '.':
                dits++;
                break;
                case '-':
                dahs++;
                break;
                case ' ':
                spcs++;
                break;
                case '/':
                slashes++;
                break;
                //need default
            }
        }
        //status += " "+dits+" "+dahs+" "+spcs+" "+slashes;
        int length = ditLen*8*(dits*(1+pause1)+dahs*(dahLen+pause1)+spcs*(pause2-pause1)+slashes*(pause3-pause1));
        
        sample = new byte[length];
        
        int sPos = 0;
        for (int i = 0; i < morse.length(); i++) {
            c = morse.charAt(i);
            switch (c) {
                case '.':
                addSound(1, sPos, ditLen);
                sPos += (1+pause1)*ditLen*8;
                break;
                case '-':
                addSound(dahLen, sPos, ditLen);
                sPos += (dahLen+pause1)*ditLen*8;
                break;
                case ' ':
                sPos += (pause2-pause1)*ditLen*8;
                break;
                case '/':
                sPos += (pause3-pause1)*ditLen*8;
                break;
            }
        }
        playable = new AudioData(sample);
    }
    
    public void addSound(int units, int sPos, int ditLen) {
        for (int i = 0; i < units*ditLen; i++) {
            sample[sPos+i*8] = (byte)0xA7;
            sample[sPos+i*8+1] = (byte)0x81;
            sample[sPos+i*8+2] = (byte)0xA7;
            sample[sPos+i*8+3] = (byte)0;
            sample[sPos+i*8+4] = (byte)0x59;
            sample[sPos+i*8+5] = (byte)0x7F;
            sample[sPos+i*8+6] = (byte)0x59;
            sample[sPos+i*8+7] = (byte)0;
        }
    }
    
    
    
    
    public static void main(String[] args) {
        MorseApp test = new MorseApp();
        test.setBackground(Color.lightGray);
        test.setLocation(100,100);
        test.setSize(800, 500);
        test.setVisible(true);
        
        
        // TODO code application logic here
    }}




Diskussionsverlauf:
eventhandling / Problemmeldung Bitte schaut euch den Code an
    Re: eventhandling / Problemmeldung Bitte schaut euch den Code an

 Auf diesen Beitrag antworten
 Neuen Beitrag verfassen


|<   <   >   >|

                                                                                                                                                                                                                           

Impressum  · Datenschutz  · AGB  · Infos  · Presse
Ein modernes Forum: teamturn.com