当前位置: 技术问答>java相关
新手问题,帮忙看看这段代码
来源: 互联网 发布时间:2017-04-26
本文导语: import java.awt.*; import java.awt.geom.*; import java.util.*; import java.awt.event.*; import javax.swing.*; public class TempSketch { public static void main(String[] args) { TempFrame frame=new TempFrame(); frame.setDefaultCloseOperation(JFrame...
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class TempSketch
{
public static void main(String[] args)
{
TempFrame frame=new TempFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TempFrame extends JFrame
{
public TempFrame()
{
setTitle("Temp Sketch");
setSize(frmWidth,frmHeight);
TempPanel panel=new TempPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
private static int frmWidth=400;
private static int frmHeight=110;
}
class TempPanel extends JPanel
{
public TempPanel()
{
setBackground(Color.black);
last=new Point2D.Double(10,10);
lines=new ArrayList();
addKeyListener(new KeyAction());
}
public boolean isFocusTraversable()
{
return true;
}
public void addline(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);
g.setColor(Color.white);
Graphics2D g2=(Graphics2D)g;
for(int i=0; i
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class TempSketch
{
public static void main(String[] args)
{
TempFrame frame=new TempFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TempFrame extends JFrame
{
public TempFrame()
{
setTitle("Temp Sketch");
setSize(frmWidth,frmHeight);
TempPanel panel=new TempPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
private static int frmWidth=400;
private static int frmHeight=110;
}
class TempPanel extends JPanel
{
public TempPanel()
{
setBackground(Color.black);
last=new Point2D.Double(10,10);
lines=new ArrayList();
addKeyListener(new KeyAction());
}
public boolean isFocusTraversable()
{
return true;
}
public void addline(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);
g.setColor(Color.white);
Graphics2D g2=(Graphics2D)g;
for(int i=0; i