当前位置: 代码迷 >> J2SE >> 真正无人能解的有关问题,哈哈
  详细解决方案

真正无人能解的有关问题,哈哈

热度:11   发布时间:2016-04-24 14:37:16.0
真正无人能解的问题,哈哈
源码如下(corejava例8-3   卷一page289稍作改动),其效果应该是移动方向键绘制直线,可是程序没有发现绘制效果,请高手指教(已通过编译,可运行)
import   java.awt.*;
import   javax.swing.*;
import   java.awt.geom.*;
import   java.awt.event.*;
import   java.util.*;

public   class   SktStu  
{
public   static   void   main(String[]   args)  
{
SktStu.start();
}
public   static   void   start()
{
SktJFrame   frame=new   SktJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class   SktJFrame   extends   JFrame
{
public   SktJFrame()
{
setTitle( "SktStu ");
setExtendedState(JFrame.MAXIMIZED_BOTH);
setSize(600,400);
SktPanel   panel=new   SktPanel();
add(panel);
}
}
class   SktPanel   extends   JPanel
{
public   SktPanel()
{
last=new   Point2D.Double(100,100);
lines=new   ArrayList <Line2D> ();
KeyHandler   listener=new   KeyHandler();
addKeyListener(listener);
setFocusable(true);
}
private   Point2D   last;
private   ArrayList <Line2D>   lines;
private   int   SMALL_INCREMENT=1;
private   int   LARGE_INCREMENT=5;
private   static   int   d;

public   void   paintComponent(Graphics   g)
{
super.paintComponent(g);
Graphics2D   g2=(Graphics2D)g;
for(Line2D   l:lines)
{
g2.draw(l);
}
}
private   class   KeyHandler   implements   KeyListener
{
public   void   keyPressed(KeyEvent   event)
{
int   keyCode=event.getKeyCode();
if(event.isShiftDown())
d=LARGE_INCREMENT;
else
d=SMALL_INCREMENT;
if(keyCode==KeyEvent.VK_LEFT)   moveLeft();
                        else   if(keyCode==KeyEvent.VK_RIGHT)   moveRight();
else   if(keyCode==KeyEvent.VK_UP)   moveUp();
else   if(keyCode==KeyEvent.VK_DOWN)   moveDown();
}

public   void   keyReleased(KeyEvent   event){}

public   void   keyTyped(KeyEvent   event)
{
}

private   void   moveLeft(){move(-d,0);}
private   void   moveRight(){move(d,0);}
private   void   moveUp(){move(0,-d);}
private   void   moveDown(){move(0,d);}
private   void   move(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;
}

}
}


------解决方案--------------------
都说了无人能解了,大家还浪费什么力气呢.
  相关解决方案