| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1288 人关注过本帖
标题:我碰到问题了!!!请大家救急啊!!!
只看楼主 加入收藏
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
结帖率:100%
收藏
 问题点数:0 回复次数:6 
我碰到问题了!!!请大家救急啊!!!

代码太长铁不出来,下面就上传文件了。大家把文件下载下来,就可以运行了。

这个程序为了实现画板的功能,至少要实现所有按钮的功能。

就是当你按 line Button, 你就可以通过鼠标先按住左键,拖动左键到某一位置,释放鼠标,即画出了一条直线。同样的你可以画出矩形, 椭圆, 圆。 另外当你按下 移动或消除按钮,则可以对选中的图形予以移动或消除,需要移动时,先将鼠标置于某个或多个图形区域里面,鼠标左键点击之,这样就表示为对那些图形予以选中,然后拖动鼠标,这些被选中的图形将随之移动,如要消除某个或多个图形,则将鼠标置于其区域之中,右键就可消除他们。

文字输入的实现方式是这样:先按下 text Button, 再按下左键,确定文字的起始位置,然后敲键盘,直到按回车,文字输入结束。文字应该一边输入一边显示,如按退格间,同样在显示时,也应有退格。

我现在碰到的问题是1)文字不能显示,当我按下text Button,之后,keyListener 就没反映了。

也就是如果一按鼠标, keyListener 就没反映了。

2)移动时,图形不能爽快跟着跑。

大家帮着看看阿。 我也请大家救急了。

搜索更多相关主题的帖子: 鼠标 图形 左键 拖动 
2004-06-12 00:13
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
我上传的文件怎么没看见?

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-06-12 00:15
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

文件显示不了,也上传不了,就分段贴了

程序代码:

package zeichnen;

/** * Date: 2004-5-8 * author: jellen * kaihua wu * Purpose: This is a simple drawing program. */

import javax.swing.*; import java.awt.*; import java.io.*; import java.util.*; import java.awt.event.*; import java.awt.geom.*;

class ProgrammZustand { // programm zustand public static final int SELECT = 1; public static final int LINE = 2; public static final int RECTANGLE = 3; public static final int ELLIPSE = 4; public static final int CIRCLE = 5; public static final int TEXT = 6; public static final int MOVE_OR_ERASE = 7;

// zus鋞chlicher symbol public static final int RASTER = 8; public static final int PFEIL = 9; public static final int KREISBOGEN = 10;

public static String text; protected int startX; protected int startY; protected int endX; protected int endY; protected int zustand; protected boolean isStored; protected boolean isSelected; // protected boolean isMoveOrEraseLocked; protected boolean isFill; protected Color color;

protected static final Color DEFAULTCOLOR_AS_BLACK = Color.BLACK;

public ProgrammZustand() { isStored = false; isSelected = false; // isMoveOrEraseLocked = true; // kann weder bewegen noch l鰏chen isFill = false; color = DEFAULTCOLOR_AS_BLACK; }

public ProgrammZustand(int sx, int sy, int ex, int ey) { isStored = false; isSelected = false; // isMoveOrEraseLocked = true; // kann weder bewegen noch l鰏chen isFill = false; color = DEFAULTCOLOR_AS_BLACK;

startX = sx; startY = sy; endX = ex; endY = ey; }

public void setStart(int x, int y) { startX = x; startY = y; } public void setEnd(int x, int y) { endX = x; endY = y; } public Point2D getStart() { Point2D point = new Point2D.Double(startX, startY); return point; } public Point2D getEnd() { Point2D point = new Point2D.Double(endX, endY); return point; } public int maxX() { int max_X = (startX >= endX? startX : endX); return max_X; } public int minX() { int min_X = (startX <= endX? startX : endX); return min_X; } public int maxY() { int max_Y = (startY >= endY? startY : endY); return max_Y; } public int minY() { int min_Y = (startY <= endY? startY : endY); return min_Y; }

public boolean isContain(int x, int y) { if(x >= minX() && x <= maxX() && y >= minY() && y <= maxY()) return true; else return false; }

public void writeData(PrintWriter out)throws IOException { int c = color.getRGB(); int fill;

if( isFill ) fill = 1; else fill = 0;

out.println(zustand + \"|\" + c + \"|\" + fill + \"|\" + startX + \"|\" + startY + \"|\" + endX + \"|\" + endY); }

public void readData(BufferedReader in)throws IOException { String s = in.readLine(); StringTokenizer t = new StringTokenizer(s, \"|\");

zustand = Integer.parseInt(t.nextToken()); color = new Color(Integer.parseInt(t.nextToken())); int fill = Integer.parseInt(t.nextToken()); if(fill == 1) isFill = true; else isFill = false; startX = Integer.parseInt(t.nextToken()); startY = Integer.parseInt(t.nextToken()); endX = Integer.parseInt(t.nextToken()); endY = Integer.parseInt(t.nextToken()); }

/* public static void main(String[] args) { // Line line = new Line(1, 2, 3, 4);

// System.out.print(\"The first: \"); // System.out.println(line.getStart() + \", \" + line.getEnd());

try { PrintWriter out = new PrintWriter(new FileWriter(\"jellen.txt\")); // line.writeData(out); out.close();

BufferedReader in = new BufferedReader(new FileReader(\"jellen.txt\")); Polygon s = new Polygon(); s.readData(in); in.close();

System.out.print(\"The second: \"); System.out.println(s.getStart() + \", \" + s.getEnd()); } catch(IOException e) { e.printStackTrace(); } }*/ } //------------ Class Line ------------ class Line extends ProgrammZustand { public Line() { super(); zustand = LINE; } public Line(int sx, int sy, int ex, int ey) { super(sx, sy, ex, ey); zustand = LINE; } public boolean isContain(int x, int y) { double c = Math.sqrt((endX-startX)*(endX-startX)+(endY-startY)*(endY-startY)); double c1 = Math.sqrt((x-startX)*(x-startX)+(y-startY)*(y-startY)); double c2 = Math.sqrt((endX-x)*(endX-x)+(endY-y)*(endY-y));

if(c1+c2-c <= 2) { isSelected = true; return true; } else { isSelected = false; return false; } } } //------------ Class Rectangle ------------ class Rectangle extends ProgrammZustand { public Rectangle() { super(); zustand = RECTANGLE; } public Rectangle(int sx, int sy, int ex, int ey) { super(sx, sy, ex, ey); zustand = RECTANGLE; } public boolean isContain(int x, int y) { if(x >= minX() && x <= maxX() && y >= minY() && y <= maxY()) { isSelected = true; return true; } else { isSelected = false; return false; } } } //------------ Class Ellipse ------------ class Ellipse extends ProgrammZustand { private double dx; // MittelPunkt private double dy; private double a; // L鋘ge a private double b; // L鋘ge b public Ellipse() { super(); zustand = ELLIPSE; } public Ellipse(int sx, int sy, int ex, int ey) { super(sx, sy, ex, ey); zustand = ELLIPSE; } public boolean isContain(int x, int y) { dx = (double) (startX+endX) / 2; dy = (double) (startY+endY) / 2; a = (double) (maxX()-minX()) / 2; b = (double) (maxY()-minY()) / 2;

double x1 = (x-dx)*(x-dx) / (a*a); double y1 = (y-dy)*(y-dy) / (b*b); if(x1+y1 <= 1) { isSelected = true; return true; } else { isSelected = false; return false; }

} } //------------ Class Circle ------------ class Circle extends ProgrammZustand { private double dx; // MittelPunkt private double dy; private double r; // Radius

