InsaneCoder
InsaneCoder

Reputation: 8268

Getting Unexpected output with the swing code

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();
    }
}

Upvotes: 2

Views: 135

Answers (2)

mKorbel
mKorbel

Reputation: 109813

  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

Upvotes: 3

Related Questions