Results 1 to 5 of 5
  1. #1
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default applet working in applet viewer, not browser

    i made several silly little applets that works fine in a applet viewer on my ide, but when i tried to load it on a browser, i would get weird results.

    Java Code:
        import javax.swing.*;
        import java.awt.event.*;
        import java.awt.*;
         
         
        public class Eyes2 extends JApplet{
         
        private int x2 = 198;
        private int y2 = 199;
        private int width2 = 15;
        private int height2 = 15;
         
        public void init(){
        getContentPane().setBackground(Color.black);
        }
         
        public void paint(Graphics g){
        super.paint(g);
         
        g.setColor(Color.white);
         
        g.fillOval(x2, y2, width2, height2);
        addMouseListener(new MyMouseListener());
         
        addMouseMotionListener(new MyMouseMotionListener());
         
        }
        private class MyMouseListener
        implements MouseListener
        {
        public void mousePressed(MouseEvent e)
        {
        }
         
        public void mouseClicked(MouseEvent e)
        {
         
        }
         
        public void mouseReleased(MouseEvent e)
        {
        }
         
        public void mouseEntered(MouseEvent e)
        {
        x2 = 198;
        y2 = 199;
        repaint();
        }
         
        public void mouseExited(MouseEvent e)
        {
        x2 = 198;
        y2 = 199;
        repaint();
        }
        }
         
         
        private class MyMouseMotionListener
        implements MouseMotionListener
        {
        public void mouseDragged(MouseEvent e)
        {
        }
         
        public void mouseMoved(MouseEvent e)
        {
        x2 = e.getX();
        y2 = e.getY();
         
        repaint();
        }
         
         
        }
         
         
         
         
         
        }
    all this applet does is display a circle that would follow your mouse everywhere. again, this works perfect on the applet viewer, but on a browser, the circle doesn't move at all.

    i've also made other applets, and one of them would appear on the site half of the time, and the other half, it would give me a blank page. can someone tell me is there something fundamentally wrong with my code?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Why are you adding the MouseListener every time you paint? That seems like a bad idea.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default

    ok i fix that and put it in the init() instead, and it now works on the browser. but how come it previously work on the applet viewer and not on the browser?

    i have this other applet that's works half of the time, and gives me a blank page the other half. again this works fine on an applet viewer.
    i just recently started reading about java gui, so this program may seem a bit ghetto..

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.text.DecimalFormat;
    
    public class PlaceOrder extends JApplet{
    	
    	private JPanel blank;
    	private JPanel blank2;
    	private JPanel blank3;
       private JPanel blank4;
    	private JPanel blank5;
    	private JPanel title; 
    	private JPanel quantity; 
    	private JPanel item1;
    	private JPanel item2;
    	private JPanel item3;
    	private JPanel button;
    	private JPanel taco;
    	private JPanel cake;
    	private JPanel fish;
    	private JPanel total;
    	private JTextField qty1;
    	private JTextField qty2;
    	private JTextField qty3;
    	private JTextField totalField;
    	private JTextField totalTime;
    	private JPanel name;
    	
    	public void init(){
    		
    		buildBlank();
    		buildBlank2();
    		buildBlank3();
    		buildBlank4();
    		buildBlank5();
    		buildTitle();
    		buildQuantity();
    		buildItem1();
    		buildItem2();
    		buildItem3();
    		buildField1();
    		buildField2();
    		buildField3();
    		buildTotal();
    		buildButton();
    		buildName();
    		setLayout(new GridLayout(9,3));
    		
    		add(title);
    		add(blank2);
    		add(name);
    		add(blank);
    		add(blank5);
    		add(quantity);
    		add(item1);
    		add(taco);
    		add(item2);
    		add(cake);
    		add(item3);
    		add(fish);
    		add(blank3);
    		add(total);
    		add(blank4);
    		add(button);
    	}
    	private void buildName(){
    		name = new JPanel();
    		JLabel name1 = new JLabel("name: ");
    		JTextField name2 = new JTextField(10);
    		name.setLayout(new FlowLayout(FlowLayout.LEFT));
    		name.add(name1);
    		name.add(name2);
    	}
    	private void buildBlank(){
    		blank = new JPanel();
    		blank.setLayout(new FlowLayout(FlowLayout.RIGHT));
    	}
    	private void buildBlank2(){
    		blank2 = new JPanel();
    		blank2.setLayout(new FlowLayout(FlowLayout.RIGHT));
    	}
    	private void buildBlank3(){
    		blank3 = new JPanel();
    		blank3.setLayout(new FlowLayout(FlowLayout.LEFT));
    	}
    	private void buildBlank4(){
    		blank4 = new JPanel();
    		blank4.setLayout(new FlowLayout(FlowLayout.LEFT));
    	}
    	private void buildBlank5(){
    		blank5 = new JPanel();
    		blank5.setLayout(new FlowLayout(FlowLayout.LEFT));
    	}
    
    
    	private void buildTitle(){
    		title = new JPanel();
    		JLabel message11 = new JLabel("The Anxious Clown ***");
    		JLabel message1 = new JLabel("Take Out / Place Order: ");
    		title.setLayout(new FlowLayout(FlowLayout.LEFT));
    		title.add(message11);
    		title.add(message1);
    	}
    	private void buildQuantity(){
    		quantity = new JPanel();
    		JLabel message2 = new JLabel("QTY");
    		quantity.setLayout(new FlowLayout(FlowLayout.CENTER));
    		quantity.add(message2); 
    	}
    	private void buildItem1(){
    		item1 = new JPanel();
    		JLabel message3 = new JLabel("skirt steak taco: ");
    		ImageIcon image1 = new ImageIcon("cilantro cream2.jpg");
    		message3.setIcon(image1);
    		item1.setLayout(new FlowLayout(FlowLayout.LEFT));
    		item1.add(message3);
    	}
    	private void buildItem2(){
    		item2 = new JPanel();
    		JLabel message42 = new JLabel("ice cream cake:");
    		ImageIcon image2 = new ImageIcon("cake2.jpg");
    		message42.setIcon(image2);
    		item2.setLayout(new FlowLayout(FlowLayout.LEFT));
    		item2.add(message42);
    		
    
    	}
    	private void buildItem3(){
    		item3 = new JPanel();
    		JLabel message5 = new JLabel("fish and chips: ");
    		ImageIcon image3 = new ImageIcon("fishchips2.jpg");
    		message5.setIcon(image3);
    
    		item3.setLayout(new FlowLayout(FlowLayout.LEFT));
    		item3.add(message5);
    
    	}
    	private void buildField1(){//taco
    		taco = new JPanel();
    		qty1 = new JTextField(5);
    		qty1.setText("0");
    		qty1.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		taco.add(qty1);	
    	}
    	private void buildField2(){//cake
    		cake = new JPanel();
    		qty2 = new JTextField(5);
    		qty2.setText("0");
    		qty2.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		cake.add(qty2);
    	}
    	private void buildField3(){//fish
    		fish = new JPanel();
    		qty3 = new JTextField(5);
    		qty3.setText("0");
    		qty3.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		fish.add(qty3);
    	}
    	private void buildTotal(){
    		total = new JPanel();
    		JLabel totalMessage = new JLabel("Total: $");
    		JLabel timeMessage = new JLabel("Food is ready in: ");
    		totalField = new JTextField(5);
    		totalTime = new JTextField(7);
    		total.setLayout(new FlowLayout(FlowLayout.CENTER));
    		total.add(totalMessage);
    		total.add(totalField); 
    		total.add(timeMessage);
    		total.add(totalTime);
    	}
    	private void buildButton(){
    		button = new JPanel();
    		JButton totalButton = new JButton("Place Order! ");
    		totalButton.addActionListener(new ButtonListener());
    		totalButton.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		button.add(totalButton);
    	}
    	private class ButtonListener implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			int numTaco, numCake, numFish;
    			numTaco = Integer.parseInt(qty1.getText());
    			numCake = Integer.parseInt(qty2.getText());
    			numFish = Integer.parseInt(qty3.getText());
    			DecimalFormat formatter = new DecimalFormat("0.00");
    			double totalAmount = (numTaco * 1.99) + (numCake * 3.99) + (numFish * 4.99);
    			totalField.setText(formatter.format(totalAmount));
    			totalTime.setText("30 mintues");
    			
    		}
    	}
    	
    	
    	
    	
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    I'm not really sure why it worked in one and not the other. Maybe something to do with the number of times paint was called, or something. You could throw in a simple count and a print statement to test that theory.

    As for your other program, I don't really know. Without an SSCCE, it's hard to narrow down the cause of the problem.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 1
    Last Post: 08-19-2010, 12:47 PM
  2. Applet working in Eclipse but not in browser
    By leeloo in forum Java Applets
    Replies: 7
    Last Post: 07-05-2010, 01:24 AM
  3. Applet not working in browser
    By gkr1989 in forum Java Applets
    Replies: 3
    Last Post: 07-03-2009, 09:43 AM
  4. java applet viewer dimensions
    By yuriythebest in forum Java Applets
    Replies: 1
    Last Post: 11-08-2008, 10:44 PM
  5. Eclipse Applet Viewer not showing graphics?
    By sjchase in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •