Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Main {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        MyTextArea txt = new MyTextArea();
        f.getContentPane().add(txt);
        f.getContentPane().add(new JButton("OK"), BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);

    }
}

class MyTextArea extends JTextArea implements MouseListener {
    private Image myImage;

    public MyTextArea() {
        addMouseListener(this);
    }

    public void addImage(Image image) {
        this.myImage = image;
        invalidate();
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(myImage, 0, 0, this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        ImageIcon img = new ImageIcon("yourImage.png");
        //place image in your working directory
        addImage(img.getImage());
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}