import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.Image; public class samurai extends JFrame { public samurai() { setSize(750, 500); setTitle("Samurai"); setDefaultCloseOperation(EXIT_ON_CLOSE); MyJPanel myJPanel = new MyJPanel(); Container c = getContentPane(); c.add(myJPanel); setVisible(true); } public static void main(String[] args) { new samurai(); } public class MyJPanel extends JPanel implements MouseListener, MouseMotionListener, ActionListener { Timer timer; double line = -3.14/2; //自機 int jiki_x = 100; int jiki_y; Image jiki_ima1,jiki_ima2; int jiki_w, jiki_h; int jiki_id=0;//瞬間移動打消、0=マウスに反応 1=マウスが出た 2=up 3=down int cut; public MyJPanel() { setResizable(false); timer = new Timer(10, this); timer.start(); //自機初期化 ImageIcon icon1 = new ImageIcon("normal.png"); ImageIcon icon2 = new ImageIcon("cut.png"); jiki_ima1 = icon1.getImage(); jiki_ima2 = icon2.getImage(); jiki_w = jiki_ima1.getWidth(this); jiki_h = jiki_ima1.getHeight(this); cut = 0; jiki_id=1; addMouseListener(this); addMouseMotionListener(this); } public void actionPerformed(ActionEvent e){ int heightL = 50; Dimension d = getSize(); if(e.getSource() == timer){ line += 3.14/30; //自機とマウスの照準 if(jiki_id==2){ jiki_y+=5; if(jiki_y>d.height-heightL-70){ jiki_y = d.height-heightL-70; jiki_id=3; } }else if(jiki_id==3){ jiki_y-=5; if(jiki_y<0){ jiki_y = 0; jiki_id=2; } } //cut 復元 if(cut == 0){}else{ cut --; if(cut < 0){ cut = 0; } } repaint(); } } public void mouseClicked(MouseEvent me) { Dimension d = getSize(); //int heightR = teki_ima.getHeight(this); //cut if(cut == 0){ cut = 30; //当たり判定 /* for(int num =0;num=tekiki_y[num] && jiki_y[she]<=tekiki_y[num]+heightR && jiki_x[she]== tekiki_x[num] && tekiki_alive[num]>0){ //敵消滅 tekiki_alive[num] --; } }*/} repaint(); } public void mousePressed(MouseEvent me) { } public void mouseReleased(MouseEvent me) { } public void mouseExited(MouseEvent me) { jiki_id=1; } public void mouseEntered(MouseEvent me) { if(jiki_y==me.getY()){ jiki_id=0; }else if(jiki_yme.getY()){ jiki_id=3; } } public void mouseMoved(MouseEvent me) { int heightL = 50; Dimension d = getSize(); if(jiki_id==0){//手動操縦 jiki_y = me.getY(); if(jiki_y>d.height-heightL-70){ jiki_y = d.height-heightL-70; } repaint(); }else{ if(jiki_y==me.getY()){ jiki_id=0; }else if(jiki_yme.getY()){ jiki_id=3; } } repaint(); } public void mouseDragged(MouseEvent me) { } public void paintComponent(Graphics g) { super.paintComponent(g); int heightL = jiki_ima1.getHeight(this); int widthL = jiki_ima1.getWidth(this); int maru_y = jiki_y + heightL +35; int maru_x = jiki_x + 35; //自機を描く if(cut == 0){ g.drawImage(jiki_ima1, jiki_x, jiki_y, this); }else{ g.drawImage(jiki_ima2, jiki_x+35, jiki_y, this); } //ball g.fillOval(jiki_x, jiki_y + heightL,70,70); g.setColor(Color.red); g.drawLine(maru_x, maru_y, (int)(maru_x + 35*(Math.cos(line))), (int)(maru_y + 35*(Math.sin(line)))); } } }