java 中用鼠标画直线,椭圆,矩形的时候。怎么才能使三种图形一起显示出来?用链表的地方可能有缺陷。

下面代码职能实现一种图形的复数显示,3种图形一起显示的话,怎么修改能?请教下。importjava.awt.*;importjava.awt.geom.*;importj... 下面代码职能实现一种图形的复数显示,3种图形一起显示的话,怎么修改能?请教下。

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Test{
public static void main(String[] args){
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFrame extends JFrame implements ActionListener{
MyPanel panel = new MyPanel();

JToolBar t;
JButton obj1;
JButton obj2;
JButton obj3;
public MyFrame(){
obj1 = new JButton("Line");
obj2 = new JButton("yuan");
obj3 = new JButton("juxing");
t = new JToolBar();
obj1.addActionListener(this);
obj2.addActionListener(this);
obj3.addActionListener(this);
t = new JToolBar();
t.add(obj1);
t.add(obj2);
t.add(obj3);
ButtonGroup buttonGroup = new ButtonGroup();
setTitle("DrawTest");
setSize(W,H);
Container contentPane = getContentPane();
contentPane.add(t,BorderLayout.NORTH);
contentPane.add(panel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==obj1)
panel.shape=0;//直线
if(e.getSource()==obj2)
panel.shape=1;//円
if(e.getSource()==obj3)
panel.shape=2;//长方形
}

public static final int W = 400;
public static final int H = 400;
}
class MyPanel extends JPanel implements MouseListener{

private ArrayList<Point> pointList = new ArrayList<Point>();
int shape = -1;
int x1 = 0,y1 = 0;
int x2 = 0,y2 = 0;
int i = 0;
MyPanel(){
addMouseListener(this);
}
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
pointList.add(new Point(x1,y1));
}

public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
pointList.add(new Point(x2,y2));
repaint();

}

public void mouseClicked(MouseEvent e){
}

public void mouseMoved(MouseEvent e){
}

public void mouseEntered(MouseEvent e){
}

public void mouseExited(MouseEvent e){
}

public void paintComponent(Graphics g){
super.paintComponent(g);
System.out.println(pointList.size());
for(int i =0;i < pointList.size()-1;i=i+2){
switch(shape){
case 0:
Point p1 = pointList.get(i);
Point p2 = pointList.get(i+1);
g.drawLine(p1.x,p1.y,p2.x,p2.y);
break;
case 1:
p1 = pointList.get(i);
p2 = pointList.get(i+1);
int width1 = p2.x - p1.x;
int height1 = p2.y - p1.y;
g.drawOval(p1.x,p1.y,width1,height1);
break;
case 2:
p1 = pointList.get(i);
p2 = pointList.get(i+1);
int width2 = p2.x - p1.x;
int height2 = p2.y - p1.y;
g.drawRect(p1.x,p1.y,width2,height2);
break;
default:
System.out.println("please once agan!");
break;
}
}
}
}
展开
 我来答
紫薇参星
科技发烧友

