当前位置: 技术问答>java相关
300分的问题?帮我查一下问题出在哪里,高分重谢!!
来源: 互联网 发布时间:2015-07-02
本文导语: /** * 重新绘制panel * @param g */ public void paintComponent(Graphics g) { int x,y; super.paintComponent(g); currentGraphics=g; Dimension d = getSize(); int clientWidth = d.width; int clientHe...
/**
* 重新绘制panel
* @param g
*/
public void paintComponent(Graphics g)
{ int x,y;
super.paintComponent(g);
currentGraphics=g;
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
if (image!=null)
{
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
if (clientWidth > imageWidth){
if (clientHeight>imageHeight){
x=(clientWidth-imageWidth)/2;
y=(clientHeight-imageHeight)/2;}
else
{x=0;y=0;}
}
else
{x=0;y=0;}
g.drawImage(image,x, y, this);
}
}
public void drawLines(Component c1,Component c2)
{
Graphics g = this.getGraphics();
paintComponent(g);
int c1x=c1.getX()+c1.getWidth()/2;
int c1y=c1.getY()+c1.getHeight()/2;
int c2x=c2.getX()+c2.getWidth()/2;
int c2y=c2.getY()+c2.getHeight()/2;
g.setColor(Color.red);
g.drawLine(c1x,c1y,c2x,c2y);]
repaint();
}
运行没有出错,但是在调用drawLines的时候,在两个控件间画的线只是闪了一下就不见了
各位老大可以帮我找出原因吗?如何把画出来的线同原来的画面合并在一起??
* 重新绘制panel
* @param g
*/
public void paintComponent(Graphics g)
{ int x,y;
super.paintComponent(g);
currentGraphics=g;
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
if (image!=null)
{
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
if (clientWidth > imageWidth){
if (clientHeight>imageHeight){
x=(clientWidth-imageWidth)/2;
y=(clientHeight-imageHeight)/2;}
else
{x=0;y=0;}
}
else
{x=0;y=0;}
g.drawImage(image,x, y, this);
}
}
public void drawLines(Component c1,Component c2)
{
Graphics g = this.getGraphics();
paintComponent(g);
int c1x=c1.getX()+c1.getWidth()/2;
int c1y=c1.getY()+c1.getHeight()/2;
int c2x=c2.getX()+c2.getWidth()/2;
int c2y=c2.getY()+c2.getHeight()/2;
g.setColor(Color.red);
g.drawLine(c1x,c1y,c2x,c2y);]
repaint();
}
运行没有出错,但是在调用drawLines的时候,在两个控件间画的线只是闪了一下就不见了
各位老大可以帮我找出原因吗?如何把画出来的线同原来的画面合并在一起??
|
还没有行吗?写了个小程序,不知道算不算你说的‘随便画线’,希望有所帮助
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DrawLinePanel
{
public static void main(String[] args)
{
JFrame frame=new JFrame("--DrawLinePanel--");
Container con=frame.getContentPane();
MyPanel mp=new MyPanel();
MyMouseListener listener1=new MyMouseListener(mp);
MyMouseListener2 listener2=new MyMouseListener2(mp);
mp.addMouseListener(listener1);
mp.addMouseMotionListener(listener2);
con.add(mp,BorderLayout.CENTER);
frame.setBounds(30,30,300,300);
frame.show();
}
}
class MyPanel extends JPanel
{
Vector v=new Vector();
MyPoint point=new MyPoint();
boolean flag=false;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Enumeration enum=v.elements();
while(enum.hasMoreElements())
{
Object obj=enum.nextElement();
MyPoint p=(MyPoint)obj;
g.drawLine(p.sX,p.sY,p.eX,p.eY);
}
if(flag)
{
g.drawLine(point.sX,point.sY,point.eX,point.eY);
}
}
}
class MyMouseListener extends MouseAdapter
{
MyPanel dlp;
public MyMouseListener(MyPanel d)
{
this.dlp=d;
}
public void mousePressed(MouseEvent e)
{
dlp.point.sX=e.getX();
dlp.point.sY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
if(dlp.flag)
{
dlp.v.add(new MyPoint(dlp.point.sX,dlp.point.sY,e.getX(),e.getY()));
dlp.flag=false;
dlp.point.reset();
dlp.repaint();
}
}
}
class MyMouseListener2 extends MouseMotionAdapter
{
MyPanel dlp=null;
public MyMouseListener2(MyPanel d)
{
this.dlp=d;
}
public void mouseDragged(MouseEvent e)
{
dlp.flag=true;
dlp.point.eX=e.getX();
dlp.point.eY=e.getY();
dlp.repaint();
}
}
class MyPoint
{
int sX=0;
int sY=0;
int eX=0;
int eY=0;
public MyPoint()
{
}
public MyPoint(int x,int y,int m,int n)
{
sX=x;sY=y;eX=m;eY=n;
}
void reset()
{
sX=0;
sY=0;
eX=0;
eY=0;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DrawLinePanel
{
public static void main(String[] args)
{
JFrame frame=new JFrame("--DrawLinePanel--");
Container con=frame.getContentPane();
MyPanel mp=new MyPanel();
MyMouseListener listener1=new MyMouseListener(mp);
MyMouseListener2 listener2=new MyMouseListener2(mp);
mp.addMouseListener(listener1);
mp.addMouseMotionListener(listener2);
con.add(mp,BorderLayout.CENTER);
frame.setBounds(30,30,300,300);
frame.show();
}
}
class MyPanel extends JPanel
{
Vector v=new Vector();
MyPoint point=new MyPoint();
boolean flag=false;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Enumeration enum=v.elements();
while(enum.hasMoreElements())
{
Object obj=enum.nextElement();
MyPoint p=(MyPoint)obj;
g.drawLine(p.sX,p.sY,p.eX,p.eY);
}
if(flag)
{
g.drawLine(point.sX,point.sY,point.eX,point.eY);
}
}
}
class MyMouseListener extends MouseAdapter
{
MyPanel dlp;
public MyMouseListener(MyPanel d)
{
this.dlp=d;
}
public void mousePressed(MouseEvent e)
{
dlp.point.sX=e.getX();
dlp.point.sY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
if(dlp.flag)
{
dlp.v.add(new MyPoint(dlp.point.sX,dlp.point.sY,e.getX(),e.getY()));
dlp.flag=false;
dlp.point.reset();
dlp.repaint();
}
}
}
class MyMouseListener2 extends MouseMotionAdapter
{
MyPanel dlp=null;
public MyMouseListener2(MyPanel d)
{
this.dlp=d;
}
public void mouseDragged(MouseEvent e)
{
dlp.flag=true;
dlp.point.eX=e.getX();
dlp.point.eY=e.getY();
dlp.repaint();
}
}
class MyPoint
{
int sX=0;
int sY=0;
int eX=0;
int eY=0;
public MyPoint()
{
}
public MyPoint(int x,int y,int m,int n)
{
sX=x;sY=y;eX=m;eY=n;
}
void reset()
{
sX=0;
sY=0;
eX=0;
eY=0;
}
}
|
paintComponent()会调用panel的UI代表重绘整个panel,所以以前画的线都会消失,fightboy()说得应该没错。
当然,照xioyoo()的程序中用Vector数组来保存所画的线,以便重绘是使用也是一种方法,选用哪种方法就要看你的程序的目的是什么了。
当然,照xioyoo()的程序中用Vector数组来保存所画的线,以便重绘是使用也是一种方法,选用哪种方法就要看你的程序的目的是什么了。
|
把g作为参数传到drawLines里去。在paintComponent(Graphics g)里调用,而不是在drawLines里调用paintComponent,paintComponent是自动调用的。