我想制定一个简单明了的方案。 想法是树立形象,改变 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.