2013-01-18 · 有一些普通的科技小锦囊
知道大有可为答主
回答量:5983
采纳率:92%
帮助的人:3552万
展开全部
你的程序我看了一下,要想三个图形分别显示出来,需要在actionPerformed函数中加一句panel.repaint();。如果想让三个图形一起显示,还需要去掉switch语句中,case 0:和case 1:中的break;语句。
完整的程序如下:(改动的地方见注释)
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Test{
public static void main(String[] args){
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFrame extends JFrame implements ActionListener{
MyPanel panel = new MyPanel();
JToolBar t;
JButton obj1;
JButton obj2;
JButton obj3;
public MyFrame(){
obj1 = new JButton("Line");
obj2 = new JButton("yuan");
obj3 = new JButton("juxing");
t = new JToolBar();
obj1.addActionListener(this);
obj2.addActionListener(this);
obj3.addActionListener(this);
t = new JToolBar();
t.add(obj1);
t.add(obj2);
t.add(obj3);
ButtonGroup buttonGroup = new ButtonGroup();
setTitle("DrawTest");
setSize(W,H);
Container contentPane = getContentPane();
contentPane.add(t,BorderLayout.NORTH);
contentPane.add(panel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==obj1)
panel.shape=0;//直线
if(e.getSource()==obj2)
panel.shape=1;//圆
if(e.getSource()==obj3)
panel.shape=2;//长方形
panel.repaint(); //这里加一句
}
public static final int W = 400;
public static final int H = 400;
}
class MyPanel extends JPanel implements MouseListener{
private ArrayList<Point> pointList = new ArrayList<Point>();
int shape = -1;
int x1 = 0,y1 = 0;
int x2 = 0,y2 = 0;
int i = 0;
MyPanel(){
addMouseListener(this);
}
public void mousePressed(MouseEvent e){
x1 = e.getX();y1 = e.getY();
pointList.add(new Point(x1,y1));
}
public void mouseReleased(MouseEvent e){
x2 = e.getX();y2 = e.getY();
pointList.add(new Point(x2,y2));
repaint();
}
public void mouseClicked(MouseEvent e){ }
public void mouseMoved(MouseEvent e){ }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void paintComponent(Graphics g){
super.paintComponent(g);
System.out.println(pointList.size());
for(int i =0;i < pointList.size()-1;i=i+2){
switch(shape){
case 0:Point p1 = pointList.get(i);
Point p2 = pointList.get(i+1);
g.drawLine(p1.x,p1.y,p2.x,p2.y);
break; //如果想让三个图形一起显示去掉这里的break;语句
case 1:p1 = pointList.get(i);
p2 = pointList.get(i+1);
int width1 = p2.x - p1.x;
int height1 = p2.y - p1.y;
g.drawOval(p1.x,p1.y,width1,height1);
break; //如果想让三个图形一起显示去掉这里的break;语句
case 2:p1 = pointList.get(i);
p2 = pointList.get(i+1);
int width2 = p2.x - p1.x;
int height2 = p2.y - p1.y;
g.drawRect(p1.x,p1.y,width2,height2);
break;
default: System.out.println("please once agan!");
break;
}
}
}
}
追问
谢谢你的回答,不过我想要的是,三个图形分别自由的画在一起。分散开的那种模型。彼此互不影响。
追答
按照你的要求,改完的程序如下:(新建了一个StylePoint类,你看看吧)
由于程序超长,所以只贴上改动的部分MyPanel类(mouseListener的几个要重写的函数不变,没有贴上来)和新建的类StylePoint,其它部分不变。
......//省略
class StylePoint {
int style;
Point point=new Point();

}
class MyPanel extends JPanel implements MouseListener{
private ArrayList pointList = new ArrayList();
int shape = -1;
int x1 = 0,y1 = 0;
int x2 = 0,y2 = 0;
int i = 0;
MyPanel(){
addMouseListener(this);
}
public void mousePressed(MouseEvent e){
x1 = e.getX();y1 = e.getY();
StylePoint sp1=new StylePoint();
sp1.style=shape;
sp1.point.x=x1;
sp1.point.y=y1;
pointList.add(sp1);
}
public void mouseReleased(MouseEvent e){
x2 = e.getX();y2 = e.getY();
StylePoint sp2=new StylePoint();
sp2.style=shape;
sp2.point.x=x2;
sp2.point.y=y2;
pointList.add(sp2);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
System.out.println(pointList.size());
for(int i =0;i < pointList.size()-1;i=i+2){
int style=pointList.get(i).style;
switch(style){
case 0:Point p1 = pointList.get(i).point;
Point p2 = pointList.get(i+1).point;
g.drawLine(p1.x,p1.y,p2.x,p2.y);
break;
case 1:p1 = pointList.get(i).point;
p2 = pointList.get(i+1).point;
int width1 = p2.x - p1.x;
int height1 = p2.y - p1.y;
g.drawOval(p1.x,p1.y,width1,height1);
break;
case 2:p1 = pointList.get(i).point;
p2 = pointList.get(i+1).point;
int width2 = p2.x - p1.x;
int height2 = p2.y - p1.y;
g.drawRect(p1.x,p1.y,width2,height2);
break;
default: System.out.println("please once agan!");
break;
}
}
}
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友4c08a42
2013-01-17 · TA获得超过258个赞
知道小有建树答主
回答量:158
采纳率:0%
帮助的人:78.2万
展开全部
逻辑错误。你自己改吧。

每个图形,你只用pointList记录了坐标,没有记录它的shape。
每次repaint,你都用当前shape去重绘。
追问
shape怎么和坐标一起记录呢?
给个简单的例子,能容易明白点。谢谢
追答
方法一:

private ArrayList pointList = new ArrayList();
这是你的pointList。每2个点是一对。
你再定义一个数组存放每个图元的形状,每个元素的值是0、 1、 2

只是下标不一致

方法二:干脆定义一个图元类,每个图元有两个点和一个shape
class Graph{//
Point p1;
Point p2;
int shape;

}
//再用Graph来定义一个图元线性表

private ArrayList pointList = new ArrayList();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式