public Circle() { super(); zustand = CIRCLE; } public Circle(int sx, int sy, int ex, int ey) { super(sx, sy, ex, ey); zustand = CIRCLE; } public boolean isContain(int x, int y) { dx = (double) (startX+endX) / 2; dy = (double) (startY+endY) / 2; r = 0.5*Math.sqrt(Math.pow((endX-startX), 2));

if((x-dx)*(x-dx)+(y-dy)*(y-dy)<=r*r) //und temp.stored = false { isSelected = true; return true; } else { isSelected = false; return false; }

} } //------------ Class Text ------------ class Text extends ProgrammZustand { static String myText = \" \"; public Text() { super(); super.text = new String(); zustand = TEXT; } public Text(int sx, int sy, String theText) { super.startX = sx; super.startY = sy; super.isSelected = false; super.isFill = false; super.color = Color.BLACK; myText = theText; zustand = TEXT; } public void setText(String theText) { // super.startX = sx; // super.startY = sy; super.isSelected = false; super.isFill = false; super.color = Color.BLACK; myText = theText; zustand = TEXT; } } //------------ Class Raster ------------ class Raster extends ProgrammZustand { static final int ABSTAND = 20; public Raster() { super(); zustand = RASTER; } }

//continued


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-06-12 00:26
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

程序代码:

//------------ Class ButtonPanel ------------ class ButtonPanel extends JPanel { static int cnt = 0; public ButtonPanel() { JButton lineButton = new JButton(\"line\"); JButton rectangleButton = new JButton(\"rectangle\"); JButton ellipseButton = new JButton(\"ellipse\"); JButton circleButton = new JButton(\"circle\"); JButton textButton = new JButton(\"text\"); JButton move_or_eraseButton = new JButton(\"move_or_erase\"); JButton rasterButton = new JButton(\"raster\"); JButton colorButton = new JButton(\"color\"); add(lineButton); add(rectangleButton); add(ellipseButton); add(circleButton); add(textButton); add(move_or_eraseButton); add(rasterButton); add(colorButton); // lineButton: lineButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.lineButtonActived = true; PaintPanel.rectangleButtonActived = false; PaintPanel.ellipseButtonActived = false; PaintPanel.circleButtonActived = false; PaintPanel.textButtonActived = false; PaintPanel.move_or_eraseButtonActived = false; //PaintPanel.moveEnabled = false; PaintPanel.currentShape = ProgrammZustand.LINE; } }); // rectangleButton: rectangleButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.lineButtonActived = false; PaintPanel.rectangleButtonActived = true; PaintPanel.ellipseButtonActived = false; PaintPanel.circleButtonActived = false; PaintPanel.textButtonActived = false; PaintPanel.move_or_eraseButtonActived = false; PaintPanel.currentShape = ProgrammZustand.RECTANGLE; } }); // ellipseButton: ellipseButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.lineButtonActived = false; PaintPanel.rectangleButtonActived = false; PaintPanel.ellipseButtonActived = true; PaintPanel.circleButtonActived = false; PaintPanel.textButtonActived = false; PaintPanel.move_or_eraseButtonActived = false; PaintPanel.currentShape = ProgrammZustand.ELLIPSE; } }); // circleButton: circleButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.lineButtonActived = false; PaintPanel.rectangleButtonActived = false; PaintPanel.ellipseButtonActived = false; PaintPanel.circleButtonActived = true; PaintPanel.textButtonActived = false; PaintPanel.move_or_eraseButtonActived = false; PaintPanel.currentShape = ProgrammZustand.CIRCLE; } }); // textButton: textButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.lineButtonActived = false; PaintPanel.rectangleButtonActived = false; PaintPanel.ellipseButtonActived = false; PaintPanel.circleButtonActived = false; PaintPanel.textButtonActived = true; PaintPanel.move_or_eraseButtonActived = false; PaintPanel.currentShape = ProgrammZustand.TEXT; } }); // move_or_eraseButton: move_or_eraseButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.lineButtonActived = false; PaintPanel.rectangleButtonActived = false; PaintPanel.ellipseButtonActived = false; PaintPanel.circleButtonActived = false; PaintPanel.textButtonActived = false; PaintPanel.move_or_eraseButtonActived = true; } }); // rasterButton: rasterButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.rasterButtonActived = true; // PaintPanel.moveEnabled = false; if(cnt == 0) { PaintPanel.rasterButtonActived = true; cnt = 1; } else { PaintPanel.rasterButtonActived = false; cnt = 0; } } });

