Search This Blog

Showing posts with label JFrame. Show all posts
Showing posts with label JFrame. Show all posts

Java Program to Rotate Polygon - 2d Transformation Rotation in Java

This is a java program for rotation transformation in computer graphics. Rotation is one of the important 2d transformations in computer graphics. The program will tell you how to rotate points or polygon around a point (the pivot point). This CG lab program in java language reads the number of sides of polygon, co-ordinates of its vertices, the pivot point for rotation, and angle of rotation. It displays the original polygon and translated polygon in different colors in same screen. This program will tell you how to rotate a polygon in java. I override the paintComponent() method of JPanel to draw on a JPanel.


How to Load a Webpage in a Java App

Do you wonder how to load a webpage in your java application? It is simple. You can load a web document whether it is a web document in internet or a web document from a file in the localhost. I know three methods to do it. You can use JTextPane in java swing (javax.swing), JEditorPane in java swing (javax.swing) or WebView in javaFX (javafx.scene.web.WebView). We will discuss each method to display a webpage in a java application.

The three methods we are going to discuss are:
  • Using JTextPane
  • Using JEditorPane
  • Using WebView in JavaFX

What Does Modal and Modeless (Non Modal) Mean in Java

In java, the dialogue boxes in JOptionPane class are very popular. Now when we use dialogue boxes, whether it is message dialogue box, input dialogue or any other,  we may sometimes need to make the parent container (often JFrame or JWindow) not focusable. In another words, we may have to prevent a (parent) component taking input from the user while one of its member component is active. (You may have noticed that when error message boxes are displayed, you cannot click its parent window on many software until you dismiss it by clicking 'OK' or 'close'). Such a property of a member or child component, is called modality. In java, you can use JDialog with or without modality. A JDialog can be either modal or modeless.
A Modal dialog box is a dialog box that blocks input to some other top-level windows in the application. The modal dialog box captures the window focus until it is closed, usually in response to a button press.
A Modeless dialog box is a dialog box that does not block input to any other top level window while it is shown.
See following java code example:

How to Set JFrame in Center of Screen

By default, a java application appears at top left corner of the screen. You might have seen that most of the applications appear in the middle of the screen. I will tell you how to center your java application on screen.

To set JFrame in center of Just call this.setLocationRelativeTo(null); in constructor of the JFrame. If you are using Netbeans IDE, it should be after calling initComponents() method. But if you call setLocation() method of JFrame after this, your JFrame will get relocated.

In most cases this works and this is enough. if you want to get the center point of the screen, just refer to the following post:

How to Get Center Point of Screen in Java

How to Set Background Color of a JFrame in Java

It is easy to set Background color of a JFrame in java. But still many people cannot do it (me too was once like this). Most such people may have tried this java code to set java JFrame background color: setBackground(Color.BLACK);. But this won't work. Instead, you should set the background color of the ContentPane of the JFrame. So, use the following code to set Background color of Frame in your Java Application.

getContentPane().setBackground(Color.BLACK);

or


getContentPane().setBackground(new Color(34,172,176));

Keep in mind that you have to import java.awt.Color; You can use the first one if you don't know RGB values of the desired color. Most common colors are defined as constants in Font class. If you know the RGB color values for your desired color, you can use the second example.

How to Remove Title bar from JFrame of Java Application

To remove the title bar completely, there is a method called setUndecorated() in java. It takes a boolean variable. If the boolean is true, the title bar will be removed. The following one is an example. The method setUndecorated() invoked with a true boolean argument, will remove the title bar from the JFrame MyJFrame. The method is called here from the constructor. You may call it anywhere from within the class scope.

How to Change Shape of JFrame - Rounded Rectangle, Circle, Ellipse etc

You can change the shape of the JFrame. By default its shape is rectangular and assumes the size set using setSize() method. In this post we will discuss how to change the shape of  JFrame. You can change JFrame's shape into rounded rectagle, cirlce, ellipse, etc. We will see how to set the shape of JFrame with java code sample and screenshots. To change the shape of JFrame to any shape, we use the setShape() method.

Prerequisites

There are some prerequisites for setting or changing the shape of JFrame. They are:
The Frame should be undecorated. You should call the setUndecorated(true) method before calling setShape() method. When you set the frame undecorated, you will lose the title-bar,close button, maximise and minimise buttons. So you will have to create these button syourself.