Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Java SE 综合讨论区
打印话题 寄给朋友 订阅主题 |
作者 | 我的错误在哪儿? |
blueshoot
发贴: 15 积分: 1 |
于 2006-11-02 00:16
我本来想利用键盘的上下左右,来在面板上划线,但是我调试的程序只能显示面板,无法显示线条,请大家帮我找一下我的错误在哪儿,非常感谢. package event; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SketchTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SketchFrame frame=new SketchFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class SketchFrame extends JFrame{ public SketchFrame(){ setTitle("SketchFrame"); setSize(400,300); SketchPanel panel=new SketchPanel(); add(panel); } } class SketchPanel extends JPanel{ private Point2D last; private ArrayList<Line2D> lines; private static final int SMALL_INCREMENT=1; private static final int LARGE_INCREMENT=5; public SketchPanel(){ last=new Point2D.Double(100,100); lines=new ArrayList<Line2D>(); KeyHandler listener=new KeyHandler(); addKeyListener(listener); setFocusable(true); } public void add(int dx, int dy) { Point2D end=new Point2D.Double(last.getX()+dx,last.getY()+dy); Line2D line=new Line2D.Double(last,end); lines.add(line); repaint(); last=end; } public void paintComponent(Graphics g){ super.paintComponent( g ); Graphics2D g2=(Graphics2D)g; for(Line2D line:lines){ g2.draw(line); } } private class KeyHandler implements KeyListener{ public void keyPressed(KeyEvent ke) { // TODO Auto-generated method stub int d; char key=ke.getKeyChar(); if(ke.isShiftDown()) d=LARGE_INCREMENT; else d=SMALL_INCREMENT; if(key==KeyEvent.VK_RIGHT) add(d,0); else if(key==KeyEvent.VK_DOWN) add(0,d); else if(key==KeyEvent.VK_LEFT) add(-d,0); else if(key==KeyEvent.VK_UP) add(0,-d); } public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } } } 致JAVA初学者+如何下手学JAVA |
话题树型展开 |
人气 | 标题 | 作者 | 字数 | 发贴时间 |
7233 | 我的错误在哪儿? | blueshoot | 3960 | 2006-11-02 00:16 |
5405 | Re:我的错误在哪儿? | cxp108 | 172 | 2006-11-02 11:23 |
5502 | Re:我的错误在哪儿? | cxp108 | 321 | 2006-11-02 11:27 |
5783 | Re:我的错误在哪儿? | dejinzhao | 3317 | 2006-11-05 10:06 |
已读帖子 新的帖子 被删除的帖子 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |