我想建立一个简单的荧光笔程序。我们的想法是设置一个图像来改变光标,使其看起来像荧光笔。然后,当我们移动光标时,它将跟踪我们的移动线。这里的要求是能够在透明背景上绘制线条(虽然不是完全透明,只有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 :(得分: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) {}
}