当前位置: 技术问答>java相关
请问用鼠标画的图形这样才能刷新。
来源: 互联网 发布时间:2015-08-26
本文导语: 我得程序如下: import java.awt.*; import javax.swing.*; import com.borland.jbcl.layout.*; import java.awt.event.*; /** * Title: * Description: * Copyright: Copyright (c) 2003 * Company: * @author * @version 1.0 */ public class Frame1 ext...
我得程序如下:
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2003
* Company:
* @author
* @version 1.0
*/
public class Frame1 extends JFrame implements MouseListener,MouseMotionListener{
int x0,y0,x,y;
public Frame1(){
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}}
private void jbInit() throws Exception {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent evt)
{}
public void mouseDragged(MouseEvent evt)
{
x=evt.getX();
y=evt.getY();
Graphics gg=this.getGraphics();
gg.drawLine(x0,y0,x,y);
x0=x;
y0=y;
}
public void mousePressed(MouseEvent evt)
{
x0=evt.getX();
y0=evt.getY();
}
public void mouseReleased(MouseEvent evt)
{}
public void mouseExited(MouseEvent evt)
{}
public void mouseEntered(MouseEvent evt)
{}
public void mouseClicked(MouseEvent evt)
{
}
public static void main(String[] args)
{
Frame1 frame=new Frame1();
frame.setSize(400,300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
}
当用鼠标在frame上拖动,画了一条曲线后,把frame最小化再复原,刚才画的线就没有了,请各位大侠给点意见帮忙解决一下。
先谢了!!。。。
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2003
* Company:
* @author
* @version 1.0
*/
public class Frame1 extends JFrame implements MouseListener,MouseMotionListener{
int x0,y0,x,y;
public Frame1(){
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}}
private void jbInit() throws Exception {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent evt)
{}
public void mouseDragged(MouseEvent evt)
{
x=evt.getX();
y=evt.getY();
Graphics gg=this.getGraphics();
gg.drawLine(x0,y0,x,y);
x0=x;
y0=y;
}
public void mousePressed(MouseEvent evt)
{
x0=evt.getX();
y0=evt.getY();
}
public void mouseReleased(MouseEvent evt)
{}
public void mouseExited(MouseEvent evt)
{}
public void mouseEntered(MouseEvent evt)
{}
public void mouseClicked(MouseEvent evt)
{
}
public static void main(String[] args)
{
Frame1 frame=new Frame1();
frame.setSize(400,300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
}
当用鼠标在frame上拖动,画了一条曲线后,把frame最小化再复原,刚才画的线就没有了,请各位大侠给点意见帮忙解决一下。
先谢了!!。。。
|
在内存里建一个与你的frame完全相同尺寸的image对象,然后在其上画就行了,然后在你的frame的paint事件里重画image对象就OK了.
|
我认为还是要将直线的两个端点保存起来,比如生成一个 Point对象,然后将各条
直线的 Point对象保存在一个 Vector中(如:lineVector),在你的程序中加这样一个方法:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawLine(g);
}
drawLine(g)实现画直线,可以这样写:
Enumeration temp=lineVector.elements();
while(temp.hasMoreElements())
{
Point p1=(Point)temp.nextElement();
Point p2=(Point)temp.nextElement();
g.drawLine(p1.x,p1.y,p2.x,p2.y);
}
你试试行不行
直线的 Point对象保存在一个 Vector中(如:lineVector),在你的程序中加这样一个方法:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawLine(g);
}
drawLine(g)实现画直线,可以这样写:
Enumeration temp=lineVector.elements();
while(temp.hasMoreElements())
{
Point p1=(Point)temp.nextElement();
Point p2=(Point)temp.nextElement();
g.drawLine(p1.x,p1.y,p2.x,p2.y);
}
你试试行不行