java 在已有图形上用鼠标画图
如何不让界面在鼠标第一次点击时自动刷新以至于第一次点击所画的的图形被清掉!原题:使用画直线,我已部分实现,关键是图形处理!代码太长,发不了,完整代码找我代码://Unit...
如何不让界面在 鼠标第一次点击时自动刷新 以至于第一次点击所画的的图形被清掉!
原题:使用画直线,我已部分实现,关键是图形处理!
代码太长,发不了,完整代码找我
代码:
//Units.unit是一固定值!
public class selfpanel extends JPanel{
private int x1,y1,x2,y2;
public int Xaa,Xbb,Yaa,Ybb;
private JLabel status;
private JPanel p1 ;
private JPanel p2 ;
private JPanel p3 ;
private JButton clearpane;
private JTextArea JTextArea1;
Color color;
private int mark;
public selfpanel()
{
color=Color.blue;
x1=50;y1=50;x2=300;y2=300;
mark=1;
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
status = new JLabel();
clearpane = new JButton("clear");
JTextArea1 = new JTextArea(10,15);
p1.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout(FlowLayout.LEFT ));
p2.add(status);
p3.setLayout(new BorderLayout());
p3.add(clearpane,BorderLayout.NORTH);
p3.add(JTextArea1,BorderLayout.CENTER);
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.SOUTH);
this.add(p3, BorderLayout.EAST);
addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent event)
{
int i=event.getX()/Units.unit-1;
int j=event.getY()/Units.unit-1;
if(event.getX()<25||event.getY()<25||event.getX()>300||event.getY()>300)
{ i=0;j=0;}
status.setText(" "+ i + "," + j );
}
}
);
addMouseListener(
new MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
if(event.getX()>=50&&event.getY()>=50&&event.getX()<=300&&event.getY()<=300&&mark<4)
{
drawfangkuai(event.getX(),event.getY());
mark = mark +1;
if(mark==3)
{
Xaa = event.getX(); Yaa = event.getY();
}
if(mark==4)
{
Xbb = event.getX(); Ybb = event.getY();
drawpoint();
mark = 2;
}
}
}
}
);
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(color);
drawLine(g2,x1,y1,x2,y2);
}
void drawLine(Graphics2D g,int x11,int y11,int x22,int y22)
{//画10*10网格 }
public void repaint()
{
Graphics g = getGraphics();
paint(g);
mark = 2;
}
public void drawpoint()
{
//根据Bresenham算法找到坐标,调用drawfangkuai画方块
}
void drawfangkuai(int startX,int startY)
{
//根据坐标画方块
}
出新问题了,每次触发点击鼠标事件后整个面板会刷新,这样,就不能在网格上连续画几条直线了。
问题:怎样让刷新不把以前画的图形清掉?(区域小方格的刷新,我已经知道了,有没有更高级的点的方法?) 展开
原题:使用画直线,我已部分实现,关键是图形处理!
代码太长,发不了,完整代码找我
代码:
//Units.unit是一固定值!
public class selfpanel extends JPanel{
private int x1,y1,x2,y2;
public int Xaa,Xbb,Yaa,Ybb;
private JLabel status;
private JPanel p1 ;
private JPanel p2 ;
private JPanel p3 ;
private JButton clearpane;
private JTextArea JTextArea1;
Color color;
private int mark;
public selfpanel()
{
color=Color.blue;
x1=50;y1=50;x2=300;y2=300;
mark=1;
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
status = new JLabel();
clearpane = new JButton("clear");
JTextArea1 = new JTextArea(10,15);
p1.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout(FlowLayout.LEFT ));
p2.add(status);
p3.setLayout(new BorderLayout());
p3.add(clearpane,BorderLayout.NORTH);
p3.add(JTextArea1,BorderLayout.CENTER);
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.SOUTH);
this.add(p3, BorderLayout.EAST);
addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent event)
{
int i=event.getX()/Units.unit-1;
int j=event.getY()/Units.unit-1;
if(event.getX()<25||event.getY()<25||event.getX()>300||event.getY()>300)
{ i=0;j=0;}
status.setText(" "+ i + "," + j );
}
}
);
addMouseListener(
new MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
if(event.getX()>=50&&event.getY()>=50&&event.getX()<=300&&event.getY()<=300&&mark<4)
{
drawfangkuai(event.getX(),event.getY());
mark = mark +1;
if(mark==3)
{
Xaa = event.getX(); Yaa = event.getY();
}
if(mark==4)
{
Xbb = event.getX(); Ybb = event.getY();
drawpoint();
mark = 2;
}
}
}
}
);
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(color);
drawLine(g2,x1,y1,x2,y2);
}
void drawLine(Graphics2D g,int x11,int y11,int x22,int y22)
{//画10*10网格 }
public void repaint()
{
Graphics g = getGraphics();
paint(g);
mark = 2;
}
public void drawpoint()
{
//根据Bresenham算法找到坐标,调用drawfangkuai画方块
}
void drawfangkuai(int startX,int startY)
{
//根据坐标画方块
}
出新问题了,每次触发点击鼠标事件后整个面板会刷新,这样,就不能在网格上连续画几条直线了。
问题:怎样让刷新不把以前画的图形清掉?(区域小方格的刷新,我已经知道了,有没有更高级的点的方法?) 展开
展开全部
这段代码给你参考下,实现功能:不让界面在 鼠标第一次点击时自动刷新 以至于第一次点击所画的的图形被清掉!至于画直线的,就需要你自己修改下代码,这个不难。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class freedraw1 extends Applet implements MouseMotionListener
{
int x,y;
public void init()
{
setBackground(Color.cyan);
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.red);
BasicStroke bs1=new BasicStroke(4);
g2d.setStroke(bs1);
Line2D obj1=new Line2D.Double(x,y,x,y);
g2d.draw(obj1);
}
public void mouseDragged(MouseEvent e)
{
x=(int)e.getX();
y=(int)e.getY();
repaint();
}
public void update(Graphics g)
{
paint(g);
}
public void mouseMoved(MouseEvent e)
{
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class freedraw1 extends Applet implements MouseMotionListener
{
int x,y;
public void init()
{
setBackground(Color.cyan);
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.red);
BasicStroke bs1=new BasicStroke(4);
g2d.setStroke(bs1);
Line2D obj1=new Line2D.Double(x,y,x,y);
g2d.draw(obj1);
}
public void mouseDragged(MouseEvent e)
{
x=(int)e.getX();
y=(int)e.getY();
repaint();
}
public void update(Graphics g)
{
paint(g);
}
public void mouseMoved(MouseEvent e)
{
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询