java 画图问题

谁能给我个简单的例子,教我怎么在JPanel上画一个圆,再画一个圆,而前一个圆还在我是个菜鸟,求各位高手帮忙!!!是要用到鼠标监听的那种就像在画板上画画,以拖拽就有个图形... 谁能给我个简单的例子,教我怎么在JPanel上画一个圆,再画一个圆,而前一个圆还在

我是个菜鸟,求各位高手帮忙!!!
是要用到鼠标监听的那种 就像在画板上画画,以拖拽就有个图形,再一画还有一个图形而第一个不消失
怪我没说明白,各位大侠救命啊
展开
 我来答
fuhaiwei
推荐于2016-02-07 · TA获得超过3409个赞
知道大有可为答主
回答量:974
采纳率:75%
帮助的人:1393万
展开全部
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawOvel extends JFrame {
class Mouse1 extends MouseAdapter {
public void mousePressed(MouseEvent e) {
start_x = e.getX();
start_y = e.getY();
}

public void mouseReleased(MouseEvent e) {
int minX = Math.min(start_x, e.getX());
int minY = Math.min(start_y, e.getY());
int maxX = Math.max(start_x, e.getX());
int maxY = Math.max(start_y, e.getY());
g_image.drawOval(minX, minY, maxX - minX, maxY - minY);
g_jpanel.drawImage(image, 0, 0, null);
}
}

class Mouse2 extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
int minX = Math.min(start_x, e.getX());
int minY = Math.min(start_y, e.getY());
int maxX = Math.max(start_x, e.getX());
int maxY = Math.max(start_y, e.getY());
g_jpanel.drawImage(image, 0, 0, null);
g_jpanel.drawOval(minX, minY, maxX - minX, maxY - minY);
}
}

public DrawOvel() {
super();
setSize(600, 600);
jpanel = new JPanel();
add(jpanel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
g_jpanel = jpanel.getGraphics();
image = jpanel.createImage(jpanel.getWidth(), jpanel.getHeight());
g_image = image.getGraphics();
jpanel.addMouseListener(new Mouse1());
jpanel.addMouseMotionListener(new Mouse2());
}

JPanel jpanel;
Graphics g_jpanel, g_image;
Image image;
int start_x, start_y;

public static void main(String[] args) {
DrawOvel c = new DrawOvel();
}
}
若无其事NIE
2010-07-29 · TA获得超过202个赞
知道小有建树答主
回答量:229
采纳率:0%
帮助的人:174万
展开全部
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test2 extends JPanel{

/**
*
*/
static int num = 0;
private static final long serialVersionUID = 1L;

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf = new JFrame("两个圆");
jf.setSize(400, 400);
jf.setVisible(true);
Test2 t2 = new Test2();
jf.add(t2);
for(; num<2; num++){
t2.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
if(num == 0) g.drawOval(200, 200, 100, 100);
if(num == 1) g.drawOval(200, 200, 50, 50);
}

}

刚做的,可以实现了。。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式