Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I need help with JApplet

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help with JApplet

    First off, here is my code:
    HTML Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.imageio.ImageIO;
    
    public class Main extends JApplet implements Runnable, ActionListener {
    
    	private static final long serialVersionUID = 1L;
    	
    	private Thread thread = new Thread(this);
    	
    	private boolean running = true;
    	private boolean laidOut = false;
    
    	private int screenWidth = 500;
    	private int screenHeight = 700;
    
    	public Image jumper;
    	public Image selectedJumper;
    	public Image circle;
    	public Image square;
    	
    	private JButton startButton;
    	private	JPanel panel;
    	private	JTable table;
    	private	JScrollPane scrollPane;
    
    	public void init() {
    
    		setSize(screenWidth, screenHeight);
    		startButton = new JButton("Start"); 
    		startButton.addActionListener(this);
    		getContentPane().add(startButton);
    
    		circle = getImage(getCodeBase(), "http://www.javaprogrammingforums.com/images/Circle.png");
    		square = getImage(getCodeBase(), "http://www.javaprogrammingforums.com/images/Square.png");
    		
    		panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    		Border border = BorderFactory.createTitledBorder("Chose a jumper");
    		
    		panel.setBorder(border);
    		
    		setLayout(new BorderLayout());
    		add(panel, BorderLayout.SOUTH);
    		
    		String columnNames[] = { "Circle", "Square" };
    
    		Image dataValues[][] =
    		{
    			{ circle, square }
    		};
    
    		table = new JTable( dataValues, columnNames );
    
    		scrollPane = new JScrollPane( table );
    		panel.add( scrollPane, BorderLayout.CENTER );
    		
    
    	}
    
    	public void start() {
    		
    	}
    
    	public void destroy() {
    
    	}
    
    	public void stop() {
    
    	}
    
    	public void run() {
    
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		getContentPane().setBackground(Color.yellow);
    		System.out.println("Button was clicked");
    	}
    	public void paint(Graphics g) {
    		if (!this.laidOut) {
    			this.startButton.setSize(screenWidth/4, screenHeight/8);
    			this.startButton.setLocation(screenWidth/2-(startButton.getWidth()/2), screenHeight/2);
    			this.laidOut = true;
    		}
    		
    	}
    	
    }
    I am making a game and there are two problems that I have and can't seem to solve. I never made on applet before, and I'm not good with the swing components, but I am decent programming in Java.
    Problem #1: the start button is in the JPanel, I don't want that, I want it to be above the JPanel.
    Problem #2: I want to put images in to a JTable for organization and character selection purposes, but I keep on getting a string. Do I have to use an ImageIcon to get it to work, or does it matter?
    In case you were wonder what kind of game I'm making, I'm making one of those jump games where you jump on platforms and try to go as high as you can without missing a platform. There is going to be character selection for the jumper, and the jumpers are going to be food, like a potato, a banana, a strawberry, etc. Once I get the game completed, I'm going to post to to GameJolt and make some money from ad revenue hopefully.
    Any help would be great, thanks in advance.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: I need help with JApplet

    If you want to put the button at a different location you have to set it to the right location. Read up ony layout managers and who to use them properly. Maybe start with a GUI-building software to look at some examples.

    To display images in your table you have to define a custom renderer for your table cells. look at the setCellRenderer() method of JTable and go from there.

Similar Threads

  1. [SOLVED] Flicker with JPanel in JApplet
    By tgjava in forum AWT / Java Swing
    Replies: 10
    Last Post: June 4th, 2012, 11:52 PM
  2. Can I set the contentPane of JF to be JApplet?
    By javapenguin in forum Java Applets
    Replies: 1
    Last Post: May 23rd, 2012, 11:07 PM
  3. Animation in JApplet
    By Aurin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 14th, 2012, 10:18 AM
  4. Sockets in JApplet
    By shrey.haria in forum Java Networking
    Replies: 4
    Last Post: September 5th, 2011, 09:45 AM
  5. JApplet containing JButton and a JLabel
    By JuneM in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 08:32 AM