• 締切済み

Javaのプログラミング手伝ってください

以下のソースコードを応用して、添付画像のような数字のかかれた画像を3つ並べてそれを回して7を揃えるというゲームを作りたいのですが全く進みません。どなたか完成させてくださいm(__)m使用する画像のファイル名は「slot1.jpeg」~「slot7.jpeg」です。 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class OneSlot extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; final int MAX = 7; Timer timer = new Timer(150 , this); JLabel label= new JLabel("スタートで始まるよ",JLabel.CENTER); JButton[] bt = {new JButton("スタート"),new JButton("ストップ")}; JLabel slot= new JLabel(new ImageIcon("Slot1.jpg"),JLabel.CENTER); ImageIcon[] slot_icon = new ImageIcon[MAX]; int iCount = 0; OneSlot(String title) { super(title); timer.setActionCommand("timer"); setBounds(200, 200, 230, 150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for(int i = 0;i < slot_icon.length; i++) slot_icon[i] = new ImageIcon("Slot" + (i+1) + ".jpg"); add("North",label); add("Center",slot); JPanel p = new JPanel(); for (int i = 0; i < bt.length;i++) { bt[i].addActionListener(this); p.add(bt[i]); } add("South",p); setVisible(true); } public static void main(String[] args) { new OneSlot("スロットマシン"); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if(cmd.equals("timer")) { if(++iCount == slot_icon.length) iCount = 0; slot.setIcon(slot_icon[iCount]); } if(e.getSource() == bt[0] && !timer.isRunning()) { label.setText("7が当たりだよ"); timer.start(); } else if(e.getSource() == bt[1] && timer.isRunning()) { if(iCount+1 == MAX) label.setText("やった!!おめでとう"); else label.setText("残念でした"); timer.stop(); } } }

  • Java
  • 回答数1
  • ありがとう数1

みんなの回答

回答No.1

質問じゃないなら、 http://www.lancers.jp/ こういうところでお願いするといいよ~

