java中如何用画布做出移动方格的图形?
答案:1 悬赏:30 手机版
解决时间 2021-03-28 10:23
- 提问者网友:自食苦果
- 2021-03-28 03:21
java中如何用画布做出移动方格的图形?
最佳答案
- 五星知识达人网友:青灯有味
- 2021-03-28 04:17
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Example6_8 extends JFrame implements ActionListener
{
private JButton left=new JButton("向左移");
private JButton right=new JButton("向右移");
private JButton up=new JButton("向上移");
private JButton down=new JButton("向下移");
MoveCanvas drawing=new MoveCanvas();
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
public Example6_8()
{
super("移动方块");
setSize(400,400);
setVisible(true);
Panel p=new Panel();
p.setLayout(new FlowLayout());
setLayout(new BorderLayout());
add(p,BorderLayout.SOUTH);
add(drawing,BorderLayout.CENTER);
p.add(up);
p.add(down);
p.add(left);
p.add(right);
validate();
left.addActionListener(this);
right.addActionListener(this);
up.addActionListener(this);
down.addActionListener(this);
addWindowListener(new WindowCloser());
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==up)
drawing.moveUp();
else if(e.getSource()==down)
drawing.moveDown();
else if(e.getSource()==left)
drawing.moveLeft();
else if(e.getSource()==right)
drawing.moveRight();
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
new Example6_8();
}
}
class MoveCanvas extends Canvas
{
int WIDTH=30,HEIGHT=30,INC=10;
int i,j;
public void paint(Graphics g)
{
g.drawRect(0,0,getSize().width-1,getSize().height-1);
g.setColor(Color.black);
g.fillRect(i+2,j+2,WIDTH+2,HEIGHT+2);
g.setColor(Color.red);
g.fillRect(i,j,WIDTH,HEIGHT);
}
public void moveUp()
{
if(j>0)
j-=INC;
else
j=getSize().height-INC;
repaint();
}
public void moveDown()
{
if(j j+=INC;
else
j=0;
repaint();
}
public void moveLeft()
{
if(i>0)
i-=INC;
else
i=getSize().width-INC;
repaint();
}
public void moveRight()
{
if(i i+=INC;
else
i=0;
repaint();
}
}
import java.awt.event.*;
import javax.swing.*;
public class Example6_8 extends JFrame implements ActionListener
{
private JButton left=new JButton("向左移");
private JButton right=new JButton("向右移");
private JButton up=new JButton("向上移");
private JButton down=new JButton("向下移");
MoveCanvas drawing=new MoveCanvas();
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
public Example6_8()
{
super("移动方块");
setSize(400,400);
setVisible(true);
Panel p=new Panel();
p.setLayout(new FlowLayout());
setLayout(new BorderLayout());
add(p,BorderLayout.SOUTH);
add(drawing,BorderLayout.CENTER);
p.add(up);
p.add(down);
p.add(left);
p.add(right);
validate();
left.addActionListener(this);
right.addActionListener(this);
up.addActionListener(this);
down.addActionListener(this);
addWindowListener(new WindowCloser());
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==up)
drawing.moveUp();
else if(e.getSource()==down)
drawing.moveDown();
else if(e.getSource()==left)
drawing.moveLeft();
else if(e.getSource()==right)
drawing.moveRight();
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
new Example6_8();
}
}
class MoveCanvas extends Canvas
{
int WIDTH=30,HEIGHT=30,INC=10;
int i,j;
public void paint(Graphics g)
{
g.drawRect(0,0,getSize().width-1,getSize().height-1);
g.setColor(Color.black);
g.fillRect(i+2,j+2,WIDTH+2,HEIGHT+2);
g.setColor(Color.red);
g.fillRect(i,j,WIDTH,HEIGHT);
}
public void moveUp()
{
if(j>0)
j-=INC;
else
j=getSize().height-INC;
repaint();
}
public void moveDown()
{
if(j
else
j=0;
repaint();
}
public void moveLeft()
{
if(i>0)
i-=INC;
else
i=getSize().width-INC;
repaint();
}
public void moveRight()
{
if(i
else
i=0;
repaint();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