Skip to main content
どうせなので置換:Tab→Sp
ソース リンク
user10259
user10259
インデントを調整しました(ただし、まだスペースとタブが混在しています)
ソース リンク
nekketsuuu
  • 2.4万
  • 12
  • 53
  • 117
package luna.sexydesign;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Subthread extends Thread {

    private MyPanel2 p2;
    
    public Subthread(MyPanel2 p2) {
        this.p2 = p2;
    }

    @Override
    public void run() {// TODO Auto-generated constructor stub
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//      SwingUtilities.invokeLater(() ->
                p2.setPanel3();
    }
}

public class ScreenToucher extends JFrame {

    int i = 0;
    static int width = 500;
    static int height = 500;
    private MyPanel1 p1;

    public static void main(String args[]) {

         ScreenToucher frame = new ScreenToucher("Screen Toucher");
        frame.setVisible(true);
    }

    ScreenToucher(String title) {
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, width, height);

        p1 = new MyPanel1();
        MyPanel2 p2 = new MyPanel2(this);

        Subthread thread = new Subthread(p2);
        thread.start();

        add(p1, BorderLayout.NORTH);
        add(p2, BorderLayout.CENTER);
    }
 

    public void setCount(int count) {
        p1.setCount(count);
    }
    
}

class MyPanel1 extends JPanel {

    int i;

    private JLabel jl1;

    MyPanel1() {
        JPanel jp1 = new JPanel();
        jl1 = new JLabel();
        jp1.setBackground(Color.green);
        Integer j = new Integer(i);
        String text = j.toString();
        jl1.setText(text);
        jp1.add(jl1);
        add(jp1);
    }

    public void setCount(int count) {
        jl1.setText(Integer.toString(count));
    }

 }

class MyPanel2 extends JPanel {

    static int width = 500;
    static int height = 500;
    static int i = 0;
    static int r = 60;
    static int x;
    static int y;

    final static Color bc = Color.black;
    final static Color dc = Color.green;

    private ScreenToucher owner;

    public MyPanel2(ScreenToucher owner) {
        setBackground(Color.black);
        this.owner = owner;
        MouseListener();
        repaint();
    }

    void MouseListener() {
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                double mouseX = e.getX();
                double mouseY = e.getY();
                if (mouseX > x && mouseX < x + 2 * r) {
                    if (mouseY > y && mouseY < y + 2 * r) {
                        repaint();
                        owner.setCount(Count());
                    }
                }
            }
        });
    }
        
    protected void paintComponent(Graphics g) {
            super.paintComponent(g);
        x = (int) (Math.random() * width);
        y = (int) (Math.random() * height) + 30;
        if ((x < width - 2 * r) && (y < height - 2 * r)) {
            g.setColor(dc);
            g.fillOval(x, y, r, r);
        } else {
            repaint();
        }
    }

    int Count() {
        i += 100;
        return i;
    }

    public void setPanel3(){
        MyPanel3 p3 = new MyPanel3();
        add(p3);
    }

}

class MyPanel3 extends JPanel {

    public MyPanel3() {
        setBackground(new Color(0,0,0,100));
     }
    
    public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.white);
            g.drawString("GAME OVER", 100, 200);
        }
    }
}
package luna.sexydesign;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Subthread extends Thread {

    private MyPanel2 p2;
    
public Subthread(MyPanel2 p2) {
    this.p2 = p2;
}

@Override
public void run() {// TODO Auto-generated constructor stub
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//      SwingUtilities.invokeLater(() ->
                p2.setPanel3();
    }
}

public class ScreenToucher extends JFrame {

    int i = 0;
    static int width = 500;
    static int height = 500;
    private MyPanel1 p1;

    public static void main(String args[]) {

         ScreenToucher frame = new ScreenToucher("Screen Toucher");
        frame.setVisible(true);
   }

    ScreenToucher(String title) {
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, width, height);

        p1 = new MyPanel1();
        MyPanel2 p2 = new MyPanel2(this);

        Subthread thread = new Subthread(p2);
        thread.start();

