关于JAVA的Graphics2D画图的问题,详情见问题补充

这是代码:importjava.awt.Color;importjava.awt.Container;importjava.awt.Graphics;importjava... 这是代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

import javax.swing.JPanel;

class mainframe extends JFrame{
mainframe(){
super("简易画图工具");
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}

}

class canvas extends JPanel {
static Graphics2D g2;
static Point p1,p2;
static double x,y,a,b;
static int N=5;
public void paint(Graphics g){
super.paint(g);
g2=(Graphics2D)g;
g2.draw(new Ellipse2D.Double(10,10,10,10));
MouseAdapter ma=new MouseAdapter(){

public void mousePressed(MouseEvent e) {

p1=e.getPoint();
System.out.println(p1.x+" "+p1.y);
}

public void mouseReleased(MouseEvent e) {
p2=e.getPoint();
a=(double)(p2.x-p1.x);
b=(double)(p2.y-p1.y);
if(a>0&&b>0){
g2.draw(new Ellipse2D.Double((double)p1.x,(double)p1.y, a, b));
}
System.out.println(p2.x+" "+p2.y);
repaint();
}
};
addMouseListener(ma);
addMouseMotionListener(ma);

}
}

public class linshi {
static canvas ca=new canvas();
public static void main(String args[]){
mainframe mf=new mainframe();
Container c=mf.getContentPane();
c.setLayout(null);
c.add(ca);
ca.setBounds(50, 30,570,550);
ca.setBackground(Color.WHITE);
mf.setVisible(true);
}

}

我想实现通过鼠标按下和松开在窗口里画出一个圆,但是试了一下鼠标操作完成之后没有窗口没有变化,是不是重绘的问题,这个不太懂,求解答。
展开
 我来答
匿名用户
2013-11-17
展开全部
class DrawDemo extends JFrame{
DrawDemo(){
super("简易画图工具");
this.setSize(600,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);

Canvas ca=new Canvas();
Container c=this.getContentPane();
//c.setLayout(null);
c.add(ca);
//ca.setBounds(50, 30,570,550);
ca.setBackground(Color.WHITE);
this.setVisible(true);
}

public static void main(String args[]){
new DrawDemo();
}
}

class Canvas extends JPanel {
static Graphics2D g2;
static Point p1,p2;
static double x,y,a,b;
static int N=5;
public Canvas(){
MouseAdapter ma=new MouseAdapter(){
public void mousePressed(MouseEvent e) {
p1=e.getPoint();
System.out.println("P1:"+p1.x+" "+p1.y);
}

public void mouseReleased(MouseEvent e) {
p2=e.getPoint();
System.out.println("P2:"+p2.x+" "+p2.y);
repaint();
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}

public void paint(Graphics g){
super.paint(g);
g2=(Graphics2D)g;
g2.draw(new Ellipse2D.Double(10,10,10,10));
if(p1==null || p2==null){
return;
}
a=(double)(p2.x-p1.x);
b=(double)(p2.y-p1.y);
if(a>0&&b>0){
g2.draw(new Ellipse2D.Double((double)p1.x,(double)p1.y, a, b));
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式