This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An off-topic question regarding basic Java Programming
#5
Here you go:
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GradientTranslucentWindow extends JFrame implements KeyListener, MouseListener, MouseMotionListener {
    private int counter = 0;
    private int draw = 0;
    private Point start, end;
    private JPanel panel;
    private static final int red[] = {58, 71, 231, 243, 255};
    private static final int green[] = {54, 224, 235, 109, 40};
    private static final int blue[] = {241, 95, 61, 52, 40};
    private static final int R = 240;
    private static final int G = 240;
    private static final int B = 200;

    public GradientTranslucentWindow() {
        setBackground(new Color(0, 0, 0, 0));
        setSize(new Dimension(500, 500));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel() {
            protected void paintComponent(Graphics g) {
                if (g instanceof Graphics2D) {
                    Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 50),
                            0.0f, getHeight(), new Color(R, G, B, 255), true);
                    Graphics2D g2d = (Graphics2D) g;
                    g2d.setPaint(p);
                    g2d.fillRect(0, 0, getWidth(), getHeight());
                    if (draw == 1) {
                        g2d.setColor(new Color(red[counter], green[counter], blue[counter]));
                        g2d.drawLine(start.x, start.y, end.x, end.y);
                        start = end;
                    }
                }
            }
        };
        setContentPane(panel);
        addKeyListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        new GradientTranslucentWindow().setVisible(true);
    }

    public void mousePressed(MouseEvent e) {
        start = e.getPoint();
        if (e.getButton() == MouseEvent.BUTTON1) draw = 1;
        else if (e.getButton() == MouseEvent.BUTTON3) draw = 0;
    }

    public void mouseClicked(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {}

    public void mouseDragged(MouseEvent e) {
        start = e.getPoint();
    }

    public void mouseMoved(MouseEvent e) {
        if (draw == 1) {
            end = e.getPoint();
            int x = (start.x < end.x) ? start.x : end.x;
            int y = (start.y < end.y) ? start.y : end.y;
            panel.repaint(x, y, Math.abs(start.x - end.x) + 1, Math.abs(start.y - end.y) + 1);
            System.out.println(start.x + " - " + start.y);
        }
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            counter++;
            if (counter > 4) counter = 0;    
        }
    }

    public void keyReleased(KeyEvent e) {}

    public void keyTyped(KeyEvent e) {}
}
Reply


Messages In This Thread
RE: An off-topic question regarding basic Java Programming - by Itaru - 11-24-2011, 07:13 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)