2011-11-23 56 views
1

我想構建一個簡單的熒光筆程序。這個想法是設置一個圖像來改變光標,使它看起來像一個熒光筆。然後,當我們移動光標時,它會隨着我們的動作跟蹤一條線。這裏的要求是能夠在透明背景上繪製線條(不完全透明,只有大約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) {} 
} 

我已經試過每個像素的透明度的概念......但白描沒有被立即被吸引......有繪製直線之前的延遲。但是,它確實產生了正確的結果,例如線條不透明而邊框透明。 有人可以幫我修改這段代碼以符合要求...? 謝謝。

回答

0

我已經設法解決這個問題,使用每像素的透明度,感謝來自emunewz論壇的人:)訣竅是繪圖後,所有的時間使用repaint()調用paintComponent(),但fillRect()方法只需要調用一次,這樣我們的行就不會消失。 這是我的代碼:

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