        add(p1, BorderLayout.NORTH);
        add(p2, BorderLayout.CENTER);
}
 

    public void setCount(int count) {
        p1.setCount(count);
    }
    
}

class MyPanel1 extends JPanel {

    int i;

    private JLabel jl1;

    MyPanel1() {
        JPanel jp1 = new JPanel();
        jl1 = new JLabel();
        jp1.setBackground(Color.green);
        Integer j = new Integer(i);
        String text = j.toString();
        jl1.setText(text);
        jp1.add(jl1);
        add(jp1);
    }

    public void setCount(int count) {
        jl1.setText(Integer.toString(count));
    }

 }

class MyPanel2 extends JPanel {

    static int width = 500;
    static int height = 500;
    static int i = 0;
    static int r = 60;
    static int x;
    static int y;

    final static Color bc = Color.black;
    final static Color dc = Color.green;

    private ScreenToucher owner;

    public MyPanel2(ScreenToucher owner) {
        setBackground(Color.black);
        this.owner = owner;
        MouseListener();
        repaint();
   }

    void MouseListener() {
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                double mouseX = e.getX();
                double mouseY = e.getY();
                if (mouseX > x && mouseX < x + 2 * r) {
                    if (mouseY > y && mouseY < y + 2 * r) {
                        repaint();
                        owner.setCount(Count());
                    }
                }
            }
        });
    }
        
    protected void paintComponent(Graphics g) {
            super.paintComponent(g);
        x = (int) (Math.random() * width);
        y = (int) (Math.random() * height) + 30;
        if ((x < width - 2 * r) && (y < height - 2 * r)) {
            g.setColor(dc);
            g.fillOval(x, y, r, r);
        } else {
            repaint();
        }
    }

    int Count() {
        i += 100;
        return i;
    }

    public void setPanel3(){
        MyPanel3 p3 = new MyPanel3();
        add(p3);
    }

}

class MyPanel3 extends JPanel {

    public MyPanel3() {
        setBackground(new Color(0,0,0,100));
     }
    
    public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.white);
            g.drawString("GAME OVER", 100, 200);
        }
    }
package luna.sexydesign;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Subthread extends Thread {

    private MyPanel2 p2;
    
    public Subthread(MyPanel2 p2) {
        this.p2 = p2;
    }

    @Override
    public void run() {// TODO Auto-generated constructor stub
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//      SwingUtilities.invokeLater(() ->
        p2.setPanel3();
    }
}

public class ScreenToucher extends JFrame {

    int i = 0;
    static int width = 500;
    static int height = 500;
    private MyPanel1 p1;

    public static void main(String args[]) {
        ScreenToucher frame = new ScreenToucher("Screen Toucher");
        frame.setVisible(true);
    }

    ScreenToucher(String title) {
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, width, height);

        p1 = new MyPanel1();
        MyPanel2 p2 = new MyPanel2(this);

        Subthread thread = new Subthread(p2);
        thread.start();

        add(p1, BorderLayout.NORTH);
        add(p2, BorderLayout.CENTER);
    }

    public void setCount(int count) {
        p1.setCount(count);
    }
}

class MyPanel1 extends JPanel {

    int i;

    private JLabel jl1;

    MyPanel1() {
        JPanel jp1 = new JPanel();
        jl1 = new JLabel();
        jp1.setBackground(Color.green);
        Integer j = new Integer(i);
        String text = j.toString();
        jl1.setText(text);
        jp1.add(jl1);
        add(jp1);
    }

    public void setCount(int count) {
        jl1.setText(Integer.toString(count));
    }
}

class MyPanel2 extends JPanel {

    static int width = 500;
    static int height = 500;
    static int i = 0;
    static int r = 60;
    static int x;
    static int y;

    final static Color bc = Color.black;
    final static Color dc = Color.green;

    private ScreenToucher owner;

    public MyPanel2(ScreenToucher owner) {
        setBackground(Color.black);
        this.owner = owner;
        MouseListener();
        repaint();
    }

