java 最简单的画线演示程序
谁能给个简单swing的画直线演示,功能:按下鼠标坐标拖动可以调整长度,方向,松开鼠标就可以画到画板上我用的是graphics的drawline函数画线,问题是拖动的时候...
谁能给个简单swing的画直线演示,功能:按下鼠标坐标拖动可以调整长度,方向,松开鼠标就可以画到画板上
我用的是graphics 的drawline函数画线,问题是拖动的时候会描出很多线,我想知道解决办法 展开
我用的是graphics 的drawline函数画线,问题是拖动的时候会描出很多线,我想知道解决办法 展开
3个回答
展开全部
对于你拖动的时候会描出很多线的问题,可以在public void paint(Graphics g)函数中,用super.paint(g);语句解决,就象下面我的程序一样。
最简单的画线演示程序如下:
import java.awt.Graphics;
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 DrawLine extends JFrame implements MouseListener,MouseMotionListener{
private int x0=0,y0=0,x=0,y=0;
JPanel jp=new JPanel();
DrawLine(){
jp.addMouseListener(this);
jp.addMouseMotionListener(this);
add(jp);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.drawLine(x0, y0, x, y);
}
public static void main(String[] args) {
new DrawLine();
}
public void mouseDragged(MouseEvent arg0) {
x=arg0.getXOnScreen();
y=arg0.getYOnScreen();
repaint();
}
public void mouseMoved(MouseEvent arg0) {}
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {
x0=arg0.getXOnScreen();
y0=arg0.getYOnScreen();
}
public void mouseReleased(MouseEvent arg0) {
x=arg0.getXOnScreen();
y=arg0.getYOnScreen();
repaint();
}
}
最简单的画线演示程序如下:
import java.awt.Graphics;
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 DrawLine extends JFrame implements MouseListener,MouseMotionListener{
private int x0=0,y0=0,x=0,y=0;
JPanel jp=new JPanel();
DrawLine(){
jp.addMouseListener(this);
jp.addMouseMotionListener(this);
add(jp);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.drawLine(x0, y0, x, y);
}
public static void main(String[] args) {
new DrawLine();
}
public void mouseDragged(MouseEvent arg0) {
x=arg0.getXOnScreen();
y=arg0.getYOnScreen();
repaint();
}
public void mouseMoved(MouseEvent arg0) {}
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {
x0=arg0.getXOnScreen();
y0=arg0.getYOnScreen();
}
public void mouseReleased(MouseEvent arg0) {
x=arg0.getXOnScreen();
y=arg0.getYOnScreen();
repaint();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询