// colorButton: colorButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.colorButtonActived = true; Color defaultColor = Color.BLACK; Color selected = JColorChooser.showDialog( ButtonPanel.this, \"Select Color\", defaultColor); PaintPanel.currentColor = selected; } }); } } //------------ PaintPanel ------------ class PaintPanel extends JPanel { static int currentShape; static boolean currentFill; static Color currentColor; static Color currentBackColor; // f黵 Button Activit鋞e abzufragen static boolean lineButtonActived; static boolean rectangleButtonActived; static boolean ellipseButtonActived; static boolean circleButtonActived; static boolean textButtonActived; static boolean move_or_eraseButtonActived; static boolean rasterButtonActived; static boolean colorButtonActived;

static boolean rasterEnabled; static boolean paintEnabled; static ArrayList polygon = new ArrayList(); private static int moveX, moveY; private int currentX, currentY;

public PaintPanel() { /////////////////////////// lineButtonActived = false; rectangleButtonActived = false; ellipseButtonActived = false; circleButtonActived = false; textButtonActived = false; move_or_eraseButtonActived = false; rasterButtonActived = false;

/////////////////////////// paintEnabled = false; rasterEnabled = false; currentFill = false; currentColor = Color.BLACK; currentBackColor = Color.WHITE;

polygon.ensureCapacity(20);

setLayout(new BorderLayout()); ButtonPanel buttonPanel = new ButtonPanel(); add(buttonPanel, BorderLayout.NORTH); LabelPanel label = new LabelPanel(); add(label, BorderLayout.SOUTH);

addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); //addKeyListener(new KeyHandler()); //super.addKeyListener(new KeyHandler()); addKeyListener(new MyKeyListener()); setFocusable(true); requestFocus(); }

