请问用鼠标画的图形这样才能刷新。 加public void paint(Graphics g){} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 加paint方法后,里面怎么写。 请问paint方法里怎么写,我不可能把鼠标移动过的位置都存起来,让paint方法来重画阿??? 直接在paint里面画就得啦, help 里面有现成的。 我认为还是要将直线的两个端点保存起来,比如生成一个 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);}你试试行不行 rongrongGsr() 我用你的方法试了一下,好像是可以的不过就是其中有一些小问题,我画的不是一条线,而是几条,在判断鼠标抬起是就不好处理。 还有这样把所有的点都保存下来,是不是有一点耗资源, 有没有什么方法,可以在程序调用paint时,先把图形保存起来,下一次重画时就直接把次图形repaint()一下。谢谢。 e_snow(呆呆) 这位朋友,你说help里有,我找不到,我用的时jb5。请详细一点好吗?谢谢 在内存里建一个与你的frame完全相同尺寸的image对象,然后在其上画就行了,然后在你的frame的paint事件里重画image对象就OK了. 谢谢haichuang,用你的方法我在Applet里面成功那,但是在Frame里面却报错java.lang.NullPointerException不知为什么??? 我的意思是,在sample里面找个类似的source,可以借鉴一下! 贴出你的新源码,这个提示是说什么对象是null,你可能有地方写的有点错误. public class Frame1 extends JFrame implements MouseListener,MouseMotionListener{ int x1,y1,x2,y2; Canvas c=new Canvas(); Image image=c.createImage(400,300); Graphics gg=image.getGraphics(); public Frame1(){ try { jbInit(); } catch(Exception e) { e.printStackTrace(); }} private void jbInit() throws Exception { this.getContentPane().setLayout(xYLayout1); this.addMouseListener(this); this.addMouseMotionListener(this); this.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseExited(MouseEvent e) { this_mouseExited(e); } }); } public void paint(Graphics g) { g.drawImage(image,0,0,this); } public void mouseMoved(MouseEvent evt) { } public void mouseDragged(MouseEvent evt) { x2=evt.getX(); y2=evt.getY(); gg.drawLine(x1,y1,x2,y2); x1=x2; y1=y2; } public void mousePressed(MouseEvent evt) { x1=evt.getX(); y1=evt.getY(); } public void mouseReleased(MouseEvent evt) { } public void mouseExited(MouseEvent evt) {} public void mouseEntered(MouseEvent evt) {} public void mouseClicked(MouseEvent evt) { } XYLayout xYLayout1 = new XYLayout(); void this_mouseExited(MouseEvent e) { }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);} } 我误打误创的成功了。下面死我的代码: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 x1,y1,x2,y2; boolean flag=false; Canvas c=new Canvas(); Image image=null; Graphics gg=null; FlowLayout flowLayout1 = new FlowLayout(); public Frame1(){ try { jbInit(); } catch(Exception e) { e.printStackTrace(); }} private void jbInit() throws Exception { this.getContentPane().setLayout(flowLayout1); this.addMouseListener(this); this.addMouseMotionListener(this); } public void paint(Graphics g) { if(flag) g.drawImage(image,0,0,this); } public void mouseMoved(MouseEvent evt) { } public void mouseDragged(MouseEvent evt) { x2=evt.getX(); y2=evt.getY(); gg.drawLine(x1,y1,x2,y2); x1=x2; y1=y2; this.repaint(); } public void mousePressed(MouseEvent evt) { flag=true; x1=evt.getX(); y1=evt.getY(); image=createImage(400,300); gg=image.getGraphics(); } public void mouseReleased(MouseEvent evt) { } public void mouseExited(MouseEvent evt) {} public void mouseEntered(MouseEvent evt) {} public void mouseClicked(MouseEvent evt) { } void this_mouseExited(MouseEvent e) { } public void update(Graphics g) { paint(g); }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);} }程序中有什么需要改进的,请各位朋友不吝赐教,谢谢,我明天来结贴,谢谢 你不应该在MousePressed事件中建立Image对象,应该在初始化的时候就建立. 如果你只是想将文件保存成如 jpg等格式的图片,用以上的方法的确是不错的,但是如果你要在下次用你的程序打开你上次画的图形文件并且要继续编辑修改的话,我认为还是要设一些数组来保存你画的点、矩形、椭园等。这点资源对计算机来说并不算什么,我是这样认为的。 rongrongGsr()朋友说得问题正好是我又想解决的,怎样才能把已经化好的image保存出来,这已是另一个问题,希望大家有什么好方法告诉我。谢谢。 保存图片,处理图片可以使用sun的jai包.http://java.sun.com/products/java-media/jai/ 如何获取GOOGLE MAP中所有的精度纬度 思考了头疼,求救,如果判断线程已经被suspend了吗? 各位帮忙看一下,为什么这个运行结果是这样的 问一个时加减的问题,谢谢了,很急!!!!!!!!!!!!! 类型申明时的星号是什么意思?如long * v 精确定位的JFrame如何刷新 我写的线程小程序,可是发现线程内的Run()方法内的代码并没有执行,请高手指点。 布局管理器 特别菜鸟的问题,希望朋友们帮忙!谢谢!(我是菜鸟) java小白求助 关于内部类的问题 快绕晕了 c++转java 望高手指教 mysql & JDBC 问题--(在线等待)
直线的 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);
}
你试试行不行
还有这样把所有的点都保存下来,是不是有一点耗资源,
有没有什么方法,可以在程序调用paint时,先把图形保存起来,下一次重画时就直接把次图形repaint()一下。
谢谢。
java.lang.NullPointerException
不知为什么???
int x1,y1,x2,y2;
Canvas c=new Canvas();
Image image=c.createImage(400,300);
Graphics gg=image.getGraphics();
public Frame1(){
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}}
private void jbInit() throws Exception { this.getContentPane().setLayout(xYLayout1);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseExited(MouseEvent e) {
this_mouseExited(e);
}
});
}
public void paint(Graphics g)
{
g.drawImage(image,0,0,this);
}
public void mouseMoved(MouseEvent evt)
{
}
public void mouseDragged(MouseEvent evt)
{
x2=evt.getX();
y2=evt.getY();
gg.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
public void mousePressed(MouseEvent evt)
{
x1=evt.getX();
y1=evt.getY();
}
public void mouseReleased(MouseEvent evt)
{
}
public void mouseExited(MouseEvent evt)
{}
public void mouseEntered(MouseEvent evt)
{}
public void mouseClicked(MouseEvent evt)
{
}
XYLayout xYLayout1 = new XYLayout();
void this_mouseExited(MouseEvent e) {
}
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);} }
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 x1,y1,x2,y2;
boolean flag=false;
Canvas c=new Canvas();
Image image=null;
Graphics gg=null;
FlowLayout flowLayout1 = new FlowLayout();
public Frame1(){
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}}
private void jbInit() throws Exception {
this.getContentPane().setLayout(flowLayout1);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paint(Graphics g)
{
if(flag)
g.drawImage(image,0,0,this);
}
public void mouseMoved(MouseEvent evt)
{
}
public void mouseDragged(MouseEvent evt)
{
x2=evt.getX();
y2=evt.getY();
gg.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
this.repaint();
}
public void mousePressed(MouseEvent evt)
{
flag=true;
x1=evt.getX();
y1=evt.getY();
image=createImage(400,300);
gg=image.getGraphics();
}
public void mouseReleased(MouseEvent evt)
{
}
public void mouseExited(MouseEvent evt)
{}
public void mouseEntered(MouseEvent evt)
{}
public void mouseClicked(MouseEvent evt)
{
}
void this_mouseExited(MouseEvent e) {
}
public void update(Graphics g)
{
paint(g);
}
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);}
}
程序中有什么需要改进的,请各位朋友不吝赐教,谢谢,我明天来结贴,谢谢
http://java.sun.com/products/java-media/jai/