PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   09-10-2021

Draw

To put some graphics make your own paintable widget. Widget (GUI) - a control element in a graphical user interface
 
import javax.swing.*;
import java.awt.*;

public class App {

    public static void main(String[] args) {

        JFrame frame = new JFrame(); 
        
        MyDrawPanel panel = new MyDrawPanel(); // my panel
        
        frame.getContentPane().add(panel); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

class MyDrawPanel extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
    
        g.setColor(Color.orange);
        g.fillRect(20,50,100,100); // draw a rectangle
    }
}
... 13 lines
 

Image

Put images/ on project root, on the same level with build/ and src/.
 
import java.awt.*;
import javax.swing.*;

public class App {
    public static void main(String[] args) {

        JFrame frame = new JFrame(); 
        MyDrawPanel panel = new MyDrawPanel();

        frame.getContentPane().add(panel); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

class MyDrawPanel extends JPanel {
    
    @Override
    public void paintComponent(Graphics g) {
    
        Image image = new ImageIcon("images/elephant.png").getImage();
        g.drawImage(image, 10, 50, this); // x, y
    }
}
... 13 lines
 

Circle Gradient

Fill oval object with whatever is loaded on your paintbrush (gradient).
 
import java.awt.*;
import javax.swing.*;

public class App {
    public static void main(String[] args) {

        JFrame frame = new JFrame(); 
        MyDrawPanel panel = new MyDrawPanel();
        
        frame.getContentPane().add(panel); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

class MyDrawPanel extends JPanel {
    
    @Override
    public void paintComponent(Graphics g) {
    
        Graphics2D g2d = (Graphics2D) g;
        GradientPaint gradient = 
            new GradientPaint(70,70,Color.blue,150,150,Color.orange);
                // 70,70 starting point
        
        g2d.setPaint(gradient);
        g2d.fillOval(70, 70, 100, 100);
            // fill oval with 
            // whatever is loaded on your paintbrush (gradient)
    }
}
... 19 lines
 

Layout

By default, a frame has five regions you can add to. You can add only one thing to each region of a frame. That one thing might be a panel that holds three other things.
 
import java.awt.*;
import javax.swing.*;

public class App {
    public static void main(String[] args) {

        SimpleGui gui = new SimpleGui();
        gui.run();
    }
}

class SimpleGui {

    JFrame frame = new JFrame(); 
    JButton button = new JButton("click me");
    MyDrawPanel drawPanel = new MyDrawPanel();

    public void run() {

        // Add the two widget to the two region of the frame

        frame.getContentPane().add(BorderLayout.SOUTH, button);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel); 

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

class MyDrawPanel extends JPanel {
            
    @Override
    public void paintComponent(Graphics g) {

        g.setColor(Color.orange);
        g.fillOval(70,70,100,100);
    }
}
... 22 lines
 

Console

To simulate a console on a GUI, use a JTextPane. System.out.println can't be used to print to GUI.
 
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class App {

    private static JTextPane panel;

    public static void main(String[] args) {

        JFrame frame = new JFrame(); 
        panel = new JTextPane(); // my panel

        print("abc");
        print("abc");
        
        frame.getContentPane().add(panel); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    private static void print(String msg) {

        Document doc = panel.getDocument();   // Look Here
        try {
            doc.insertString(doc.getLength(), msg + "\r\n", null);
        } catch (BadLocationException e) {e.printStackTrace();}
    }
}
... 16 lines
 
To change color on panel, use styled document.
 
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class App {

    private static JTextPane panel;

    public static void main(String[] args) throws InterruptedException {

        JFrame frame = new JFrame("My Console"); 
        
        panel = new JTextPane();
        panel.setSize(300, 300);
        panel.setBackground(Color.BLACK);

        frame.getContentPane().add(panel); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.setSize(300, 300);
        frame.setVisible(true);

        while(true) {
            print("Console Message ...");
            print("Error Message ...",Color.RED);
            Thread.sleep(1000);
        }
    }

    private static void print(String msg, Color color) {

        StyledDocument doc = 
            (StyledDocument) panel.getDocument(); // Look Here

        Style style = doc.addStyle("Colors", null);
        StyleConstants.setForeground(style, color);

        try {
            doc.insertString(doc.getLength(), msg + "\r\n", style);
        } catch (BadLocationException e) {e.printStackTrace();}
    }

    private static void print(String msg) {
        print(msg, Color.WHITE);
    }
}
... 31 lines
 

Questions    
Listener