public void paintComponent(Graphics g) { paintEnabled = lineButtonActived || rectangleButtonActived || ellipseButtonActived || circleButtonActived || textButtonActived; super.paintComponent(g); Graphics2D g2 = (Graphics2D)g;

int w = getWidth(); int h = getHeight();

Rectangle2D scale = new Rectangle2D.Double(0, 0, w, h); g2.setColor(currentBackColor); g2.fill(scale);

if(rasterButtonActived) { g2.setColor(Color.LIGHT_GRAY); for(int i = 1; i<=w; i += Raster.ABSTAND) g2.drawLine(i,0,i,h); for(int j = 1; j<=h; j += Raster.ABSTAND) g2.drawLine(0,j,w,j); }

for(int i=0; i<polygon.size(); i++) { ProgrammZustand temp = (ProgrammZustand)polygon.get(i); boolean fill = temp.isFill; g2.setColor(temp.color); switch(temp.zustand) { case ProgrammZustand.LINE: Line2D showLine = new Line2D.Double(temp.getStart(), temp.getEnd()); g2.draw(showLine); break; case ProgrammZustand.RECTANGLE: Rectangle2D showSquare = new Rectangle2D.Double(); showSquare.setFrameFromDiagonal(temp.getStart(), temp.getEnd()); if(fill) g2.fill(showSquare); else g2.draw(showSquare); break; case ProgrammZustand.ELLIPSE: Ellipse2D showEllipse = new Ellipse2D.Double(); showEllipse.setFrameFromDiagonal(temp.getStart(), temp.getEnd()); if(fill) g2.fill(showEllipse); else g2.draw(showEllipse); break; case ProgrammZustand.CIRCLE: int xStart = (int)temp.getStart().getX(); int yStart = (int)temp.getStart().getY(); int circleWidth = (int)(temp.getEnd().getX()-temp.getStart().getX()); if(circleWidth>0) { if (fill) g.fillOval( (int) temp.getStart().getX(), (int) temp.getStart().getY(), (int) (temp.getEnd().getX() - temp.getStart().getX()), (int) (temp.getEnd().getX() - temp.getStart().getX())); else g.drawOval( (int) temp.getStart().getX(), (int) temp.getStart().getY(), (int) (temp.getEnd().getX() - temp.getStart().getX()), (int) (temp.getEnd().getX() - temp.getStart().getX())); temp.setStart(xStart, yStart); temp.setEnd(xStart+circleWidth, yStart+circleWidth); } break; case ProgrammZustand.TEXT: String myText = temp.text; g.drawString(myText, (int)temp.getStart().getX(), (int)temp.getStart().getY()); break; } } } private class LabelPanel extends JPanel { JLabel label; public LabelPanel() { label = new JLabel(\"Current X: \" + currentX + \" Current Y: \" + currentY); add(label); } public void paintComponent(Graphics g) { super.paintComponent(g); label.setText(\"Current X: \" + currentX + \" Current Y: \" + currentY); } } private class MouseHandler extends MouseAdapter {

public void mousePressed(MouseEvent event) { int tempX, tempY; ProgrammZustand temp; tempX = event.getX(); tempY = event.getY(); currentX = tempX; currentY = tempY;

int isLeft = event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK; int isRight = event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK; // right mouse tipped and move_or_erase Button actived // the figur will be removed. if(isRight != 0 && move_or_eraseButtonActived ) { for(int i=0; i<polygon.size(); i++) { temp = (ProgrammZustand)polygon.get(i); if(temp.isContain(tempX, tempY)) { polygon.remove(i); repaint(); i--; } } repaint(); } else if(isRight != 0 && paintEnabled) { currentX = event.getX(); currentY = event.getY(); repaint(); } // left mouse and paintEnabled, should create the // approciated figur else if((isLeft != 0) && paintEnabled) { switch(currentShape) { case ProgrammZustand.LINE: temp = new Line(); break; case ProgrammZustand.RECTANGLE: temp = new Rectangle(); break; case ProgrammZustand.ELLIPSE: temp = new Ellipse(); break; case ProgrammZustand.CIRCLE: temp = new Circle(); break; case ProgrammZustand.TEXT: temp = new Text(); break; default: temp = new Rectangle(); } temp.setStart(tempX, tempY); temp.color = currentColor; temp.isFill = currentFill; polygon.add(temp); } //////////////////////////////////// else if((isLeft != 0) && move_or_eraseButtonActived) { for(int i=0; i<polygon.size(); i++) { temp = (ProgrammZustand) polygon.get(i); if(temp.isContain(tempX, tempY)) { //temp.isSelected = true; temp.isStored = true; moveX = tempX; moveY = tempY; break; } } }

} /* public void mouseReleased(MouseEvent event) { int tempX, tempY; ProgrammZustand temp; tempX = event.getX(); tempY = event.getY(); currentX = tempX; currentY = tempY;

int isLeft = event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK;

if((isLeft != 0) && paintEnabled) { //for(int i = 0; i<polygon.size(); i++) //{ temp = (ProgrammZustand)polygon.remove(polygon.size()-1); if(temp.isStored) {

temp.setEnd(tempX, tempY); temp.isStored = false; polygon.add(temp); } repaint(); } //temp = (ProgrammZustand)polygon.remove(polygon.size()-1); //temp.setEnd(tempX, tempY); //polygon.add(temp);

else if((isLeft != 0) && !move_or_eraseButtonActived) { for(int i=0; i<polygon.size(); i++) { temp = (ProgrammZustand)polygon.get(i); if(temp.isStored) { temp = (ProgrammZustand)polygon.remove(i); int x1 = (int)temp.getStart().getX() + (tempX - moveX); int y1 = (int)temp.getStart().getY() + (tempY - moveY); int x2 = (int)temp.getEnd().getX() + (tempX - moveX); int y2 = (int)temp.getEnd().getY() + (tempY - moveY); temp.setStart(x1, y1); temp.setEnd(x2, y2);

//move_or_eraseButtonActived = true; temp.isSelected = false; temp.isStored = false; polygon.add(i, temp); repaint(); break; } } } } */ }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-06-12 00:29
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

程序代码:

private class MouseMotionHandler implements MouseMotionListener { public void mouseMoved(MouseEvent event) { currentX = event.getX(); currentY = event.getY(); repaint(); }

public void mouseDragged(MouseEvent event) { int tempX, tempY; ProgrammZustand temp;

tempX = event.getX(); tempY = event.getY(); currentX = tempX; currentY = tempY;

int isLeft = event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK; int isRight = event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK;

//wozu bin ich da :-) // wenn left mouse gehaltenden gedrueckt und bewegt // und wenn paintEnabled , dann soll ein Figur gezeichnet werden if((isLeft != 0) && paintEnabled) { /* for(int i = 0; i<polygon.size(); i++) { temp = (ProgrammZustand)polygon.get(i); if(temp.isContain(tempX, tempY)) //&& temp.isStored) { temp = (ProgrammZustand)polygon.remove(i); temp.setEnd(tempX, tempY); polygon.add(temp); }

} */ int polygonSize = polygon.size(); temp = (ProgrammZustand)polygon.remove(polygonSize-1); //if(temp.isStored) temp.setEnd(tempX, tempY); polygon.add(temp); repaint(); } // und wenn left mouse gehaltenden gedrueckt und bewegt // und move_or_eraseButtonActived dann soll die selected Figur // bewegt werden else if((isLeft != 0) && move_or_eraseButtonActived) { for(int i=0; i<polygon.size(); i++) { temp = (ProgrammZustand)polygon.get(i); if(temp.isContain(tempX, tempY)) { // temp.isSelected = true; temp = (ProgrammZustand)polygon.remove(i);

int x1 = (int)temp.getStart().getX() + (tempX - moveX); int y1 = (int)temp.getStart().getY() + (tempY - moveY); int x2 = (int)temp.getEnd().getX() + (tempX - moveX); int y2 = (int)temp.getEnd().getY() + (tempY - moveY); temp.setStart(x1, y1); temp.setEnd(x2, y2); moveX = tempX; moveY = tempY;

polygon.add(polygon.size(), temp); repaint(); } } repaint(); } repaint();

} } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent ke) { System.out.println(\"test\"); ProgrammZustand temp; if(textButtonActived) { int polygonSize = polygon.size(); temp = (ProgrammZustand)polygon.remove(polygonSize-1); if(ke.getKeyCode() == KeyEvent.VK_ENTER) textButtonActived = false; // Texteingabe beenden else { if(ke.getKeyCode() != KeyEvent.VK_BACK_SPACE) temp.text += ke.getKeyChar(); // Taste an String h鋘gen else if (temp.text.length()>0) //Backspace temp.text = temp.text.substring(0,temp.text.length()-1); //letzten Charakter l鰏chen } polygon.add(temp); repaint(); }

} }

