gpt4 book ai didi

Java Awt 钢笔工具 : Graphic disappeared

转载 作者:行者123 更新时间:2023-12-01 14:44:42 27 4
gpt4 key购买 nike

我将尝试使用钢笔工具实现 Java Applet。当我启动小程序时,我可以通过按下并释放鼠标在窗口上正确绘制线条。几秒钟后一切都消失了。解决这个问题的一些技巧?

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;


public class MarkPen extends Applet implements ActionListener, AdjustmentListener, MouseListener, MouseMotionListener {

private int x = 0;
private int y = 0;
private int prevx = 0;
private int prevy = 0;

private boolean dragging = true;
private Panel drawPanel = new Panel();

public void init()
{
setLayout(new BorderLayout());

drawPanel.setBackground(Color.white);
add(drawPanel, "Center");
drawPanel.addMouseMotionListener(this);
drawPanel.addMouseListener(this);

}


public void setGraphicalDefaults(MouseEvent e)
{
x = e.getX();
y = e.getY();
prevx = e.getX();
prevy = e.getY();
}

public void mouseDragged(MouseEvent e)
{
Graphics g = drawPanel.getGraphics();
g.setColor(Color.black);

/*
IF i'm not dragging...
*/
if (dragging)
{
setGraphicalDefaults(e);
dragging = false;
g.drawLine(prevx,prevy,x,y);
}

/*
If mouse moved..
*/
if (x != e.getX() || y != e.getY())
{
x = e.getX();
y = e.getY();

g.drawLine(prevx,prevy,x,y);

prevx = x;
prevy = y;
}
}

public void mouseReleased(MouseEvent e)
{
dragging = true;
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void adjustmentValueChanged(AdjustmentEvent arg0){}
public void actionPerformed(ActionEvent e){}

}

新版本:我更正了我的代码,下面是工作版本

   import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JPanel;



public class MarkPen extends Applet implements ActionListener, AdjustmentListener, MouseListener, MouseMotionListener {

private int x = 0;
private int y = 0;
private int prevx = 0;
private int prevy = 0;

private JPanel drawPanel = new JPanel();

public void init()
{
setLayout(new BorderLayout());

drawPanel.setBackground(Color.white);
add(drawPanel, "Center");
drawPanel.addMouseMotionListener(this);
drawPanel.addMouseListener(this);
drawPanel.add(new Comp());
drawPanel.setDoubleBuffered(true);

}

public void setGraphicalDefaults(MouseEvent e)
{
x = e.getX();
y = e.getY();
prevx = e.getX();
prevy = e.getY();
}

public class Comp extends JComponent{
public void paintComponent(Graphics g) {
super.paintComponent(g);
g = drawPanel.getGraphics();
g.setColor(Color.black);

g.drawLine(prevx,prevy,x,y);
prevx = x;
prevy = y;
g.dispose();
repaint();
}
}


public void mouseDragged(MouseEvent e)
{
if (x != e.getX() || y != e.getY())
{
x = e.getX();
y = e.getY();
}
}

public void mousePressed(MouseEvent e){
setGraphicalDefaults(e);
}

public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void adjustmentValueChanged(AdjustmentEvent arg0){}
public void actionPerformed(ActionEvent e){}

}

最佳答案

图形消失是有道理的,因为您在一个组件上调用 getGraphics() ,该组件会生成不稳定的 Graphics 对象,该对象不会持久存在。不要那样做。而是使用 paint(...) 覆盖进行绘制。更好的方法是使用 Swing GUI 并在 JPanel 的 paintComponent(...) 方法中进行绘制,或者在在 paintComponent(...) 方法中显示的 BufferedImage。

关于Java Awt 钢笔工具 : Graphic disappeared,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15553113/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com