Useful Java Code Snippets 3 - Java Applets, JPanel, JFrame



Looking for useful code examples to build your Java applications? 

An applet is a small Internet-based program written in Java, a programming language for the Web, which can be downloaded by any computer. The applet is also able to run in HTML. 
The applet is usually embedded in an HTML page on a Web site and can be executed from within a browser. Here is an example: 


import java.awt.Graphics;
import javax.swing.JApplet;

public class Applets extends JApplet {
  
  // Write a string to the Applet Screen
  public void paint(Graphics g){
    super.paint(g);
    g.drawString("Berk SOYSAL", 35, 35); 
  }
}


If you want to use this Applet in a web page; 
  • First of all, you need to find Applets.class in your workspace and upload that file to your Ftp server. 
  • Then you need to add this simple code line to the body part of your html code of the index page:

<html>
<body>
<applet code="Applets.class" width="400" height="100">
</applet>
</body>
</html>


  • If you do everything right, the result should be like this,











2 - Summation of two numbers via Init() method and printing the result on an Applet

/**
* Author : Berk Soysal
* Applets.java
*/

import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

public class Applets extends JApplet {
  
  private double sum;
 
  public void init(){
    String s = JOptionPane.showInputDialog("Enter a number");
    String s2 = JOptionPane.showInputDialog("Enter another number");
    
    double n = Double.parseDouble(s);
    double n2 = Double.parseDouble(s2);
    
    sum = n + n2;
    
  }
  
  public void paint(Graphics g){
    super.paint(g);
    g.drawString("The sum is " + sum, 35, 35); 
  }
}



  • If you do everything right, the result should be like this,
 











3 - Drawing A Circle Using JPanel, JFrame and JSlider 

In this example, we have 3 classes; 
  • DrawCircle: A class used to draw the circle according to the diameter value provided by the TheWindow class. 
  • TheWindow: In this class a DrawCircle and a Slider objects were created and the value read from the ChangeListener is given to the DrawCircle class. Also the background color of the Panel is determined in this class.
  • Main: In this class a TheWindow object is created and the size of the frame is determined as 400x400 pixels.
 

/**
* Author : Berk Soysal
* DrawCircle.java
*/

package herrberk;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawCircle extends JPanel{
  
  private int diameter=10;
  
  protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.RED);
    g.fillOval(10, 10, diameter, diameter);
    
  }
  
  // To check if the diameter is greater than zero
  protected void setDiameter(int newDiameter){
    diameter = (newDiameter > 0 ? newDiameter :1);
    repaint();
  }
  
  public Dimension getPreferredSize(){
    return new Dimension(200,200);
  }
  
  public Dimension getMinimumSize(){
    return getPreferredSize();
  }
}




/**
* Author : Berk Soysal
* TheWindow.java
*/

package herrberk;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TheWindow extends JFrame{
  
  private JSlider slider;
  private DrawCircle myPanel;
  
  //Constructor of the class
  public TheWindow(){
    super("Slider Controlled Circle");
    myPanel = new DrawCircle();
    
    // Change the Background Color of the Panel
    myPanel.setBackground(Color.YELLOW);
    
    slider = new JSlider(SwingConstants.HORIZONTAL,0,300,10);
    slider.setMajorTickSpacing(20);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    slider.addChangeListener(
                             new ChangeListener(){
      public void stateChanged(ChangeEvent e){
        myPanel.setDiameter(slider.getValue());
        repaint(); 
      }
    }
    );
    // Add the Slider and the Panel to the Frame
    add(slider, BorderLayout.SOUTH);
    add(myPanel, BorderLayout.CENTER);
  }
}




/**
* Author : Berk Soysal
* Main.java
*/

package herrberk;

import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    
    TheWindow w = new TheWindow();
    w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    w.setSize(400, 400);
    w.setVisible(true);
    
  }
}



  • If you do everything right, the result should be like this, 




Please comment below if you have any questions. Thanks.



Continue Reading Useful Java Code Snippets 4 - Multithreading in Java

Author:

Software Developer, Codemio Admin

Disqus Comments Loading..