###前提
java swingでミニゲームを作っています。
###発生している問題
一応ソースを載せておきますが、エラーが起きているわけではありません。
ですが実行しても何も表示されません。
###該当のソースコード
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; @SuppressWarnings("serial") public class Main extends JFrame{ MainPanel p = new MainPanel(); public static void main(String[] args) { Main m = new Main(); m.setVisible(true); } Main() { setBounds(100, 150, 708, 532); setDefaultCloseOperation(EXIT_ON_CLOSE); add(p); } } @SuppressWarnings("serial") class MainPanel extends JPanel implements ActionListener , KeyListener ,Runnable{ private Thread gameLoop; private ActionKey goRightKey; private ActionKey goLeftKey; private ActionKey goUpKey; private ActionKey goDownKey; private JSlider levelsetter = new JSlider(0, 50); private JButton start = new JButton(); private PlayerPixel pp = new PlayerPixel(); private int difficultly; private int score; double x; private double y; MainPanel(){ goRightKey = new ActionKey(); goLeftKey = new ActionKey(); goUpKey = new ActionKey(); goDownKey = new ActionKey(); setFocusable(true); setLayout(null); addKeyListener(this); levelsetter.setMajorTickSpacing(5); levelsetter.setPaintTicks(true); levelsetter.setSnapToTicks(true); levelsetter.setLabelTable(levelsetter.createStandardLabels(10)); levelsetter.setPaintLabels(true); levelsetter.setFont(new Font("MSゴシック", Font.BOLD, 18)); levelsetter.setForeground(Color.BLUE); levelsetter.setBounds(525, 70, 150, 50); levelsetter.setOpaque(false); start.setContentAreaFilled(false); start.setBorderPainted(false); start.setFont(new Font("MSゴシック",Font.BOLD,20)); start.setForeground(Color.WHITE); start.setText("CLICKtoPLAY"); start.setBounds(490,400,190,40); start.addActionListener(this); add(levelsetter); add(start); } public void run() { while(true){ if(!goRightKey.isPressed()&&!goLeftKey.isPressed()) pp.horizontalAccelaration(0); else if(goRightKey.isPressed()) pp.horizontalAccelaration(1); else if(goLeftKey.isPressed()) pp.horizontalAccelaration(2); if(!goUpKey.isPressed()&&!goDownKey.isPressed()) pp.horizontalAccelaration(0); else if(goUpKey.isPressed()) pp.horizontalAccelaration(1); else if(goDownKey.isPressed()) pp.horizontalAccelaration(2); x = pp.getX(); y = pp.getY(); pp.update(); } } public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setBackground(Color.WHITE); g2.clearRect(0, 0, getWidth(), getHeight()); g2.setColor(Color.GRAY); g2.fillRect(500, 0,200,600); g2.setColor(Color.BLACK); g2.setFont(new Font("MSゴシック",Font.PLAIN,15)); g2.drawString("難易度(高いほど難しい)", 510, 50); g2.setFont(new Font("MSゴシック",Font.BOLD,30)); g2.drawString("SCORE", 520, 300); g2.setFont(new Font("MSゴシック",Font.PLAIN,30)); g2.drawString(String.valueOf(score), 520, 340); g2.fillRect((int)x,(int)y,PlayerPixel.WIDTH,PlayerPixel.HEIGHT); } @Override public void actionPerformed(ActionEvent e) { Object o = e.getSource(); if(o == start){ pp.setspeed(levelsetter.getValue()); gameLoop = new Thread(this); gameLoop.start(); } } @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) System.out.println(1231312); //goLeftKey.press(); if (key == KeyEvent.VK_RIGHT) goRightKey.press(); if (key == KeyEvent.VK_UP) goUpKey.press(); if(key == KeyEvent.VK_DOWN) goDownKey.press(); } @Override public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) goLeftKey.release(); if (key == KeyEvent.VK_RIGHT) goRightKey.release(); if (key == KeyEvent.VK_UP) goUpKey.release(); if(key == KeyEvent.VK_DOWN) goDownKey.release(); } @Override public void keyTyped(KeyEvent e) {} } class PlayerPixel { public static final int STOP = 0; public static final int RIGHT = 1; public static final int LEFT = 2; public static final int UP = 1; public static final int DOWN = 0; public static final int WIDTH = 40; public static final int HEIGHT = 40; private double speed; private double vectX; private double vectY; private double x; private double y; void horizontalAccelaration(int dir){ if(dir == STOP) vectX = 0; else if(dir == RIGHT) vectX = speed; else if(dir == LEFT) vectX = -speed; } void varticalAccelaration(int dir){ if(dir == STOP) vectY = 0; else if(dir == UP) vectY = speed; else if(dir == DOWN) vectY = -speed; } void update(){ double newX = x + vectX; //x座標の範囲指定 if(newX >= 500){ newX = 500; vectX = 0; } if(newX <= 0){ newX = 0; vectX = 0; } x = newX; double newY = y + vectY; //y座標の範囲指定 if(newY >= 500){ newY = 500; vectY = 0; } if(newY <= 0){ newY = 0; vectY = 0; } y = newY; } public void setspeed(int dif) { speed = 50 - dif / 5; } public double getX(){ return x; } public double getY(){ return y; } PlayerPixel(){ } } class ActionKey{ public static final int STATE_PRESSED = 0; public static final int STATE_RELEASED = 1; private int state; private int amount; ActionKey(){ reset(); } void reset(){ state = STATE_RELEASED; amount = 0; } void press(){ state = STATE_PRESSED; amount++; } void release(){ state = STATE_RELEASED; } boolean isPressed(){ if(amount != 0){ if(state == STATE_RELEASED) amount = 0; return true; } return false; } }
###補足情報(言語/FW/ツール等のバージョンなど)
Eclipse アプリケーションクライアントプロジェクト
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/08/31 08:31
2016/08/31 08:45
退会済みユーザー
2016/09/01 06:00