English 中文(简体)
参照透明框架
原标题:Drawing a line on transparent frame

我想制定一个简单明了的方案。 想法是树立形象,改变 cur,使之像亮相。 然后,当我们搬走 cur子时,它将追踪我们的运动。 这里的要求是能够借鉴透明背景(虽然只有55%左右,但并非完全透明)。

我迄今取得的进展是能够以透明背景来引导这一方向。 然而,这条路线也是透明的。 在这里,我手法:左上点击开始绘画,右边点击,并用新闻空间改变肤色。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
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;

public class FreehandExample extends JFrame implements MouseListener, MouseMotionListener, KeyListener {
     private int counter = 0;
     private int draw = 0;
     private int red[] = {58,71,231,243,255};
     private int green[] = {54,224,235,109,40};
     private int blue[] = {241,95,61,52,40};
     private Point start, end;
     private Graphics gd; 

     public FreehandExample()
     {
        setUndecorated(true);
        setBackground(new Color(255,0,0));
        setSize(new Dimension(300,200));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addKeyListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);
        setOpacity(0.55f);
        setVisible(true);
     }

    public void mousePressed(MouseEvent e) {
        start = new Point(e.getX(), e.getY()); 
    }
    public void mouseClicked(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1) draw = 1;
        if(e.getButton() == MouseEvent.BUTTON3) draw = 0;
    }
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseDragged(MouseEvent e)  {}

    public void mouseMoved(MouseEvent e) {
       gd = this.getGraphics();

       if(draw==1){
          end = new Point(e.getX(), e.getY());
          gd.setColor(new Color( red[counter],green[counter],blue[counter]));
          gd.drawLine(start.x, start.y, end.x, end.y);
          start = end;
       }
    }

    public static void main(String []args){
         new FreehandExample();
    }

    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) {}
}

I have tried the concept of per-pixel transparency... but the line drawing is not being drawn immediately... there is a delay before the line is drawn. However, it indeed produced the correct result e.g the line is not transparent while the frame is transparent. Could someone please help me modify this code to meet the requirement...? Thanks.

最佳回答

I ve managed to solve this problem using per-pixel transparency, thanks to someone from emunewz forum :) The trick is to call paintComponent() using repaint() all the time after drawing, but the fillRect() method only need to be called just once so that our lines won t disappear. Here is my code :

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
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 = -1;
private int red[] = {58,71,231,243,255};
private int green[] = {54,224,235,109,40};
private int blue[] = {241,95,61,52,40};
private int R = 240;
private int G = 240;
private int B = 200;
private Point start, end;
private Graphics gd; 
private JPanel panel;

public GradientTranslucentWindow() {
    setBackground(new Color(0,0,0,0));
    setSize(new Dimension(500,500));
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            if (g instanceof Graphics2D) {
                Paint p =  new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 0.0f, getHeight(), new Color(R, G, B, 150), true);
                Graphics2D g2d = (Graphics2D)g;
                g2d.setPaint(p);
                if(draw==-1) g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        }
    };
    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 = new Point(e.getX(), e.getY()); }
public void mouseClicked(MouseEvent e) {
    if(e.getButton() == MouseEvent.BUTTON1) draw = 1;
    if(e.getButton() == MouseEvent.BUTTON3) draw = 0;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e)  {}

 public void mouseMoved(MouseEvent e) {
     gd = this.getGraphics();

     if(draw==1){
         end = new Point(e.getX(), e.getY());
         gd.setColor(new Color( red[counter],green[counter],blue[counter]));
         gd.drawLine(start.x, start.y, end.x, end.y);
         start = end;
         panel.repaint();           
         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) {}
}
问题回答

暂无回答




相关问题
Non transparent image in transparent block

logo_area { background-color: #d9e670; filter:alpha(opacity=25); opacity:0.25; } and another div: #logo_image { background: url(../images/logo.png) no-repeat center 50%; } <div id="...

How to represent binary transparency?

Lately I ve been interested in representing uncompressed bitmaps in memory. However, one thing I m not sure how to implement properly is binary transparency. E.g., I start out with something like this:...

Transparent PNG in PictureBox

I am trying to make simple app that allows one to compare image to transparent PNG templates, by dragging the template over picture. For this I need a way to create a PictureBox that will contain the ...

Neither IE7 nor IE8 render PNG transparency

I ve got an element, an image, defined as instrument in css and for the life of me I cannot get it to properly display a png with transparency in IE7 or IE8 -- works fine in Safari and FF. I really ...

Hex colors: Numeric representation for "transparent"?

I am building a web CMS in which the user can choose colours for certain site elements. I would like to convert all colour values to hex to avoid any further formatting hassle ("rgb(x,y,z)" or named ...

PNG transparency in Interface Builder

I m adding an Image View in Interface Builder with a transparent PNG (A logo in the Navigation Bar) but the transparent pixels seems to render as white.. I searched for PNG in Interface Builder but ...

热门标签