Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8340101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:04:59+00:00 2026-06-09T05:04:59+00:00

I’m trying to built a simple app which has a translucent frame and it

  • 0

I’m trying to built a simple app which has a translucent frame and it draws lines where the user wants.I have also added listeners to catch the mouse events and these are displayed accordingly .Everything is working fine but the problems are:
1)the window is not transparent
2)it is completely black and the lines are appearing white.

Can anyone
Here is the code:

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MouseListen2 extends JFrame implements MouseListener {

    String str = "Nothing";
    int x[][] = new int[100][2];
    int count = 0;
    int flag = 1;
    boolean draw = false;

    MouseListen2() {
        super("Line Draw App");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        this.addMouseListener(this);
        setBackground(new Color(0, 0, 0, 0));
        JPanel jp = new JPanel() {

            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                Paint gp = new GradientPaint(300, 700, new Color(20, 20, 210, 0), 100, 00, new Color(10, 20, 40, 255));
                g2.setPaint(gp);
                g2.fillRect(0, 0, getWidth(), getHeight());
            }
        };
        setContentPane(jp);
        setVisible(true);
        //c.setOpaque(true);
    }

    public void paint(Graphics g) {
        //Graphics g=this.getGraphics();
        //super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.clearRect(0, 0, getWidth(), getHeight());
        g2.drawString(str, 50, 50);
        //initially count=0 hence i<-1 so loop will not automatically run in the beginning
        for (int i = 0; i < count - 1; i = i + 2) {
            g2.drawLine(x[i][0], x[i][1], x[i + 1][0], x[i + 1][1]);
        }
        //repaint(); using this here creates an infinite loop as after mouse event paint is called and  at the end
        //this method is again called using the repaint() and so on the loop continues.
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new MouseListen2();
            }
        });
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        str = "clicked";
        repaint();

    }

    @Override
    public void mousePressed(MouseEvent e) {
        str = "pressed";
        repaint();
        x[count][0] = e.getX();
        x[count][1] = e.getY();
        count++;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        str = "released";
        draw = true;
        x[count][0] = e.getX();
        x[count][1] = e.getY();
        count++;
        //draw();
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        str = "entered";
        repaint();
    }

    @Override
    public void mouseExited(MouseEvent e) {
        str = "exited";
        repaint();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:05:01+00:00Added an answer on June 9, 2026 at 5:05 am
    1. have to add MouseListener to the JPanel, because you added MouseListener to the JFrame (this.addMouseListener(this);)

    2. this code line setContentPane(jp); put JPanel to the BorderLayout.CENTER possition to the JFrame, in this case (isn’t there any another JComponent added to the JFrame) fills whole / all available space into JFrame

    3. then for mouse event is accesible only JPanel, mouse can’t access to the JFrame‘s RootPane or ContentPane

    4. remove / comment public void paint(Graphics g) and rellated code block move to the paintComponent for the JPanel

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I'm trying to select an H1 element which is the second-child in its group
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.