関連するQ&A

  • JAVAでの背景画像表示

    現在javaでゲームのメニュー画面を作っているのですが、ボタンを配置する所まではできたんですが、背景に画像を表示することができなくて困っています! import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Container; public class Mati extends JFrame{ public static void main(String[] args){ Mati frame = new Mati(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10,650,650); frame.setTitle("街"); frame.setVisible(true); JPanel h = new JPanel(); h.setOpaque(false); ImageIcon icon1 = new ImageIcon("Mati.jpg"); JLabel label1 = new JLabel(icon1); JLabel label2 = new JLabel(); h.add(label1); } Mati(){ JButton button1 = new JButton("宿屋"); button1.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button2 = new JButton("道具屋"); button2.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button3 = new JButton("武器屋"); button3.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button4 = new JButton("街を出る"); button4.setFont(new Font("Mairyo", Font.PLAIN, 30)); JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); p.add(Box.createRigidArea(new Dimension(10,30))); p.add(button1); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button2); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button3); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button4); getContentPane().add(p, BorderLayout.CENTER); } } ソースはこのようになっています。これからどうすれば背景画像が表示されるか教えていただきたいです。 よろしくお願いします。

  • javaでスタート画面を作っていて困っています。

    現在javaでゲームのスタート画面を作っているのですが、パネルが透過されずに困っています。 プログラムソースは import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.*; import java.awt.Font; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Container; class Start最新版 extends JFrame{ public static void main(String args[]){ Start最新版 frame = new Start最新版("タイトル"); frame.setVisible(true); } Start最新版(String title){ setTitle(title); setBounds(10, 10, 1024, 768); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); p.setOpaque( false ); ImageIcon icon1 = new ImageIcon("Start.jpg"); JLabel label1 = new JLabel(icon1); JLabel label2 = new JLabel(); p.add(label1); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); JButton button1 = new JButton("ゲームを始める"); button1.setFont(new Font("Mairyo", Font.PLAIN, 30)); JPanel n = new JPanel(); n.setOpaque(false); n.setLayout(new BoxLayout(n, BoxLayout.Y_AXIS)); n.add(Box.createRigidArea(new Dimension(290,30))); n.add(button1); となっています。「ゲームを始める」ボタンだけを残して、周りのパネルを透過して画像を表示させたいのですが、うまくいきません!解決方法をご存知の方どうか教えていただきたいです!よろしくお願いします。

  • javaのタイマープログラムの時間について

    下記のjavaのプログラムは、タイマー動作の確認プログラムです。 タイマーの起動時間を1msにし、1000回タイマーを起動しており トータルのタイマー時間は1秒と考えましたが、実際のタイマー時間は 20秒くらいかかります。 この件に関しまして、コメント頂けますと大変助かります。 import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class TimerTest1 extends JFrame implements ActionListener{ Timer timer; JLabel label; int sec; public static void main(String[] args){ TimerTest1 frame = new TimerTest1(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 300, 200); frame.setTitle("タイトル"); frame.setVisible(true); } TimerTest1(){ sec = 0; label = new JLabel(); JPanel labelPanel = new JPanel(); labelPanel.add(label); timer = new Timer(1, this); getContentPane().add(labelPanel, BorderLayout.CENTER); timer.start(); } public void actionPerformed(ActionEvent e){ label.setText(sec + " sec"); if (sec >= 1000){ timer.stop(); label.setText(" timer_stop"); }else{ sec++; } } } 以上、宜しくお願いします。

    • ベストアンサー
    • Java
  • Jave Swing 画像表示わからない

    java swingでGUIをプログラミングしている者です。 JLabelにImageIconの画像を貼り付けるプログラムです。↓ //画像を表示するプログラム //インポート import javax.swing.*; import java.awt.*; import java.awt.event.*; class gazopro extends JFrame{ JLabel l1; JButton b1; JPanel p; public static void main(String args[]) { //ウィンドウを作成また設定 gazopro frame = new gazopro(); frame.setBounds(300,200,700,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } gazopro(){ //コンポーネントを作成 p = new JPanel(); add(p); b1 = new JButton("画像を表示"); p.add(b1); ImageIcon icon = new ImageIcon("image.jpg"); l1 = new JLabel(icon); p.add(l1); } }   わからないところは、ImageIcon icon = new ImageIcon("image.jpg");のところです。 ImageIconの引数は、プログラミングを行っているパソコン(OS は Windows)のマイコンピューター→ Cドライブ→ MyJava→ image.jpgを指定しましたがこれで合ってますか? このプログラムでは、画像が表示されませんでした。 どうすればいいでしょうか? 解説と、ご指導宜しくお願いします。

    • ベストアンサー
    • Java
  • javaのボタン表示等に関する質問

    下記のコードはフォームをNORTH、CENTER、SOUTHEに分割して、それぞれのpaneに ラベルとボタンを表示するものです。 Q1)myFrame.setBounds(350,0, total_x, total_y)をコンストラクターの最初に記述しますと  フォームの表示が正常でなく、コンストラクターの最後では、正常に行なわれます。  この理由等について、コメント頂けますと有り難いです。 Q2)mainに記述してある、下記のコードは無くても、Xでクロースできますが,このコード  の記述は正しいでしょうか GridLayout_new frame = new GridLayout_new(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 以上お手数ですが、コメント頂けますと大変助かります。 //============================================= import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.LineBorder; public class GridLayout_new extends JFrame{ int panelA_x=200, panelA_y=50; int panelB_x=200, panelB_y=200; int panelC_x=200, panelC_y=50; int total_x=panelA_x; int total_y=panelA_y+panelB_y+panelC_y; public static void main(String[] args){ GridLayout_new frame = new GridLayout_new(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } GridLayout_new(){ JFrame myFrame=new JFrame(); myFrame.setTitle("panelA"); // myFrame.setBounds(350,0, total_x, total_y); //正常なformの表示が出来ない myFrame.setVisible(true); LineBorder border = new LineBorder(Color.BLACK, 1, true); //======================== JLabel button1=new JLabel("Label1"); GLabel_Layout(button1, border); JButton button2 = new JButton("button2"); button_Layout(button2, border); //======================== JLabel button3=new JLabel("Label3"); GLabel_Layout(button3, border); JButton button4 = new JButton("button4"); button_Layout(button4, border); //======================== JLabel button5=new JLabel("Label5"); GLabel_Layout(button5, border); JButton button6 = new JButton("button6"); //======================== JLabel button7=new JLabel("Label7"); GLabel_Layout(button7, border); JButton button8 = new JButton("button8"); //======================== JLabel button9=new JLabel("Label9"); GLabel_Layout(button9, border); JButton button10 = new JButton("button10"); button_Layout(button10, border); //======================== JLabel button11=new JLabel("Label11"); GLabel_Layout(button11, border); JButton button12 = new JButton("button12"); button_Layout(button12, border); //================================== JPanel p1 = new JPanel(); GridLayout layout1 = new GridLayout(2,2); //2行、2列の設定 layout1.setHgap(2); layout1.setVgap(10); p1.setLayout(layout1); p1.setBackground(Color.GREEN); p1.add(button1); p1.add(button2); p1.add(button3); p1.add(button4); myFrame.setSize(panelA_x, panelA_y); myFrame.getContentPane().add(p1, BorderLayout.NORTH); //============================== JPanel p2 = new JPanel(); GridLayout layout2 = new GridLayout(2,2); //2行、2列の設定 layout2.setHgap(2); layout2.setVgap(10); p2.setLayout(layout2); p2.setBackground(Color.GREEN); p2.add(button5); p2.add(button6); p2.add(button7); p2.add(button8); myFrame.setSize(panelB_x, panelB_y); myFrame.getContentPane().add(p2, BorderLayout.CENTER); //================================== JPanel p3 = new JPanel(); GridLayout layout3 = new GridLayout(2,2); //2行、2列の設定 layout3.setHgap(2); layout3.setVgap(10); p3.setLayout(layout3); p3.setBackground(Color.GREEN); p3.add(button9); p3.add(button10); p3.add(button11); p3.add(button12); myFrame.setSize(panelC_x, panelC_y); myFrame.getContentPane().add(p3, BorderLayout.SOUTH); myFrame.setBounds(350,0, total_x, total_y); //正常なformの表示 } //constructor void GLabel_Layout(JLabel label, LineBorder border){ label.setPreferredSize(new Dimension(80,20)); label.setBorder(border); label.setBackground(Color.lightGray); label.setOpaque(true); } void button_Layout(JButton button, LineBorder border){ button.setPreferredSize(new Dimension(80,20)); button.setBorder(border); } } //main class....GridLayout_new

    • ベストアンサー
    • Java
  • ボタンクリック後パネルを再描画repaintしたい

    ボタンをクリック後にstaticで保持していた値にプラス1して その値をパネルに再描画したいと思っています。 下記のプログラムだとボタンをクリックすると (<1<2<3<4)となって画像と数字が増えてしまいます 行いたいのは数字の部分だけが再描画されて カウントされて<3と表示されるだけになってもらいたい クラス1つ目 import java.awt.BorderLayout; import java.awt.Cursor; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Sample extends JFrame{ static int value=1; JPanel work = new JPanel(); public static void main(String[] args) { Sample frame = new Sample(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 200, 200, 100); frame.add(frame.createPanel(), BorderLayout.LINE_END); frame.setVisible(true); } public JPanel createPanel() { ImageIcon icon = new ImageIcon("./img/left.gif"); JLabel label = new JLabel(); Cursor c = new Cursor(Cursor.HAND_CURSOR); label.setCursor(c); label.setIcon(icon); JLabel strLabel = new JLabel(); String str = Integer.toString(value); strLabel.setText(str); work.add(label); work.add(strLabel); label.addMouseListener(new myListener()); return work; } class myListener extends MouseAdapter{ public void mouseClicked(MouseEvent e){ System.out.print(Sample.value++); createPanel().repaint(); setVisible(true); } } } クラス2つ目 import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class MouseAdapter implements MouseListener{ public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} }

    • ベストアンサー
    • Java
  • JButtonの座標(0,0)にアイコンをセット

    JButtonに以下のように、画像を書込む際、画像を一番は端(左上角)から乗せたいです。 つまり、ボタンの右上角(0,0)から乗せたいのです。 現状では、左端に隙間があいてしまいます。 この方法がご存知の方がおられましたら教えてください。 よろしくお願いいたします。 import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; public class Zoom extends JComponent { private static final long serialVersionUID = 1L; private ImageIcon icon = null; private int x = 0; private int y = 0; private int h = 0; private int w = 0; private double scale = 1.0d; public Zoom(ImageIcon icon, int x, int y, int w, int h) { super(); this.icon = icon; this.x = x; this.y = y; this.w = w; this.h = h; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.scale(scale, scale); g2.drawImage(icon.getImage(), x, y, w, h, this); } public static void main(String[] args) { ImageIcon icon = new ImageIcon("cherry.jpg"); Zoom zoom = new Zoom(icon,0,0,100,100); JButton b = new JButton(); JFrame f = new JFrame(); b.add(zoom); b.setPreferredSize(new Dimension(icon.getIconHeight(), icon.getIconWidth())); f.add(b); f.pack(); f.setVisible(true); } }

    • ベストアンサー
    • Java
  • Java ゲームプログラミング 例外

    JavaでSwing GUIプログラミングをしている者です。 初めて自分で1からプログラムを組んで、念願のゲームを作るということになりました。 自分が作っているゲームは、サバイバルゲームで自分の体力1000相手100でどれだけ倒せるか、というゲームです。 まだ、攻撃してダメージを与えるや、相手の数は一体だったりします。初の挑戦なのでご了承ください。 プログラム↓(長いかも) //ゲームサバイバル import java.awt.*; import javax.swing.*; import java.awt.event.*; class startgame { //スタート画面クラス public static JButton b1start; public static JPanel p1,p2; //p2はスタートボタンを押した後にでるJPanel public static JLabel l1; startgame start = new startgame(); startgame(){ p1 = new JPanel(); p1.setLayout(new BorderLayout()); p1.setBackground(Color.green); p2 = new JPanel(); p2.setLayout(new BorderLayout()); p2.setBackground(Color.green); b1start = new JButton("GAME START"); b1start.setForeground(Color.red); Font font = new Font(Font.SERIF,Font.BOLD,50); p1.add(b1start); l1 = new JLabel("SURVIVAL GAME"); l1.setFont(font); p1.add(l1,BorderLayout.NORTH); } } class Teki{ //敵クラス public static JLabel l0icon,l0p; public static ImageIcon Ticon; static int yourP; Teki teki = new Teki(); Teki(){ ImageIcon Ticon = new ImageIcon("./.jpeg"); //相手の画像 l0icon = new JLabel(Ticon); startgame.p2.add(l0icon,BorderLayout.CENTER); l0p = new JLabel("100"); //相手体力 int yourP = Integer.parseInt(l0p.getText()); //JLabelをint型に型変化 startgame.p2.add(l0p,BorderLayout.EAST); } } class Jibun{ //自分クラス public static JLabel l0p2; public static JButton kogeki; Jibun jibun = new Jibun(); Jibun(){ l0p2 = new JLabel("1000"); startgame.p2.add(l0p2,BorderLayout.WEST); //自分の体力 kogeki = new JButton("攻撃"); startgame.p2.add(kogeki,BorderLayout.WEST); //攻撃ボタン } } class maingame extends JFrame implements ActionListener{ public static void main(String[] args) { //はじめ maingame frame = new maingame(); frame.setBounds(200,200,700,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } maingame(){ super("サバイバルゲーム"); add(startgame.p1); startgame.b1start.addActionListener(this); Jibun.kogeki.addActionListener(this); } public void actionPerformed(ActionEvent e){ Object obj = e.getSource(); if(startgame.b1start == obj) { startgame.p1.add(startgame.p2); //スタートボタンの処理 } else if(Jibun.kogeki == obj){ System.out.println("攻撃した"); //攻撃処理 } } これをコンパイルすると、このような例外が出ます。↓ Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Unknow Source) at java.awt.Container.add(Unknow Source) at javax.swing.JFrame.addImpl(Unknow Source) at java.awtContainer.add(Unknow Source) at maingame.<init>(maingame.java:88) at maingame.main(maingame.java:77) となります。 これは、どうすれば解決できますか? 他にも、「ここはこうしたほうがいい」などがありましたら、どんどんご指摘ください。

    • ベストアンサー
    • Java
  • setIcon() の反対のようなものを探しています。

    こんばんは。GUIで、一秒ごとに画像を切り替える、スライドショーのようなものを作ってみたのですが、画像が切り替わらず、画像の上に画像が配置され、どんどん積み重なっていく感じになりました。以下がそのコードです。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class IconChange extends JFrame{ private Timer timer; private JLabel img; private int sec; private Icon[] icon=new ImageIcon[11]; private JPanel pane; public static void main(String[] args){ IconChange frame=new IconChange("画像チェンジ"); frame.pack(); frame.setDefaultCloseOperation(3); frame.setVisible(true); } public IconChange(String title){ super(title); sec = 0; pane=(JPanel)getContentPane(); pane.setPreferredSize(new Dimension(800,600)); pane.setOpaque(true); pane.setBackground(new Color(0x00000000, true)); img=new JLabel(new ImageIcon("images2/1.jpg")); pane.add(img); for(int i=0;i<=10;i++){ icon[i]=new ImageIcon("images2/"+i+".png"); } timer = new Timer(1000 , new Kirikae()); timer.start(); } class Kirikae implements ActionListener{ public void actionPerformed(ActionEvent e){ img.setIcon(icon[sec]); if(sec>=10){sec=0;} sec++; } } } JLabelに画像をセットしたら、前の画像と入れ替わり新しい画像がセットされるとおもったのですが、前の画像が残ったまま新しい画像がセットされます。新しい画像をセットする前に前の画像のセットを取り消したいのですが、いい方法がございましたらアドバイスお願いいたします。

    • ベストアンサー
    • Java
  • javaのBoxlayoutについて

    下記はBoxLayout用ののコードです。 Q1)Buttonのサイズを設定する方法がありますか? Q2)Buttonの配置を設定する方法がありますか? //===================================== import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.LayoutManager; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class AlignmentX extends JFrame{ JButton button1; JButton button2; JButton button3; public static void main(String[] args){ AlignmentX frame = new AlignmentX(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("タイトル"); frame.setVisible(true); } AlignmentX(){ button1 = new JButton("AAAAA"); button1.setAlignmentY(0.5f); button1.setPreferredSize(new Dimension(80,20)); button2 = new JButton("BBB"); button2.setAlignmentY(0.5f); button2.setPreferredSize(new Dimension(80,20)); button3 = new JButton("CC"); button3.setAlignmentY(0.5f); button3.setPreferredSize(new Dimension(80,20)); JPanel p = new JPanel(); p.setLayout((LayoutManager) new BoxLayout(p, BoxLayout.Y_AXIS)); p.add(button1); p.add(button2); p.add(button3); getContentPane().add(p, BorderLayout.CENTER); setBounds(10, 10, 300, 200); } } //以上,宜しくお願いします。

    • ベストアンサー
    • Java

専門家に質問してみよう