| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1234 人关注过本帖
标题:键盘画直线疑问
只看楼主 加入收藏
tgnian
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-4-13
收藏
 问题点数:0 回复次数:5 
键盘画直线疑问
为什么不能画直线
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawLine extends JFrame implements KeyListener
{
  
 int x=getWidth()/2;
 int y=getHeight()/2;
    int firstx=getWidth()/2;
    int firsty=getHeight()/2;
  private Graphics g;
  public DrawLine()
  {
    setFocusable(true);
    addKeyListener(this);
  }
  public static void main(String args[])
  {
   DrawLine frame=new DrawLine();
   frame.setTitle("DrawLine");
   frame.setSize(400,300);
   frame.setVisible(true);
   frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    /*frame.x=frame.getWidth()/2;
   frame.y=frame.getHeight()/2;
   frame.firstx=frame.getWidth()/2;
   frame.firsty=frame.getHeight()/2; */
 }
  public void keyReleased(KeyEvent e)
  {
  
  }
  public void keyTyped (KeyEvent e)
  {
   
  }
  public void keyPressed(KeyEvent e)
  {
   g=getGraphics();
   
   switch(e.getKeyCode())
   {
   case KeyEvent.VK_DOWN:y+=10;break;
   case KeyEvent.VK_UP:y-=10;break;
   case KeyEvent.VK_LEFT:x-=10;break;
   case KeyEvent.VK_RIGHT:x+=10;break;
   }
   g.drawLine(firstx,firsty,x,y);
   firstx=x;
   firsty=y;
  }
}
把int x=getWidth()/2;
 int y=getHeight()/2;
    int firstx=getWidth()/2;
    int firsty=getHeight()/2;
删去,把注释符号删去,使frame.x=frame.getWidth()/2;
   frame.y=frame.getHeight()/2;
   frame.firstx=frame.getWidth()/2;
   frame.firsty=frame.getHeight()/2;能执行
 为什么能画直线
   KeyPressed方法怎么能调用frame的x ,y, firstx,firsty
搜索更多相关主题的帖子: 直线 疑问 键盘 
2007-11-23 01:25
huwangvs
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:764
专家分:0
注 册:2007-2-4
收藏
得分:0 
第一个没读懂你的意思。。
第二个KeyPressed方法就在类里面。。。为什么不能调用类的属性、、、
2007-11-23 08:06
Eastsun
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:802
专家分:0
注 册:2006-12-14
收藏
得分:0 
LZ这样写是不对的,应该把作图部分的代码写到方法 protected void paintComponent(Graphics g)中,然后需要重绘的时候调用repaint()方法.
你可以参考一下下面这个代码:
程序代码:
/**
    一个用键盘作图的程序
  使用'h','j','k','L'件操纵绘图
*/

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

public class Sketch
{  
   public static void main(String[] args)
   {  
      SketchFrame frame = new SketchFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

/**
   A frame with a panel for sketching a figure
*/
class SketchFrame extends JFrame
{
   public SketchFrame()
   {
      setTitle("Sketch");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // add panel to frame

      SketchPanel panel = new SketchPanel();
      add(panel);
   }

   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;  
}

/**
   A panel for sketching with the keyboard.
*/
class SketchPanel extends JPanel
{ 
   public SketchPanel()
   {  
      last = new Point2D.Double(100, 100);
      lines = new ArrayList<Line2D>();
      KeyHandler listener = new KeyHandler();
      addKeyListener(listener);
      setFocusable(true);
   }

   /**
      Add a new line segment to the sketch.
      @param dx the movement in x direction
      @param dy the movement in y direction
   */
   public void add(int dx, int dy)
   {  
      // compute new end point
      Point2D end = new Point2D.Double(last.getX() + dx, last.getY() + dy);

      // add line segment
      Line2D line = new Line2D.Double(last, end);
      lines.add(line);
      repaint();

      // remember new end point
      last = end;
   }

   public void paintComponent(Graphics g)
   {  
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;

      // draw all lines
      for (Line2D l : lines)
         g2.draw(l);
   }

   private Point2D last;
   private ArrayList<Line2D> lines;

   private static final int SMALL_INCREMENT = 1;
   private static final int LARGE_INCREMENT = 5;

   private class KeyHandler implements KeyListener
   {  
      public void keyPressed(KeyEvent event)
      {  
         int keyCode = event.getKeyCode();

         // set distance
         int d;
         if (event.isShiftDown())
            d = LARGE_INCREMENT;
         else
            d = SMALL_INCREMENT;

         // add line segment
         if (keyCode == KeyEvent.VK_LEFT) add(-d, 0);
         else if (keyCode == KeyEvent.VK_RIGHT) add(d, 0);
         else if (keyCode == KeyEvent.VK_UP) add(0, -d);
         else if (keyCode == KeyEvent.VK_DOWN) add(0, d);
      }

      public void keyReleased(KeyEvent event) {}

      public void keyTyped(KeyEvent event)
      {  
         char keyChar = event.getKeyChar();

         // set distance
         int d;
         if (Character.isUpperCase(keyChar))
         {  
            d = LARGE_INCREMENT;
            keyChar = Character.toLowerCase(keyChar);
         }
         else
            d = SMALL_INCREMENT;

         // add line segment
         if (keyChar == 'h') add(-d, 0);
         else if (keyChar == 'l') add(d, 0);
         else if (keyChar == 'k') add(0, -d);
         else if (keyChar == 'j') add(0, d);
      }
   }
}

My BlogClick Me
2007-11-23 12:47
tgnian
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-4-13
收藏
得分:0 
sorry,能分段画直线,但为什么一开始
int x=getWidth()/2;
    int y=getHeight()/2;
       int firstx=getWidth()/2;
       int firsty=getHeight()/2;
x,y,firstx,firsty都为0;不为panel的宽,高的一半

2007-11-24 18:28
huwangvs
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:764
专家分:0
注 册:2007-2-4
收藏
得分:0 
JFrame默认宽,高是0,0.。位置0,0.。
2007-11-24 20:32
天人和一
Rank: 1
等 级:新手上路
帖 子:137
专家分:0
注 册:2007-10-11
收藏
得分:0 
不错有意思!!!
2007-11-24 21:01
快速回复:键盘画直线疑问
数据加载中...
 
   



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

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