JFrames in Applets?

hi im trying to put Jframes in an applet.
this is the part i dont get.
public class Scores extends Applet
but i need to extend JFrames aswell which it wont allow me to do.
any help or suggestions would be most Appreciated

1) You're probably better of extendind JApplet, rather than the basic AWT Applet object.
2) JFrames represent independant windows on the client machine. Applet windows are not independant, they are controlled by the browser so your main window can't be both (though an applet can open JFrame windows, if the browser doesn't block them).
3) If you want to put child windows in your applet window, that's fine, but they aren't going to JFrame based. You should be able to set up a JDeskPane, for example, inside your applet window and put JInternalFrame windows in that. If the child windows are fixed then use JPanel objects.

Similar Messages

  • From Jframe to applet

    Hi everyone.I need to convert graphics from JFrame to Japplet. I am using a class which contains the main method the main method runs the constructor of my class and the constructor creates a new object which is a class that extends to Jframe.I need to make this program to run in an Internet Explorer.Any ideas Please help me guys It is impossible to change the whole program. Thanx in advance.

    Your JFrame extended class functionality should be put into the JApplet's
    functionality. Probably mostly in the init method.
    You could launch JFrames from within an Applet. But I'm not sure why
    you wouldn't just migrate everything your JFrame does.

  • Is correct? Application (JFrame) to Applet (JApplet)

    I need to convert a java application (JFrame) into an applet (JApplet), and I have seen a few "big steps":
    1�: make the class extends to JApplet instead of a JFrame
    2�: To replace the construction method by init ()
    3�: To comment the main method
    Is correct?, because I have some doubts that later I�ll explain
    Thanks

    I think in the init method (in a JApplet) I cannot call the super method. but my problem is how to change this? if in others files (in Files.java) I have calls to super metod, because thus it constructs a dialog box, asking for a file.
    In file Files.java I have a class called "Abort" (extends to JDialog) in whitch:
    /* class Files.java */
    class Abort extends JDialog
              boolean abort = true;
              JTextArea mensage = null;
              JButton acept = new JButton("Acept");
              JButton cancel = new JButton("Cancel");
              Abort(String nanemFile)
                   super(MainClassFile.mainWindow, " Attention!", true);
    /* clas Files.java */Please help

  • Open an applet inside a JFrame?

    Hi all!
    I know this subject is kind of covered in previous topics, but my situation here is a little different.
    I have a TRENDnet TV-IP100 Internet Camera that uses a Java-Applet to show in a browser window the video it captures, using an xview.class file.
    I built a swing application that extends JFrame and I want to open in a JPanel, inside the JFrame, the applet-class that my camera uses.
    My problem is that I can't take and decompile that class file from the camera, in order to compile from the scratch the Applet; some could say illegal.
    So, I wonder if there is a way to open the class file inside my JFrame application. Also, I want to know if I can pass argumets in the JFrame file, like PARAM, NAME, VALUE, as there are important to start the applet.
    Thank you all, in advance.
    John_Astralidis.

    I have not tried this but if the Applet or JApplet class is to be used only as far as a component in a Container (JFrame.getContentPane() returns a Container) I see no problem with explicitly casting into a Panel or even a Container
    Panel p=(Panel)theAppletInstance;
    this.getContentPane().add(p);//this being the JFrame;There be problem of which I have not considered as the applet class is 'different' to many classes.
    Bamkin
    Message was edited by:
    bamkin-ov-lesta
    Container and Panel examples were used as Applet is derived from Panel, which is derived from Containter

  • Take out multiple values from a db and handle them in an applet.

    I have a db with tables. I want to take out several values from them and make them visible in a JFrame (or applet).
    I dont know how to do. I have an app that connects to the db and selects the values and puts the Strings in a Vector. But now then?
    I have looked at examples that retrieve Strings but not vectors, isn't that possible?

    Hi,
    On initialization of your frame you can loop over your vector and display them.
    Now the problem is in what type of component do you want to put your data.
    I can give you an example.
    What you can do is create a String which will contain all the data from
    your vector and set that string to the component.
    Example:
    Vector data;
    String result;
    for(int i=0; i < data.size(); i++) {
    result += (String)data.elementAt(i) + "\n";
    Now you need to put the string into a component which is easy done.
    Let take a textarea component.
    JTextArea txaTest = new JTextArea();
    txtTest.setText(result);
    Now all the data will be shown in the textarea component on your frame.

  • Putting JApplet into JFrame

    Hi, does anyone know how to combine a JApplet into a JFrame conponent? I've tried:
    public static void main(String args[]) {
    final JFrame frame=new JFrame();
    JApplet applet=new JApplet(); // JApplet is the class name
    applet.init();
    frame.setContentPane(applet.getContentPane());
    frame.setBounds(20, 20, background.getWidth(null)+2, 702);
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter() {
    public void winodwClosing(WindowEvent e) {
    System.exit(0);
    but it's not working. I'm using Appletviewer. Pllease help if u know the answer. Thx!

    instead of:
    frame.setContentPane(applet.getContentPane());you need to do:
    frame.getContentPane().add(applet);

  • Start applet from main without html or appletviewer

    Hello,
    I have an applet that I would like to function outside of a web page or the appletviewer.
    How would I open an instance of the applet in a main function of a class?
    My applet class is entitled ViewData.
    Thank you in advance,
    Chris

    import java.awt.*;
    import javax.swing.*;
    public class ViewData extends JApplet {
      public void init() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.red);
        panel.add(new JLabel("ViewData", SwingConstants.CENTER));
        getContentPane().add(panel);
      public static void main(String[] args) {
        JFrame frame = new JFrame("ViewData Applet");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JApplet applet = new ViewData();
        frame.getContentPane().add(applet);
        frame.setSize(400, 250);
        frame.setLocation(375, 100);
        applet.init();
        applet.start();
        frame.setVisible(true);
    }

  • Applet.show()

    Note: C:\Documents and Settings\2z7\TestCell4\src\Combox.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    JFrame appletFrame = new JFrame("My Applet");
        Applet theApplet = new Combox();
        appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        appletFrame.setSize(800,800);
        appletFrame.add(theApplet, "Center");  // applet fills
                                               // frame
        /* Now start invoking applet methods. */
        theApplet.init();
        appletFrame.show();  // bring frame and initializedHow to change it?
    Thanks

    read the API docs for JFrame...
    show() has been replaced by setVisible(boolean)

  • Difference between JFrame and JApplet

    i would like to create a GUI using java. as what i know, either i can use the normal applet or java swing. may i know what is the difference between 2 of them??
    thanks

    Hello thanku,
    You have asked two completely different questions:
    1) What is the difference between JFrame and JApplet?
    2) What is the difference between Applet and JApplet?
    A Java applet is a program that adheres to a set of conventions that allows it to run within a Java-compatible browser. To create a Java applet you may either use Applet or JApplet. If you are not running with in a browser, that is, if you are creating an application, you need to use a Frame or JFrame.
    Applet is the old way of creating a Java applet. It is better to use JApplet, since JApplet has all the functionality of Applet and more. Please note that you should not mix AWT components with Swing components.
    -Merwyn,
    Developer Technical Support,
    http://www.sun.com/developers/support.

  • Problem with threads and simulation: please help

    please help me figure this out..
    i have something like this:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class DrawShapes extends JApplet{
         private JButton choices[];
         private String names[]={"line", "square", "oval"};
         private JPanel buttonPanel;
         private DrawPanel drawingArea;
         private int width=300, height=200;
         public void init(){
              drawingArea=new DrawPanel(width, height);
              choices=new JButton[names.length];
              buttonPanel=new JPanel();
              buttonPanel.setLayout(new GridLayout(1, choices.length));
              ButtonHandler handler=new ButtonHandler();
              for(int i=0; i<choices.length; i++){
                   choices=new JButton(names[i]);
                   buttonPanel.add(choices[i]);
                   choices[i].addActionListener(handler);
              Container c=getContentPane();
              c.add(buttonPanel, BorderLayout.NORTH);
              c.add(drawingArea, BorderLayout.CENTER);
         }//end init
         public void setWidth(int w){
              width=(w>=0 ? w : 300);
         public void setHeight(int h){
              height=(h>=0 ? h : 200);
         /*public static void main(String args[]){
              int width, height;
              if(args.length!=2){
                   height=200; width=300;
              else{
                        width=Integer.parseInt(args[0]);
                        height=Integer.parseInt(args[1]);
              JFrame appWindow=new JFrame("An applet running as an application");
              appWindow.addWindowListener(
                   new WindowAdapter(){
                        public void windowClosing(WindowEvent e){
                             System.exit(0);
              DrawShapes appObj=new DrawShapes();
              appObj.setWidth(width);
              appObj.setHeight(height);
              appObj.init();          
              appObj.start();
              appWindow.getContentPane().add(appObj);
              appWindow.setSize(width, height);
              appWindow.show();
         }//end main*/
         private class ButtonHandler implements ActionListener{
              public void actionPerformed(ActionEvent e){
                   for(int i=0; i<choices.length; i++){
                        if(e.getSource()==choices[i]){
                             drawingArea.setCurrentChoice(i);
                             break;
    }//end class DrawShapes
    class DrawPanel extends JPanel{
         private int currentChoice=-1;
         private int width=100, height=100;
         public DrawPanel(int w, int h){
              width=(w>=0 ? w : 100);
              height=(h>=0 ? h : 100);
         public void paintComponent(Graphics g){
              super.paintComponent(g);
              switch(currentChoice){
                   case 0:     g.drawLine(randomX(), randomY(), randomX(), randomY());
                             break;
                   case 1: g.drawRect(randomX(), randomY(), randomX(), randomY());
                             break;
                   case 2: g.drawOval(randomX(), randomY(), randomX(), randomY());
                             break;
         public void setCurrentChoice(int c){
              currentChoice=c;
              repaint();          
         private int randomX(){
              return (int) (Math.random()*width);
         private int randomY(){
              return (int) (Math.random()*height);
    }//end class drawPanel
    That one's from a book. I used that code to start with my applet. Mine calls different merthod from the switch cases. Say I have:
    case 0: drawStart(g); break;
    public void drawStart(Graphics g){
      /* something here */
    drawMain(g);
    public void drawMain(graphics g){
    g.drawString("test", x, y);
    //here's where i'm trying to pause
    //i've tried placing Thread.sleep between these lines
    g.drawLine(x, y, a, b);
    //Thread.sleep here
    g.drawRect(x, y, 50, 70);
    }I also need to put delays between method calls but I need to synchronize them. Am I doing it all wrong? The application pauses or sleeps but afterwards, it still drew everything all at once. Thanks a lot!

    It is. Sorry about that. Just answer any if you want to. I'd appreciate your help. Sorry again if it caused you anything or whatever. .n_n.

  • Display incorrctly!

    Hi, in my code. I want to plot a 2D object, a JSlider and two comb box.
    But the 2D object can't displayed correctly, can you look at my code?
    Please don't provide the link of sun swing tutorial, I have studied many times, but no achievement.
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.applet.Applet;
    public class Combox extends JApplet{
         private JSlider dJSlider;
            private JComboBox comb;
            private JComboBox alfa;
         private Container panel;
         public void init()
              panel = getContentPane();
                    drawing = new Drawing(a,id,scale);
                    JPanel p2 = new JPanel();
                    panel.add(p2);
                    p2.add(drawing);// this drawing can't be plotted,only a small white square is showed.
                    p2.add(new getSlider(),BorderLayout.NORTH);
                    p2.add(new getComboPanel(),BorderLayout.EAST);
         class  getSlider extends JPanel {
                public getSlider(){
              add(dJSlider);
         class getComboPanel extends JPanel {
                 public getComboPanel(){
              comb = new JComboBox(e);
                    layout = new GridBagLayout();
                    setLayout(layout);
              alfa = new JComboBox(af);
               add(comb);
                     add(alfa);
         class Drawing extends JPanel
         //     plot 2D
         public static void main(String[] args)
              JFrame a = new JFrame("show");
                    Applet b = new Combox();
         }But if the code becomes
    public void init()
              panel = getContentPane();
                    drawing = new Drawing(a,id,scale);
                    panel.add(drawing);
                    panel.add(new getSlider(),BorderLayout.NORTH);
                    panel.add(new getComboPanel(),BorderLayout.EAST);
         }the plot is right.why?
    Thanks

    drawing will plot a ploygon and fill a ploygon.(contains two methods)
    drawPolygon(g2d); 
                drawSides(g2d);pets1234, you have traced my poster so long.
    here is the Drawing class.
    class Drawing extends JPanel
        final int TRANSITION = 100; // Transition x,y coordinates of cells.
        private double x1, y1, x2, y2; // to plot lines from (x1,y1) to (x2,y2)
        private double xCells[][]; // Every cell's points's x coordinates array
        private double yCells[][]; // Every cell's points's y coordinates array
        private double rgb[][]; // Color red, green, blue for each point in each cell
        private double red[][];
        private double green[][];
        private double blue[][];
            private double lineWidth;
        private int sideId;
        private int sidesTotalNumber; // Total sides number for all cells
        private int rgbLength; // Total rgb color group number for all cells at each point
        private int cellsPointsNumber[]; // point numbers of each cell.(each cell has how many points)
        private int numberofCells;
        private int numberofsets;
        int a,id;
        double scale;
        Drawing(int a, int id, double scale)
        {            this.a = a;
                     this.id = id;
                     this.scale = scale;
        public void setscale(double scale)
             this.scale = scale;
             repaint();
        public void setid(int id)
            this.id = id;
            repaint();
        public void seta(int a)
            this.a = a;
            repaint();
        public void paintComponent(Graphics g)
                super.paintComponent( g ); // call superclass's paint method
                this.setBackground( Color.WHITE );
                Graphics2D g2d = (Graphics2D)g; // cast g to Graphics 2D
                getDrawingData();
                if (a==0)
                    drawPolygon(g2d); 
                drawSides(g2d);
       // end method paintComponent
          public void getDrawingData()
              TestCell p = new TestCell();
              p.cellData();
              p.rgbs();
              xCells = p.getxCells();
              yCells = p.getyCells();
              sidesTotalNumber = p.getsidesTotalNumber();
              red = p.getred();
              green = p.getgreen();
              blue  = p.getblue();
              rgbLength = p.getrgbLength();
              cellsPointsNumber = p.getcellsPointsNumber();//array: each cell has how many points
              numberofCells = p.getnumberofCells();
              numberofsets  = p.getnumberofsets();
                for(int i = 0; i < numberofCells; i++)
                  for(int j = 0; j<cellsPointsNumber; j++)
    xCells[i][j] = (xCells[i][j])*scale+TRANSITION;
    yCells[i][j] = (yCells[i][j])*scale+TRANSITION;
    public void drawSides(Graphics2D g2d)
    g2d.setColor(Color.GRAY);
    g2d.setStroke(new BasicStroke((float)lineWidth));
    for (int i = 0; i<numberofCells; i++)
    for (int j = 0; j<cellsPointsNumber[i]-1; j++)
    x1 = xCells[i][j];
    y1 = yCells[i][j];
    x2 = xCells[i][j+1];
    y2 = yCells[i][j+1];
    Line2D line = new Line2D.Double(x1,y1,x2,y2);
    g2d.draw(line);// draw Sidess here
    public void drawPolygon(Graphics2D g2d)
    for (int i = 0; i<numberofCells; i++)
    Polygon poly = new Polygon();
    float r = (float)red[i][id];
    // red for ith cell,id is from 0 to numberofsets-1
    // if id=0, means "Mixture"
    float g = (float)green[i][id]; // green for ith cell
    float b = (float)blue[i][id]; // blue for ith cell
    List<Integer> xList = new ArrayList<Integer>();
    List<Integer> yList = new ArrayList<Integer>();
    for (int j = 0; j<cellsPointsNumber[i]; j++)
    xList.add((int)(xCells[i][j]));
    yList.add((int)(yCells[i][j]));
    Integer[] x = xList.toArray(new Integer[xList.size()]);
    Integer[] y = yList.toArray(new Integer[yList.size()]);
    for (int s = 0; s<xList.size(); s++)
    poly.addPoint(x[s],y[s]);
    g2d.setColor(new Color(r, g, b));// transparent drawing
    g2d.fillPolygon(poly);

  • URGENT Help With Scientific Calculator!

    Hi everybody,
    I designed a calculator, and I need help with the rest of the actions. I know I need to use the different Math methods, but I tried tried that and it didn't work. Also, it needs to work as an applet and application, and in the applet, the buttons don't appear in order, how can I fix that?
    I will really appreciate your help with this program, I can't get it to work and I'm frustrated, I need to finish this for next Tuesday 16th. Please e-mail me at [email protected].
    Below is the code for the calcualtor.
    Thanks a lot!
    -Maria
    // calculator
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    public class calculator extends JApplet implements
    ActionListener
      private JButton one, two, three, four, five, six, seven,
      eight, nine, zero, dec, eq, plus, minus, mult, div, clear,
      mem, mrc, sin, cos, tan, asin, acos, atan, x2, sqrt, exp, pi, percent;
      private JLabel output, blank;
      private Container container;
      private String operation;
      private double number1, number2, result;
      private boolean clear = false;
      //GUI
      public void init()
        container = getContentPane();
        //Title
        //super("Calculator");
        JPanel container = new JPanel();     
        container.setLayout( new FlowLayout( FlowLayout.CENTER
        output = new JLabel("");     
        output.setBorder(new MatteBorder(2,2,2,2,Color.gray));
        output.setPreferredSize(new Dimension(1,26));     
        getContentPane().setBackground(Color.white);     
        getContentPane().add( "North",output );     
        getContentPane().add( "Center",container );
        //blank
        blank = new JLabel( "                    " );
        container.add( blank );
        //clear
        clear = new JButton( "CE" );
        clear.addActionListener(this);
        container.add( clear );
        //seven
        seven = new JButton( "7" );
        seven.addActionListener(this);
        container.add( seven );
        //eight
        eight = new JButton( "8" );
        eight.addActionListener(this);
        container.add( eight );
        //nine
        nine = new JButton( "9" );
        nine.addActionListener(this);
        container.add( nine );
        //div
        div = new JButton( "/" );
        div.addActionListener(this);
        container.add( div );
        //four
        four = new JButton( "4" );
        four.addActionListener(this);
        container.add( four );
        //five
        five = new JButton( "5" );
        five.addActionListener(this);
        container.add( five );
        //six
        six = new JButton( "6" );
        six.addActionListener(this);
        container.add( six );
        //mult
        mult = new JButton( "*" );
        mult.addActionListener(this);
        container.add( mult );
        //one
        one = new JButton( "1" );
        one.addActionListener(this);
        container.add( one );
        //two
        two = new JButton( "2" );
        two.addActionListener(this);
        container.add( two );
        //three
        three = new JButton( "3" );
        three.addActionListener(this);
        container.add( three );
        //minus
        minus = new JButton( "-" );
        minus.addActionListener(this);
        container.add( minus );
        //zero
        zero = new JButton( "0" );
        zero.addActionListener(this);
        container.add( zero );
        //dec
        dec = new JButton( "." );
        dec.addActionListener(this);
        container.add( dec );
        //plus
        plus = new JButton( "+" );
        plus.addActionListener(this);
        container.add( plus );
        //mem
        mem = new JButton( "MEM" );
        mem.addActionListener(this);
        container.add( mem );   
        //mrc
        mrc = new JButton( "MRC" );
        mrc.addActionListener(this);
        container.add( mrc );
        //sin
        sin = new JButton( "SIN" );
        sin.addActionListener(this);
        container.add( sin );
        //cos
        cos = new JButton( "COS" );
        cos.addActionListener(this);
        container.add( cos );
        //tan
        tan = new JButton( "TAN" );
        tan.addActionListener(this);
        container.add( tan );
        //asin
        asin = new JButton( "ASIN" );
        asin.addActionListener(this);
        container.add( asin );
        //acos
        acos = new JButton( "ACOS" );
        cos.addActionListener(this);
        container.add( cos );
        //atan
        atan = new JButton( "ATAN" );
        atan.addActionListener(this);
        container.add( atan );
        //x2
        x2 = new JButton( "X2" );
        x2.addActionListener(this);
        container.add( x2 );
        //sqrt
        sqrt = new JButton( "SQRT" );
        sqrt.addActionListener(this);
        container.add( sqrt );
        //exp
        exp = new JButton( "EXP" );
        exp.addActionListener(this);
        container.add( exp );
        //pi
        pi = new JButton( "PI" );
        pi.addActionListener(this);
        container.add( pi );
        //percent
        percent = new JButton( "%" );
        percent.addActionListener(this);
        container.add( percent );
        //eq
        eq = new JButton( "=" );
        eq.addActionListener(this);
        container.add( eq );
        //Set size and visible
        setSize( 190, 285 );
        setVisible( true );
    public static void main(String args[]){
        //execute applet as application
         //applet's window
         JFrame applicationWindow = new JFrame("calculator");
    applicationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         //applet instance
         calculator appletObject = new calculator();
         //init and start methods
         appletObject.init();
         appletObject.start();
      } // end main
      public void actionPerformed(ActionEvent ae)
        JButton but = ( JButton )ae.getSource();     
        //dec action
        if( but.getText() == "." )
          //if dec is pressed, first check to make shure there
    is not already a decimal
          String temp = output.getText();
          if( temp.indexOf( '.' ) == -1 )
            output.setText( output.getText() + but.getText() );
        //clear action
        else if( but.getText() == "CE" )
          output.setText( "" );
          operation = "";
          number1 = 0.0;
          number2 = 0.0;
        //plus action
        else if( but.getText() == "+" )
          operation = "+";
          number1 = Double.parseDouble( output.getText() );
          clear = true;
          //output.setText( "" );
        //minus action
        else if( but.getText() == "-" )
          operation = "-";
          number1 = Double.parseDouble( output.getText() );
          clear = true;
          //output.setText( "" );
        //mult action
        else if( but.getText() == "*" )
          operation = "*";
          number1 = Double.parseDouble( output.getText() );
          clear = true;
          //output.setText( "" );
        //div action
        else if( but.getText() == "/" )
          operation = "/";
          number1 = Double.parseDouble( output.getText() );
          clear = true;
          //output.setText( "" );
        //eq action
        else if( but.getText() == "=" )
          number2 = Double.parseDouble( output.getText() );
          if( operation == "+" )
            result = number1 + number2;
          else if( operation == "-" )
            result = number1 - number2;
          else if( operation == "*" )
            result = number1 * number2;
          else if( operation == "/" )
            result = number1 / number2;       
          //output result
          output.setText( String.valueOf( result ) );
          clear = true;
          operation = "";
        //default action
        else
          if( clear == true )
            output.setText( "" );
            clear = false;
          output.setText( output.getText() + but.getText() );
    }

    Multiple post:
    http://forum.java.sun.com/thread.jsp?forum=31&thread=474370&tstart=0&trange=30

  • Calling a method in a subclass?

    Hi, anyone an idea why method getInit() (in Applet class) isn't recognised in class Map?
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import java.applet.*;
    import java.util.*;
    import javax.swing.*;
    public class Main extends Applet {
         public boolean initialized =true;
         Map map;
         World world = new World();
         public void init()
              Map = new map("map.jpg",0,0,world,this);
         public boolean getInit()
         { return initialized; }
    //class Map: method getInit() is not recognised???
    public class Map extends JPanel implements MouseListener
         private Image image;
         private World world;
    private int x,y;     
         private JPanel p = new JPanel();
         private JFrame frame = new JFrame();
         private Applet $applet;
         public Map(String name,int xp,int yp, World w, Applet a)
              $applet=a;
              x=xp;
              y=yp;
              world = w;
              MediaTracker media = new MediaTracker(this);
         image = Toolkit.getDefaultToolkit().getImage(name);
         media.addImage(image, 0);     
              try
                   { media.waitForID(0); }
         catch (Exception e) {}
         frame.getContentPane().add(this, BorderLayout.CENTER);
              frame.setSize(1100,750);
              frame.setLocation(0,0);
              frame.show();
              addMouseListener(this);
    public void paint(Graphics g)
         if($applet.getInit() == true ) //////...cannot resolve symbol...symbol : method getInit ()
              g.drawImage(image, x, y, this);
              world.displayWorld(g);
    public void mouseClicked(MouseEvent e)
         world.setStrength(10);
         repaint();

    when I try this, i get a null pointer exception @if(main.getInit()==true)
    public class Map extends JPanel implements MouseListener
    private Main main; ////instead of the horrible $applet :)
    public Map(String name,int xp,int yp, World w, Main m)
    main=m;
    public void paint(Graphics g)
    if(main.getInit() == true ) //////...cannot resolve symbol...symbol : method getInit ()
    g.drawImage(image, x, y, this);
    world.displayWorld(g);
    ps: new to java..., forgive me for asking dumb questions

  • Help With Program Please

    Hi everybody,
    I designed a calculator, and I need help with the rest of the scientific actions. I know I need to use the different Math methods, but what exactly? Also, it needs to work as an applet and application, and in the applet, the buttons don't appear in order, how can I fix that?
    I will really appreciate your help with this program, I need to finish it ASAP. Please e-mail me at [email protected].
    Below is the code for the calcualtor.
    Thanks in advance,
    -Maria
    // calculator
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    public class calculator extends JApplet implements
    ActionListener
    private JButton one, two, three, four, five, six, seven,
    eight, nine, zero, dec, eq, plus, minus, mult, div, clear,
    mem, mrc, sin, cos, tan, asin, acos, atan, x2, sqrt, exp, pi, percent;
    private JLabel output, blank;
    private Container container;
    private String operation;
    private double number1, number2, result;
    private boolean clear = false;
    //GUI
    public void init()
    container = getContentPane();
    //Title
    //super("Calculator");
    JPanel container = new JPanel();     
    container.setLayout( new FlowLayout( FlowLayout.CENTER
    output = new JLabel("");     
    output.setBorder(new MatteBorder(2,2,2,2,Color.gray));
    output.setPreferredSize(new Dimension(1,26));     
    getContentPane().setBackground(Color.white);     
    getContentPane().add( "North",output );     
    getContentPane().add( "Center",container );
    //blank
    blank = new JLabel( " " );
    container.add( blank );
    //clear
    clear = new JButton( "CE" );
    clear.addActionListener(this);
    container.add( clear );
    //seven
    seven = new JButton( "7" );
    seven.addActionListener(this);
    container.add( seven );
    //eight
    eight = new JButton( "8" );
    eight.addActionListener(this);
    container.add( eight );
    //nine
    nine = new JButton( "9" );
    nine.addActionListener(this);
    container.add( nine );
    //div
    div = new JButton( "/" );
    div.addActionListener(this);
    container.add( div );
    //four
    four = new JButton( "4" );
    four.addActionListener(this);
    container.add( four );
    //five
    five = new JButton( "5" );
    five.addActionListener(this);
    container.add( five );
    //six
    six = new JButton( "6" );
    six.addActionListener(this);
    container.add( six );
    //mult
    mult = new JButton( "*" );
    mult.addActionListener(this);
    container.add( mult );
    //one
    one = new JButton( "1" );
    one.addActionListener(this);
    container.add( one );
    //two
    two = new JButton( "2" );
    two.addActionListener(this);
    container.add( two );
    //three
    three = new JButton( "3" );
    three.addActionListener(this);
    container.add( three );
    //minus
    minus = new JButton( "-" );
    minus.addActionListener(this);
    container.add( minus );
    //zero
    zero = new JButton( "0" );
    zero.addActionListener(this);
    container.add( zero );
    //dec
    dec = new JButton( "." );
    dec.addActionListener(this);
    container.add( dec );
    //plus
    plus = new JButton( "+" );
    plus.addActionListener(this);
    container.add( plus );
    //mem
    mem = new JButton( "MEM" );
    mem.addActionListener(this);
    container.add( mem );
    //mrc
    mrc = new JButton( "MRC" );
    mrc.addActionListener(this);
    container.add( mrc );
    //sin
    sin = new JButton( "SIN" );
    sin.addActionListener(this);
    container.add( sin );
    //cos
    cos = new JButton( "COS" );
    cos.addActionListener(this);
    container.add( cos );
    //tan
    tan = new JButton( "TAN" );
    tan.addActionListener(this);
    container.add( tan );
    //asin
    asin = new JButton( "ASIN" );
    asin.addActionListener(this);
    container.add( asin );
    //acos
    acos = new JButton( "ACOS" );
    cos.addActionListener(this);
    container.add( cos );
    //atan
    atan = new JButton( "ATAN" );
    atan.addActionListener(this);
    container.add( atan );
    //x2
    x2 = new JButton( "X2" );
    x2.addActionListener(this);
    container.add( x2 );
    //sqrt
    sqrt = new JButton( "SQRT" );
    sqrt.addActionListener(this);
    container.add( sqrt );
    //exp
    exp = new JButton( "EXP" );
    exp.addActionListener(this);
    container.add( exp );
    //pi
    pi = new JButton( "PI" );
    pi.addActionListener(this);
    container.add( pi );
    //percent
    percent = new JButton( "%" );
    percent.addActionListener(this);
    container.add( percent );
    //eq
    eq = new JButton( "=" );
    eq.addActionListener(this);
    container.add( eq );
    //Set size and visible
    setSize( 190, 285 );
    setVisible( true );
    public static void main(String args[]){
    //execute applet as application
         //applet's window
         JFrame applicationWindow = new JFrame("calculator");
    applicationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         //applet instance
         calculator appletObject = new calculator();
         //init and start methods
         appletObject.init();
         appletObject.start();
    } // end main
    public void actionPerformed(ActionEvent ae)
    JButton but = ( JButton )ae.getSource();     
    //dec action
    if( but.getText() == "." )
    //if dec is pressed, first check to make shure there
    is not already a decimal
    String temp = output.getText();
    if( temp.indexOf( '.' ) == -1 )
    output.setText( output.getText() + but.getText() );
    //clear action
    else if( but.getText() == "CE" )
    output.setText( "" );
    operation = "";
    number1 = 0.0;
    number2 = 0.0;
    //plus action
    else if( but.getText() == "+" )
    operation = "+";
    number1 = Double.parseDouble( output.getText() );
    clear = true;
    //output.setText( "" );
    //minus action
    else if( but.getText() == "-" )
    operation = "-";
    number1 = Double.parseDouble( output.getText() );
    clear = true;
    //output.setText( "" );
    //mult action
    else if( but.getText() == "*" )
    operation = "*";
    number1 = Double.parseDouble( output.getText() );
    clear = true;
    //output.setText( "" );
    //div action
    else if( but.getText() == "/" )
    operation = "/";
    number1 = Double.parseDouble( output.getText() );
    clear = true;
    //output.setText( "" );
    //eq action
    else if( but.getText() == "=" )
    number2 = Double.parseDouble( output.getText() );
    if( operation == "+" )
    result = number1 + number2;
    else if( operation == "-" )
    result = number1 - number2;
    else if( operation == "*" )
    result = number1 * number2;
    else if( operation == "/" )
    result = number1 / number2;
    //output result
    output.setText( String.valueOf( result ) );
    clear = true;
    operation = "";
    //default action
    else
    if( clear == true )
    output.setText( "" );
    clear = false;
    output.setText( output.getText() + but.getText() );

    Multiple post:
    http://forum.java.sun.com/thread.jsp?forum=31&thread=474370&tstart=0&trange=30

  • JSlider is not working

    Please give me a hint.
    Thaks
    import java.io.*;
    import java.awt.Font;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.geom.*;
    import java.awt.*;
    import java.awt.BorderLayout;
    import javax.swing.*;
    import java.util.Hashtable;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.applet.Applet;
    public class TestLayout extends JApplet
         private Drawing drawing;
            private JComboBox comb;
            private JComboBox alfa;
            private JSlider dJSlider;
         private Container panel;
            private String af[] = {"Paint","No Paint"};
            private String e[] = {"One","Two"};
            private GridBagLayout layout;
            private GridBagConstraints gbc;
         public void init()
              panel = getContentPane();
                    drawing = new Drawing();
                    panel.add(drawing,BorderLayout.CENTER);
                    panel.add(new getSlider(),BorderLayout.NORTH);
                    panel.add(new getComboPanel(),BorderLayout.EAST);
            class  getComboPanel extends JPanel {
                    public getComboPanel(){
                    comb = new JComboBox(e);
                    layout = new GridBagLayout();
                    setLayout(layout);
                    gbc = new GridBagConstraints();
                    addComponent(this,comb,0,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER);
                    comb.setEditable(false);
                    comb.setForeground (Color.BLUE);
                    comb.setBackground (Color.GREEN);
                    comb.setAlignmentX(Component.LEFT_ALIGNMENT);
                    alfa = new JComboBox(af);
                    gbc.weightx = 100;
                    gbc.weighty = 0;
                    addComponent(this,alfa,1,0,20000,1,GridBagConstraints.NONE, GridBagConstraints.CENTER);
                    alfa.setEditable(false);
                    alfa.setForeground(Color.BLUE);
                    alfa.setBackground(Color.GREEN);
                    alfa.setAlignmentX(Component.LEFT_ALIGNMENT);
                    add(comb);
                    add(alfa);
             private void addComponent(Container container, Component component,
                     int row, int column, int width, int height, int fill, int anchor)
                 gbc.gridx = column;
                 gbc.gridy = row;
                 gbc.gridwidth = width;
                 gbc.gridheight = height;
                 gbc.fill = fill;
                 gbc.anchor = anchor;
                 layout.setConstraints(component,gbc);
                 container.add(component);
              class  getSlider extends JPanel {
                public getSlider(){
                    int min = 1, max = 16, inc = 5;
                    dJSlider = new JSlider(min, max, 6);
                    dJSlider.setMajorTickSpacing(5);
                    dJSlider.setMinorTickSpacing(1);
                    dJSlider.setSnapToTicks(true);
                    dJSlider.setLabelTable(getLabelTable(min, max, inc));
                    dJSlider.setPaintTicks(true);
                    dJSlider.setPaintLabels(true);
            private Hashtable getLabelTable(int min, int max, int inc) {
            Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
            for(int j = min; j <= max; j+=inc) {
                String s = String.format("%.1f", (j+4)/10.0);
                table.put(Integer.valueOf(j), new JLabel(s));
            return table;
    class Drawing extends JPanel
           public void paintComponent(Graphics g)
                super.paintComponent( g ); // call superclass's paint method
                this.setBackground( Color.WHITE );
                Graphics2D g2d = (Graphics2D)g;
                g2d.draw3DRect(3,100,90,55,true);
       // end method paintComponent
           public static void main(String[] args)
                     JFrame appletFrame = new JFrame("My Applet");
                     Applet theApplet = new TestLayout();
                     appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                     appletFrame.setSize(800,800);
                     appletFrame.add(theApplet,"Center");
                     theApplet.init();
                     appletFrame.setVisible(true);
    }

    //  <applet code="TestLayoutRx" width="400" height="400"></applet>
    import java.applet.Applet;
    import java.awt.*;
    import java.util.Hashtable;
    import javax.swing.*;
    public class TestLayoutRx extends JApplet
        private Drawing drawing;
        private JComboBox comb;
        private JComboBox alfa;
        private JSlider dJSlider;
        private Container panel;
        private String af[] = {"Paint","No Paint"};
        private String e[] = {"One","Two"};
        private GridBagLayout layout;
        private GridBagConstraints gbc;
        public void init()
            panel = getContentPane();
            drawing = new Drawing();
            panel.add(drawing,BorderLayout.CENTER);
            panel.add(new getSlider(),BorderLayout.NORTH);
            panel.add(new getComboPanel(),BorderLayout.EAST);
        class getComboPanel extends JPanel {
            public getComboPanel() {
                comb = new JComboBox(e);
                layout = new GridBagLayout();
                setLayout(layout);
                gbc = new GridBagConstraints();
                addComponent(this,comb,0,0,1,1,GridBagConstraints.NONE,
                             GridBagConstraints.CENTER);
                comb.setEditable(false);
                comb.setForeground (Color.BLUE);
                comb.setBackground (Color.GREEN);
                comb.setAlignmentX(Component.LEFT_ALIGNMENT);
                alfa = new JComboBox(af);
                gbc.weightx = 100;
                gbc.weighty = 0;
                addComponent(this,alfa,1,0,20000,1,GridBagConstraints.NONE,
                             GridBagConstraints.CENTER);
                alfa.setEditable(false);
                alfa.setForeground(Color.BLUE);
                alfa.setBackground(Color.GREEN);
                alfa.setAlignmentX(Component.LEFT_ALIGNMENT);
                add(comb);
                add(alfa);
            private void addComponent(Container container, Component component,
                     int row, int column, int width, int height, int fill, int anchor)
                gbc.gridx = column;
                gbc.gridy = row;
                gbc.gridwidth = width;
                gbc.gridheight = height;
                gbc.fill = fill;
                gbc.anchor = anchor;
                layout.setConstraints(component,gbc);
                container.add(component);
        class getSlider extends JPanel {
            public getSlider() {
                int min = 1, max = 16, inc = 5;
                dJSlider = new JSlider(min, max, 6);
                dJSlider.setMajorTickSpacing(5);
                dJSlider.setMinorTickSpacing(1);
                dJSlider.setSnapToTicks(true);
                dJSlider.setLabelTable(getLabelTable(min, max, inc));
                dJSlider.setPaintTicks(true);
                dJSlider.setPaintLabels(true);
                // Add this component to the panel.
                setLayout(new BorderLayout());
                add(dJSlider);
            private Hashtable getLabelTable(int min, int max, int inc) {
                Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
                for(int j = min; j <= max; j+=inc) {
                    String s = String.format("%.1f", (j+4)/10.0);
                    table.put(Integer.valueOf(j), new JLabel(s));
                return table;
        class Drawing extends JPanel
            public Drawing() {
                // You only need to call/set this once.
                this.setBackground( Color.WHITE );
            public void paintComponent(Graphics g)
                super.paintComponent( g ); // call superclass's paint method
                // This goes in a constructor, not here.
    //            this.setBackground( Color.WHITE );
                // Look how many times this method is called.
                System.out.println("paintComponent");
                Graphics2D g2d = (Graphics2D)g;
                g2d.draw3DRect(3,100,90,55,true);
        public static void main(String[] args)
            JFrame appletFrame = new JFrame("My Applet");
            Applet theApplet = new TestLayoutRx();
            appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            appletFrame.setSize(400,400);
            appletFrame.add(theApplet,"Center");
            theApplet.init();
            appletFrame.setVisible(true);
    }

Maybe you are looking for