About Java Timeline

Asked 1 years ago, Updated 1 years ago, 92 views

Counting starts at the beginning of the game, stops after 10 seconds, and game over screen cannot be displayed.

package luna.sexydesign;

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

class Subthread extensions Thread {

    private MyPanel2p2;

    publicSubthread(MyPanel2p2){
        this.p2 = p2;
    }

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

public class ScreenToucher extensions JFrame {

    inti = 0;
    static int width = 500;
    static int height = 500;
    private MyPanel1p1;

    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();
        MyPanel2p2 = new MyPanel2(this);

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

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

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

class MyPanel1 extensions JPanel {

    inti;

    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 extensions JPanel {

    static int width = 500;
    static int height = 500;
    static inti = 0;
    static intr = 60;
    static intx;
    static inty;

    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();
        maintain();
    }

    void MouseListener() {
        addMouseListener(newMouseAdapter(){
            public void mousePressed (MouseEvent) {
                double mouseX=e.getX();
                double mouseY=e.getY();
                if(mouthX>x&mouseX<x+2*r){
                    if(mouseY>y&mouseY<y+2*r){
                        maintain();
                        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{
            maintain();
        }
    }

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

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

}

class MyPanel3 extensions 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 swing

2022-09-30 11:44

1 Answers

Although not all of the items mentioned below will be solved, I would like to list two things that I can point out in terms of Java.(I don't know what the requirements are, so I don't mention it here.)

1.
Direct code to display the game over screen, MyPanel2#setPanel3()

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

However, the JavaDoc of the add method we call here is here.

(abbreviated) This method changes the layout-related information, thus disabling the component hierarchy.If the container is already listed, you will need to validate the hierarchy later in order to view the added components.

This validate method is hard to understand when translated into Japanese.

2.
The above mentioned MyPanel2#setPanel3() is doing this, but you may have noticed that it is being implemented halfway (as you may have noticed) I am still using a different thread than the drawing thread.
Therefore, you must run it in SwingUtilities.invokeLater.

In response to these two points, if you rewrite the method as follows, you will be able to confirm that the game over screen MyPanel3 appears first.

public void setPanel3(){
    SwingUtilities.invokeLater(()->{
        MyPanel3p3 = new MyPanel3();
        // ↓I don't know that the background color is the same, so I set it temporarily.
        // p3.setBackground (Color.WHITE);
        add(p3);
        validate();
    });
}

As I mentioned at the beginning, I didn't know how to display it from the text, so I left it as it is.
(Actually, I wondered if you wanted to replace the game screen (MyPanel2) with the game over screen (MyPanel3), but this seemed different from what the code meant.)


2022-09-30 11:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.