如何在Jpanel上画再继续画图

例如:如果我已经在一个Jpanel上画了一个圆,我还想画另一条直线。但不要再重画第一个圆,而是只画那条直线,画完后Jpanel上应该有一个圆和一条直线,应该怎么做?pai... 例如:如果我已经在一个Jpanel上画了一个圆,我还想画另一条直线。但不要再重画第一个圆,而是只画那条直线,画完后Jpanel上应该有一个圆和一条直线,应该怎么做?paint里 和事件监听器里应该怎么写?高手请解答!最好给个例子! 展开
 我来答
doggeral
2010-02-19
知道答主
回答量:21
采纳率:0%
帮助的人:0
展开全部
写的一个画图,写的不好,你参考一下
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.*;
import java.util.List;

import javax.swing.*;

public class huatu extends JFrame{

//画布
JPanel aa=new JPanel(null);
JPanel bb=new JPanel(null);
JPanel cc=new JPanel(null);
//一个装载图形的容器
List <shape> list=new ArrayList<shape>();
Iterator <shape> it=list.iterator();
//Panel a=(Panel) this.getContentPane();

//标记绘制那个图形
int flag=0;

//按钮
JButton line=new JButton("直线");
JButton cicle=new JButton("圆");
JButton rect=new JButton("方形");
JButton ov=new JButton("椭圆");
JButton pen=new JButton("铅笔");

//绘图的坐标
int startx=0;
int starty=0;
int endx=0;
int endy=0;

//判断是否是第一次绘图
boolean firsthui =false;

public void paint(Graphics g){
//g=aa.getGraphics();
it=list.iterator();

for(int i=0;i<list.size();i++){
//list.get(i);
shape a=it.next();
a.draw(g);

System.out.print("111");
}
//g.drawLine(10, 10, 100, 100);
}

//构造函数
public huatu(){
init();
}

//窗口初始化工作
public void init(){
//鼠标监听器
huatuMouseLisner hm=new huatuMouseLisner();
this.addMouseListener(hm);
this.add(aa);
//aa.setBounds(0, 0, 580, 590);
//
huatuMouseMotionListener hmo=new huatuMouseMotionListener();
this.addMouseMotionListener(hmo);
aa.add(bb);
bb.setBounds(0, 0, 70, 590);
//添加按钮
bb.add(line);
line.setBounds(0, 0, 70, 30);
bb.add(cicle);
cicle.setBounds(0, 30, 70,30);
bb.add(rect);
rect.setBounds(0, 60, 70, 30);
bb.add(ov);
ov.setBounds(0, 90, 70, 30);
bb.add(pen);
pen.setBounds(0, 120, 70, 30);

aa.add(cc);
cc.setBounds(70, 0, 510, 590);
//添加按钮响应
//画线
ActionListener l=new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
flag=1;
}

};
//画圆
ActionListener c=new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
flag=2;
}

};
//画方块
ActionListener r=new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
flag=3;
//System.out.println(flag);
}

};
//画椭圆
ActionListener o=new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
flag=4;
}

};

line.addActionListener(l);
cicle.addActionListener(c);
rect.addActionListener(r);
ov.addActionListener(o);

//this.add(aa);
//aa.setBounds(100, 0, 500, 500);
this.setSize(590,580);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//将窗体设置为大小不可改变
//this.setResizable(false);
this.setVisible(true);

}

class huatuMouseLisner implements MouseListener{

public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
firsthui=true;
startx=e.getX();
starty=e.getY();

}

public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
endx=e.getX();
endy=e.getY();
firsthui=false;
if(flag==1){
list.add(new line(startx,starty,endx,endy));
}
if(flag==2){
list.add(new cicle(startx,starty,endx,endy));
}
if(flag==3){
list.add(new rect(startx,starty,endx,endy));
}
if(flag==4){
list.add(new ov(startx,starty,endx,endy));
}
//System.out.println("111"+firsthui);
//Graphics g =aa.getGraphics();
//g.drawLine(startx, starty, endx, endy);
}

}

class huatuMouseMotionListener implements MouseMotionListener{

public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
Graphics g =getGraphics();
g.setXORMode(getBackground());
//System.out.println(firsthui);
switch (flag){
case 1: if(firsthui==true){

endx=e.getX();
endy=e.getY();
firsthui=false;
g.drawLine(startx, starty, endx, endy);
}
else{
g.drawLine(startx, starty, endx, endy);
endx=e.getX();
endy=e.getY();
g.drawLine(startx, starty, endx, endy);
}
break;
case 2: if(firsthui==true){

endx=e.getX();
endy=e.getY();
firsthui=false;
g.drawOval(startx, starty, Math.abs(endx-startx), Math.abs(endx-startx));
}
else{
g.drawOval(startx, starty, Math.abs(endx-startx), Math.abs(endx-startx));
endx=e.getX();
endy=e.getY();
g.drawOval(startx, starty, Math.abs(endx-startx), Math.abs(endx-startx));
}
break;
case 3:
if(firsthui==true){

endx=e.getX();
endy=e.getY();
firsthui=false;
g.drawRect(startx, starty, Math.abs(endx-startx), Math.abs(endy-starty));
}
else{
g.drawRect(startx, starty, Math.abs(endx-startx), Math.abs(endy-starty));
endx=e.getX();
endy=e.getY();
g.drawRect(startx, starty, Math.abs(endx-startx), Math.abs(endy-starty));
}
break;

case 4:
if(firsthui==true){
endx=e.getX();
endy=e.getY();
firsthui=false;
g.drawOval(startx, starty, Math.abs(endx-startx), Math.abs(endy-starty));
}
else{
g.drawOval(startx, starty, Math.abs(endx-startx), Math.abs(endy-starty));
endx=e.getX();
endy=e.getY();
g.drawOval(startx, starty, Math.abs(endx-startx), Math.abs(endy-starty));
}
break;

}

}

public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub

}

}
public static void main(String args[]){
huatu a=new huatu();

}

}

class shape {
public void draw(Graphics g){

}
}

class line extends shape{
int x,y,r,m;
public line(int i,int j,int k,int l){
x=i;
y=j;
r=k;
m=l;
}
public void draw(Graphics g){
g.drawLine(x, y, r, m);
}
}

class cicle extends shape{
int x,y,r;
public cicle(int i,int j,int k,int l){
x=i;
y=j;
r=Math.abs(k-i);
}
public void draw(Graphics g){
g.drawOval(x, y, r, r);
}
}

class ov extends shape{
int x,y,r,m;
public ov(int i,int j,int k,int l){
x=i;
y=j;
r=Math.abs(k-i);
m=Math.abs(l-j);
}
public void draw(Graphics g){
g.drawOval(x, y, r, m);
}
}

class rect extends shape{
int x,y,r1,r2;
public rect(int i,int j,int k,int l){
x=i;
y=j;
r1=Math.abs(k-i);
r2=Math.abs(l-j);
}
public void draw(Graphics g){
g.drawRect(x, y, r1, r2);
}
}
乁Freedom
2010-02-19 · TA获得超过2128个赞
知道小有建树答主
回答量:1580
采纳率:0%
帮助的人:1415万
展开全部
你是要做画图程序吧?
已经存在一个圆,再画一条线,其实在paint内部是这样的,你画了一个圆后应保存在一个集合中,如果再画一条线,那么也把它保存在集合中,paint要做的只是循环这个集合,把集合里的东西都画出来而已,它会先清空画板,然后再重新把集合里的东西全部画出来
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式