class KeyHandler extends KeyAdapter { public void keyPressed(KeyEvent e) { ProgrammZustand temp; if(textButtonActived) { int polygonSize = polygon.size(); temp = (ProgrammZustand)polygon.remove(polygonSize-1); if(e.getKeyCode() == KeyEvent.VK_ENTER) textButtonActived = false; // Texteingabe beenden else { if(e.getKeyCode() != KeyEvent.VK_BACK_SPACE) temp.text += e.getKeyChar(); // Taste an String h鋘gen else if (temp.text.length()>0) //Backspace temp.text = temp.text.substring(0,temp.text.length()-1); //letzten Charakter l鰏chen } polygon.add(temp); repaint(); } } } } //------------ Class PaintFrame ------------ class PaintFrame extends JFrame { public PaintFrame() { setTitle(\"Paint Board\"); setBounds(100, 100, 800, 400);

JMenuBar menuBar = makeMenuBar(); setJMenuBar(menuBar);

PaintPanel panel = new PaintPanel(); getContentPane().add(panel); } private JMenuBar makeMenuBar() { JMenuBar menuBar = new JMenuBar();

JMenu fileMenu = new JMenu(\"File\"); fileMenu.setMnemonic('F'); menuBar.add(fileMenu);

JMenuItem newItem = new JMenuItem(\"New\", 'N'); newItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK)); newItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.polygon.clear(); PaintPanel.currentBackColor = Color.WHITE; PaintPanel.currentColor = Color.BLACK; PaintPanel.currentFill = false; PaintPanel.paintEnabled = false; PaintPanel.move_or_eraseButtonActived = true; } }); fileMenu.add(newItem);

JMenuItem openItem = new JMenuItem(\"Open\", 'O'); openItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); openItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(\".\")); chooser.showOpenDialog(PaintFrame.this); } }); fileMenu.add(openItem);

JMenuItem saveItem = new JMenuItem(\"Save\", 'O'); saveItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); saveItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(\".\")); chooser.showSaveDialog(PaintFrame.this); } });

fileMenu.add(saveItem); fileMenu.addSeparator();

JMenuItem exitItem = new JMenuItem(\"Exit\", 'x'); exitItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); exitItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); fileMenu.add(exitItem);

JMenu editMenu = new JMenu(\"Edit\"); editMenu.setMnemonic('E'); menuBar.add(editMenu);

JMenuItem undoItem = new JMenuItem(\"Undo\", new ImageIcon(\"undo.gif\")); undoItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK)); editMenu.add(undoItem);

JMenuItem cutItem = new JMenuItem(\"Cut\", new ImageIcon(\"cut.gif\")); cutItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); editMenu.add(cutItem);

JMenuItem copyItem = new JMenuItem(\"Copy\", new ImageIcon(\"copy.gif\")); copyItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK)); editMenu.add(cutItem); JMenuItem pasteItem = new JMenuItem(\"Paste\", new ImageIcon(\"paste.gif\")); pasteItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK)); editMenu.add(pasteItem);

JMenu setMenu = new JMenu(\"Settings\"); setMenu.setMnemonic('S'); menuBar.add(setMenu);

JMenuItem backItem = new JMenuItem(\"Background\", 'B'); backItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { Color defaultColor = Color.BLACK; Color selected = JColorChooser.showDialog(PaintFrame.this, \"Select Color\", defaultColor); PaintPanel.currentBackColor = selected; } }); setMenu.add(backItem);

ButtonGroup group = new ButtonGroup(); JRadioButtonMenuItem draw = new JRadioButtonMenuItem(\"Draw\", true); draw.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.currentFill = false; } }); JRadioButtonMenuItem fill = new JRadioButtonMenuItem(\"Fill\", false); fill.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { PaintPanel.currentFill = true; } }); group.add(draw); group.add(fill); JMenu style = new JMenu(\"Style\"); style.add(draw); style.add(fill); setMenu.add(style);

JMenu helpMenu = new JMenu(\"Help\"); helpMenu.setMnemonic('H'); menuBar.add(helpMenu);

JMenuItem helpItem = new JMenuItem(\"Help\", 'H'); helpItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); helpMenu.add(helpItem);

JMenuItem aboutItem = new JMenuItem(\"About\", 'A'); aboutItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); aboutItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog( PaintFrame.this, \" JPaint Version 0.1\n\n\" + \"Author : jellen chang \nE-mail : jellen@126.com\" + \"\n Author : kaihua wu \nE-mail: kaihua1@yahoo.com\"); } }); helpMenu.add(aboutItem); return menuBar; } } //------------ Public Class ------------ public class Zeichnen { public static void main(String[] args) throws Exception { PaintFrame frame = new PaintFrame(); JFrame.setDefaultLookAndFeelDecorated(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-06-12 00:30
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
以上3段代码大家先贴在写字板上,然后存为 Zeichnen.java 通过JBuilder 或其他Java 编译器就可以编译运行了。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-06-12 00:35
快速回复:我碰到问题了!!!请大家救急啊!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045430 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved