java 画图问题
谁能给我个简单的例子,教我怎么在JPanel上画一个圆,再画一个圆,而前一个圆还在我是个菜鸟,求各位高手帮忙!!!是要用到鼠标监听的那种就像在画板上画画,以拖拽就有个图形...
谁能给我个简单的例子,教我怎么在JPanel上画一个圆,再画一个圆,而前一个圆还在
我是个菜鸟,求各位高手帮忙!!!
是要用到鼠标监听的那种 就像在画板上画画,以拖拽就有个图形,再一画还有一个图形而第一个不消失
怪我没说明白,各位大侠救命啊 展开
我是个菜鸟,求各位高手帮忙!!!
是要用到鼠标监听的那种 就像在画板上画画,以拖拽就有个图形,再一画还有一个图形而第一个不消失
怪我没说明白,各位大侠救命啊 展开
2个回答
展开全部
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();
}
}
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();
}
}
展开全部
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);
}
}
刚做的,可以实现了。。。
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);
}
}
刚做的,可以实现了。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询