Online Java Compiler

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class Dice extends JApplet{ /** Applet initialization requires putting the panel in applet.*/ public Dice() { this.setContentPane(new RollDicePanel()); }//end constructor //====================================================== method main /** Create JFrame and set content pane to a RollDicePanel. */ public static void main(String[] args) { JFrame window = new JFrame(); window.setTitle("Dice Demo"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setContentPane(new RollDicePanel()); window.pack(); window.show(); }//end main } class RollDicePanel extends JPanel { //=============================================== instance variables private Die _left; // component for one die private Die _right; //end instance variables //====================================================== constructor /** Create border layout panel with two Die's and one button. */ RollDicePanel() { //--- Create the dice _left = new Die(); _right = new Die(); //--- Create the button to roll the dice JButton rollButton = new JButton("Roll"); rollButton.setFont(new Font("Sansserif", Font.PLAIN, 24)); rollButton.addActionListener(new RollListener()); //--- Create panel for two dice JPanel dicePanel = new JPanel(); dicePanel.setLayout(new GridLayout(1, 2, 4, 0)); dicePanel.add(_left); dicePanel.add(_right); //--- Add components to content pane this.setLayout(new BorderLayout()); this.add(rollButton, BorderLayout.NORTH); this.add(dicePanel , BorderLayout.CENTER); }//end constructor /////////////////////////////////// inner listener class RollListener /** Inner listener class for Roll button. */ private class RollListener implements ActionListener { public void actionPerformed(ActionEvent e) { _left.roll(); _right.roll(); } } } class Die extends JPanel { //=============================================== instance variables private int _value; // value that shows on face of die private int _diam = 9; // Diameter of spots //end instance variables //=============================================== class variables private static Random random = new Random(); // random generator //end class variables //====================================================== constructor /** Initialize to white background and 60x60 pixels. Initial roll.*/ public Die() { setBackground(Color.white); //-- Preferred size is set, but layout may change it. setPreferredSize(new Dimension(60,60)); roll(); // Set to random initial value }//end constructor //====================================================== method roll /** Produce random roll in range 1-6. Causes repaint(). @return Result of roll (1-6). */ public int roll() { int val = random.nextInt(6) + 1; // Range 1-6 setValue(val); return val; }//end roll //================================================== method getValue /** Returns result of last roll.*/ public int getValue() { return _value; }//end setValue //================================================== method setValue /** Sets the value of the Die. Causes repaint(). @param spots Number from 1-6. */ public void setValue(int spots) { _value = spots; repaint(); // Value has changed, must repaint }//end setValue //============================================ method paintComponent /** Draws spots of die face. */ public void paintComponent(Graphics g) { super.paintComponent(g); // required int w = getWidth(); int h = getHeight(); // should use to resize spots too. switch (_value) { case 1: drawSpot(g, w/2, h/2); break; case 3: drawSpot(g, w/2, h/2); // Fall thru to next case case 2: drawSpot(g, w/4, h/4); drawSpot(g, 3*w/4, 3*h/4); break; case 5: drawSpot(g, w/2, h/2); // Fall thru to next case case 4: drawSpot(g, w/4, h/4); drawSpot(g, 3*w/4, 3*h/4); drawSpot(g, 3*w/4, h/4); drawSpot(g, w/4, 3*h/4); break; case 6: drawSpot(g, w/4, h/4); drawSpot(g, 3*w/4, 3*h/4); drawSpot(g, 3*w/4, h/4); drawSpot(g, w/4, 3*h/4); drawSpot(g, w/4, h/2); drawSpot(g, 3*w/4, h/2); break; } }//end paintComponent /** Utility method used by paintComponent(). */ //================================================== method drawSpot private void drawSpot(Graphics g, int x, int y) { g.fillOval(x-_diam/2, y-_diam/2, _diam, _diam); }//end drawSpot }//

About Online Java Compiler

Try our Online Java Compiler (Version OpenJDK 11.0.17) to Edit, Run, and Share your Java Code directly from your browser. This online development environment provides you the latest version OpenJDK 11.0.17.

How to use Online Java Compiler?

Write and Execute Code

  • Write your program (or, paste it) directly under the "Source Code" tab.
  • If you want to save your program, go to the "Project" menu and save it.
  • You can directly execute your program without saving it by clicking on on "Execute" button.

User Input

The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.

Online Java Compiler: Keyboard Shortcuts

The following are the keyword shortcut of this Online Java Compiler:

ShortcutDescription
⌘ + EnterRun the program
⌘ + SSave Project (Login Required)
⇧ + ⌘ + SSave As Project
⌘ + PNew Project
⌘ + GShare Project
⌘ + ZUndo Editing
⌘ + YRedo Editing
⌘ + ASelect All Text
⌘ + XCut Selected Text
⌘ + CCopy Selected Text
⌘ + VPaste Copied Text
⌘ + FSearch Text
⌘ + ⌥ + FReplace Text
ShortcutDescription
Ctrl + EnterRun the program
Ctrl + SSave Project
Shift + Ctrl + SSave As Project
Ctrl + GShare Project
Ctrl + ZUndo Editing
Ctrl + YRedo Editing
Ctrl + ASelect All Text
Ctrl + XCut Selected Text
Ctrl + CCopy Selected Text
Ctrl + VPaste Copied Text
Ctrl + FSearch Text
Ctrl + HReplace Text

Online Java Compiler: Save and Share Java Code (Project)

Save Java Project Online

You can save your Java Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.

Share Java Project Online

You can use this feature to share your Java Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.

More Features of Online Java Compiler

  • Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
  • Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
  • Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
  • Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
  • And, many more.

Benefits of Using Online Java Compiler

There are several benefits of using the Online Java Compiler to run your Java code:

  • Platform independence: You can run your code from any device without taking care of operating systems.
  • Convenience: You don't need to install anything for using this.
  • No setup required: There is no need for additional setup to run your code.
  • Updated version: Our online compiler/editors/terminals are the latest up-to-date.
 Execute |  Beautify | Share
My Projects
Change Password
My Profile
Logout
Undo
Redo
Cut
Copy
Paste
Delete
Select All
Find
Find and Replace
Editor Theme
Crimson
Eclipse
Github
Solarized
Cobalt
krTheme
Monokai
Terminal
Textmate
Twilight
Vibrant Ink
Font Size
8px
9px
10px
11px
12px
13px
14px
15px
16px
17px
18px
20px
22px
24px
Tab Size
1
2
3
4
5
6
7
8
Show Invisible
Hide Invisible
Show Line Numbers
Hide Line Numbers
Ace Editor (Default)
Vim Editor
Emacs Editor
Open New Project
Save Project
Save As New Project
Share Project
Search Project
Online Java Compiler
Online Python Compiler
Online C++ Compiler
Online CSharp Compiler
Online C Compiler
Online PHP Compiler
Online R Compiler
Online NumPy Compiler
More Compilers