下面是代码 我想要的效果是 画的圆只显示一下轨迹而不用画到面板上 帮忙看一下 应该在哪改!!
package awt;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class HuaTu implements MouseMotionListener, ActionListener,MouseListener
{
static int oldx,oldy,newx,newy;
public static void main(String arg[])
{
JFrame f = new JFrame("Paint");
Container p = f.getContentPane();
p.setLayout(new FlowLayout());
p.addMouseMotionListener(new HuaTu());
p.addMouseListener(new HuaTu());
f.setSize(200, 200);
f.setVisible(true); } public void actionPerformed(ActionEvent e)
{ } public void mouseDragged(MouseEvent e)
{
newx = e.getX();
newy = e.getY();
Container c = (Container)e.getSource();
Graphics g = c.getGraphics(); if(newx>oldx && newy>oldy)
{
g.drawOval(oldx,oldy,Math.abs(newx-oldx),Math.abs(newy-oldy));
}
else if(newx>oldx && newy<oldy)
{
g.drawOval(oldx,newy,Math.abs(newx-oldx),Math.abs(oldy-newy));
}
else if(newx<oldx && newy>oldy)
{
g.drawOval(newx,oldy,Math.abs(oldx-newx),Math.abs(newy-oldy));
}
else
{
g.drawOval(newx,newy,Math.abs(oldx-newx),Math.abs(oldy-newy));
}
} public void mouseMoved(MouseEvent e)
{ } public void mouseClicked(MouseEvent e)
{ }
public void mousePressed(MouseEvent e)
{
oldx = e.getX();
oldy = e.getY();
} public void mouseReleased(MouseEvent e)
{ } public void mouseEntered(MouseEvent e)
{ } public void mouseExited(MouseEvent e)
{ }
}
package awt;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class HuaTu implements MouseMotionListener, ActionListener,MouseListener
{
static int oldx,oldy,newx,newy;
public static void main(String arg[])
{
JFrame f = new JFrame("Paint");
Container p = f.getContentPane();
p.setLayout(new FlowLayout());
p.addMouseMotionListener(new HuaTu());
p.addMouseListener(new HuaTu());
f.setSize(200, 200);
f.setVisible(true); } public void actionPerformed(ActionEvent e)
{ } public void mouseDragged(MouseEvent e)
{
newx = e.getX();
newy = e.getY();
Container c = (Container)e.getSource();
Graphics g = c.getGraphics(); if(newx>oldx && newy>oldy)
{
g.drawOval(oldx,oldy,Math.abs(newx-oldx),Math.abs(newy-oldy));
}
else if(newx>oldx && newy<oldy)
{
g.drawOval(oldx,newy,Math.abs(newx-oldx),Math.abs(oldy-newy));
}
else if(newx<oldx && newy>oldy)
{
g.drawOval(newx,oldy,Math.abs(oldx-newx),Math.abs(newy-oldy));
}
else
{
g.drawOval(newx,newy,Math.abs(oldx-newx),Math.abs(oldy-newy));
}
} public void mouseMoved(MouseEvent e)
{ } public void mouseClicked(MouseEvent e)
{ }
public void mousePressed(MouseEvent e)
{
oldx = e.getX();
oldy = e.getY();
} public void mouseReleased(MouseEvent e)
{ } public void mouseEntered(MouseEvent e)
{ } public void mouseExited(MouseEvent e)
{ }
}
为了看到效果
mouseReleased事件里我又写了一遍的
你可以去掉
不过去掉后就没有图形了
import javax.swing.*;
import java.awt.event.*;public class HuaTu extends JFrame implements MouseMotionListener, ActionListener,MouseListener
{
static int oldx,oldy,newx,newy;
public static void main(String arg[])
{
JFrame f = new JFrame("Paint");
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container p = f.getContentPane();
p.setLayout(new FlowLayout());
p.addMouseMotionListener(new HuaTu());
p.addMouseListener(new HuaTu());
f.setSize(200, 200);
f.setVisible(true); } public void actionPerformed(ActionEvent e)
{ }
public void paintComponents(Graphics g) {
super.paintComponents(g);
} public void mouseDragged(MouseEvent e)
{
newx = e.getX();
newy = e.getY();
Container c = (Container)e.getSource();
Graphics g = c.getGraphics(); if(newx>oldx && newy>oldy)
{
g.drawOval(oldx,oldy,Math.abs(newx-oldx),Math.abs(newy-oldy));
}
else if(newx>oldx && newy<oldy)
{
g.drawOval(oldx,newy,Math.abs(newx-oldx),Math.abs(oldy-newy));
}
else if(newx<oldx && newy>oldy)
{
g.drawOval(newx,oldy,Math.abs(oldx-newx),Math.abs(newy-oldy));
}
else
{
g.drawOval(newx,newy,Math.abs(oldx-newx),Math.abs(oldy-newy));
}
c.repaint();
} public void mouseMoved(MouseEvent e)
{ } public void mouseClicked(MouseEvent e)
{ }
public void mousePressed(MouseEvent e)
{
oldx = e.getX();
oldy = e.getY();
} public void mouseReleased(MouseEvent e)
{
newx = e.getX();
newy = e.getY();
Container c = (Container)e.getSource();
Graphics g = c.getGraphics(); if(newx>oldx && newy>oldy)
{
g.drawOval(oldx,oldy,Math.abs(newx-oldx),Math.abs(newy-oldy));
}
else if(newx>oldx && newy<oldy)
{
g.drawOval(oldx,newy,Math.abs(newx-oldx),Math.abs(oldy-newy));
}
else if(newx<oldx && newy>oldy)
{
g.drawOval(newx,oldy,Math.abs(oldx-newx),Math.abs(newy-oldy));
}
else
{
g.drawOval(newx,newy,Math.abs(oldx-newx),Math.abs(oldy-newy));
} } public void mouseEntered(MouseEvent e)
{ } public void mouseExited(MouseEvent e)
{ }
}
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.util.Vector;import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;public class Frame1 extends JFrame
implements MouseMotionListener,
ActionListener,
MouseListener{
/**
*
*/
private static final long serialVersionUID = 1L;
static int oldx,oldy,newx,newy;
JButton b=new JButton("button1");
ButtonGroup group = new ButtonGroup();
JRadioButton jrb1=new JRadioButton("yuan");
JRadioButton jrb2=new JRadioButton("fang");
boolean blnAddTemp=false;
Vector<Shape> v=new Vector<Shape>();
public Frame1(){
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
c.add(b);
jrb1.setSelected(true);
group.add(jrb1);
group.add(jrb2);
c.add(jrb1);
c.add(jrb2);
this.setSize(600,400);
this.addWindowListener(new java.awt.event.WindowAdapter(){
public void windowClosed(WindowEvent e){
System.exit(0);
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
v.clear();
repaint();
}
});
this.addComponentListener(new ComponentListener(){ public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
} public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
} public void componentResized(ComponentEvent e) {
// TODO Auto-generated method stub
Container c = (Container)e.getSource();
c.repaint();
} public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
} );
this.addMouseListener(this);
this.addMouseMotionListener(this);
//this.setVisible(true);
}
public void paintComponents(Graphics g){
super.paintComponents(g);
}
public void paint(Graphics g)
{
super.paint(g);
Shape s;
for(int i=0;i<v.size();i++){
s=(Shape)v.get(i);
s.draw(g);
}
//g.drawArc(100, 200, 300, 400, 500, 600);
}
public static void main(String[] args){
Frame1 f=new Frame1();
f.setVisible(true);
} public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
if(blnAddTemp)
{
if( v.size()>0)
{
v.remove(v.size()-1);
}
blnAddTemp=false;
}
Shape r;
newx = e.getX();
newy = e.getY();
Container c = (Container)e.getSource();
if(newx>oldx && newy>oldy)
{
r= createShape(oldx,oldy,Math.abs(newx-oldx),Math.abs(newy-oldy));
}
else if(newx>oldx && newy<oldy)
{
r= createShape(oldx,newy,Math.abs(newx-oldx),Math.abs(oldy-newy));
}
else if(newx<oldx && newy>oldy)
{
r= createShape(newx,oldy,Math.abs(oldx-newx),Math.abs(newy-oldy));
}
else
{
r= createShape(newx,newy,Math.abs(oldx-newx),Math.abs(oldy-newy));
}
//System.out.println("asddddddddd");
v.add(r);
blnAddTemp=true;
c.repaint();
} public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
} public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
} 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
oldx = e.getX();
oldy = e.getY();
} public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
if(blnAddTemp)
{
if( v.size()>0)
{
v.remove(v.size()-1);
}
blnAddTemp=false;
}
Shape r;
newx = e.getX();
newy = e.getY();
Container c = (Container)e.getSource();
if(newx>oldx && newy>oldy)
{
r= createShape(oldx,oldy,Math.abs(newx-oldx),Math.abs(newy-oldy));
}
else if(newx>oldx && newy<oldy)
{
r= createShape(oldx,newy,Math.abs(newx-oldx),Math.abs(oldy-newy));
}
else if(newx<oldx && newy>oldy)
{
r= createShape(newx,oldy,Math.abs(oldx-newx),Math.abs(newy-oldy));
}
else
{
r= createShape(newx,newy,Math.abs(oldx-newx),Math.abs(oldy-newy));
}
v.add(r);
c.repaint();
}
public Shape createShape(int x,int y,int w,int h){
Shape s=null;
if(jrb1.getSelectedObjects()!=null){
s=new Rect(x,y,w,h);
}else{
s=new Fang(x,y,w,h);
}
return s;
}
}
package p1;import java.awt.Graphics;public class Rect implements Shape{ private int x,y,w,h;
public Rect(int x,int y,int w,int h){
this.x=x;
this.y=y;
this.w=w;
this.h=h;
}
public void draw(Graphics g) {
// TODO Auto-generated method stub
g.drawOval(x, y, w, h);
}
}package p1;import java.awt.Graphics;public interface Shape {
public abstract void draw(Graphics g);
}
package p1;import java.awt.Graphics;public class Fang implements Shape{ private int x,y,w,h;
public Fang(int x,int y,int w,int h){
this.x=x;
this.y=y;
this.w=w;
this.h=h;
}
public void draw(Graphics g) {
// TODO Auto-generated method stub
g.draw3DRect(x, y, w, h,true);
}
}
这个是书上的例子/**
* @(#)Painter.java
*
*
* @author Moon
* @version 1.00 2006/10/5
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Painter extends JFrame implements MouseListener, MouseMotionListener{
int startX, startY, endX, endY;
public Painter() {
Container contentPane = getContentPane();
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
setSize(400, 400);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
repaint();
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.drawLine(startX, startY, endX, endY);
}
public static void main(String[] args) {
new Painter();
}
}