    void MouseListener() {
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                double mouseX = e.getX();
                double mouseY = e.getY();
                if (mouseX > x && mouseX < x + 2 * r) {
                    if (mouseY > y && mouseY < y + 2 * r) {
                        repaint();
                        owner.setCount(Count());
                    }
                }
            }
        });
    }
        
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        x = (int) (Math.random() * width);
        y = (int) (Math.random() * height) + 30;
        if ((x < width - 2 * r) && (y < height - 2 * r)) {
            g.setColor(dc);
            g.fillOval(x, y, r, r);
        } else {
            repaint();
        }
    }

    int Count() {
        i += 100;
        return i;
    }

    public void setPanel3(){
        MyPanel3 p3 = new MyPanel3();
        add(p3);
    }

}

class MyPanel3 extends JPanel {

    public MyPanel3() {
        setBackground(new Color(0,0,0,100));
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.white);
        g.drawString("GAME OVER", 100, 200);
    }
}
ソース リンク

Javaの、タイムラインについて

ゲームの始まりとともに、カウントが始まり、10秒後に停止、ゲームオーバー画面を、出すことができません。

package luna.sexydesign;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Subthread extends Thread {

    private MyPanel2 p2;
    
public Subthread(MyPanel2 p2) {
    this.p2 = p2;
}

@Override
public void run() {// TODO Auto-generated constructor stub
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//      SwingUtilities.invokeLater(() ->
                p2.setPanel3();
    }
}

public class ScreenToucher extends JFrame {

    int i = 0;
    static int width = 500;
    static int height = 500;
    private MyPanel1 p1;

    public static void main(String args[]) {

        ScreenToucher frame = new ScreenToucher("Screen Toucher");
        frame.setVisible(true);
   }

    ScreenToucher(String title) {
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, width, height);

        p1 = new MyPanel1();
        MyPanel2 p2 = new MyPanel2(this);

        Subthread thread = new Subthread(p2);
        thread.start();

        add(p1, BorderLayout.NORTH);
        add(p2, BorderLayout.CENTER);
}


    public void setCount(int count) {
        p1.setCount(count);
    }
    
}

class MyPanel1 extends JPanel {

    int i;

    private JLabel jl1;

    MyPanel1() {
        JPanel jp1 = new JPanel();
        jl1 = new JLabel();
        jp1.setBackground(Color.green);
        Integer j = new Integer(i);
        String text = j.toString();
        jl1.setText(text);
        jp1.add(jl1);
        add(jp1);
    }

    public void setCount(int count) {
        jl1.setText(Integer.toString(count));
    }

}

class MyPanel2 extends JPanel {

    static int width = 500;
    static int height = 500;
    static int i = 0;
    static int r = 60;
    static int x;
    static int y;

    final static Color bc = Color.black;
    final static Color dc = Color.green;

    private ScreenToucher owner;

    public MyPanel2(ScreenToucher owner) {
        setBackground(Color.black);
        this.owner = owner;
        MouseListener();
        repaint();
   }

    void MouseListener() {
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                double mouseX = e.getX();
                double mouseY = e.getY();
                if (mouseX > x && mouseX < x + 2 * r) {
                    if (mouseY > y && mouseY < y + 2 * r) {
                        repaint();
                        owner.setCount(Count());
                    }
                }
            }
        });
    }
        
    protected void paintComponent(Graphics g) {
            super.paintComponent(g);
        x = (int) (Math.random() * width);
        y = (int) (Math.random() * height) + 30;
        if ((x < width - 2 * r) && (y < height - 2 * r)) {
            g.setColor(dc);
            g.fillOval(x, y, r, r);
        } else {
            repaint();
        }
    }

    int Count() {
        i += 100;
        return i;
    }

    public void setPanel3(){
        MyPanel3 p3 = new MyPanel3();
        add(p3);
    }

}

class MyPanel3 extends JPanel {

    public MyPanel3() {
        setBackground(new Color(0,0,0,100));
     }
    
    public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.white);
            g.drawString("GAME OVER", 100, 200);
        }
    }