paintComponent(Graphics) はオーバーライドされます。しかし、プログラムを起動すると、アプリケーションのウィンドウの場所にあった古い別のウィンドウのコンテンツが表示されるだけです。最小化、最大化、クリック、入力などをいくつか試しましたが、同じままです
コードのリクエストに対する回答 では 、クラスのコードです。メインクラスではなく、メインクラスが違います。パッケージ表示;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;import java.io.*;
import java.awt.event.*;//@Suppress Warnings 'unused' to Board
import Working.*;
public class Board extends JPanel implements Const{
/**
*
*/
private static final long serialVersionUID = 1L;
public int UDocks, CDocks, URun, CRun, UC, CC;
public boolean isToss, isBat, isOdd;
public ENGINE engine;
public BufferedImage one, two, three, four, five, six, dock, Null;
public int STATUS;
public BufferedImage UHand, CHand;
public String temp;
public JFrame Final;public Dimension preferredsize=new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
public Board(){
engine=new ENGINE(this);
UDocks=CDocks=3;
URun=CRun=UC=CC=0;
this.STATUS=GAME_STARTED;
this.addKeyListener(engine);this.setBackground(Color.green);
}
@Override
public void paintComponent(Graphics graph)
{
super.paintComponent(graph);
JOptionPane.showConfirmDialog(null, "HI", "HU", JOptionPane.PLAIN_MESSAGE);
graph.setColor(Color.black);
String a;
if(STATUS==GAME_STARTED){
graph.setColor(Color.green);
graph.fillRect(0, 0, BOARD_WIDTH, BOARD_HEIGHT);graph.setColor(Color.black);
engine.StartGame();
STATUS = Const.GAME_FIRST_INNINGS ;
this.requestFocus();
}
if(STATUS!=GAME_ENDED){
if(isToss&&isBat)a="User won the toss and decided to bat first";
else if(isToss&&!isBat)a="User won the toss and decided to bowl first";
else if(!isToss&&!isBat)a="CPU won the toss and decided to bat first";
else a="CPU won the toss and decided to bowl first";
graph.drawString(a, 112, 54);
graph.drawString("User Number", 192, 153);
graph.drawString("Computer Number", 675, 142);
if(STATUS==GAME_FIRST_INNINGS)
a="Innings No.1\t Batting"+(isBat?"User\n":"CPU\n")+"Score:"+(isBat?URun:CRun)+"\tDocks:"+(isBat?UDocks:CDocks);
else a="Innings No.2\t Batting"+(isBat?"CPU\n":"User\n")+"Score:"+(isBat?CRun:URun)+"\tDocks"+(isBat?CDocks:UDocks)+"\nTarget:"+(isBat?URun+1:CRun+1);
graph.drawString(a, 600, 46);
graph.drawImage(UHand, 130, 180, 493, 608, this);
graph.drawImage(CHand, 618, 180, 980, 606, this);this.requestFocus();
}
else
{
if(isOdd)temp="User selected ODD during toss";else temp="User selected EVEN during toss";
if(isToss){
temp.concat("\nUser won the toss and chosed to");
temp.concat((isBat?"Bat first":"Bowl first"));
}else{
temp.concat("\nCPU won the toss and elected to");
temp.concat((isBat?"Bowl first":"Bat first"));
}
if(isBat)
{
temp.concat("\nUser scored "+URun);
temp.concat("\nCPU scored "+CRun);
}else{temp.concat("\nCPU scored "+CRun);temp.concat("\nUser scored "+URun);}
if(CRun>URun)temp.concat("\nCPU won");
else temp.concat("\nUser won");
JOptionPane.showConfirmDialog(null, temp, "Match Summary", JOptionPane.PLAIN_MESSAGE);this.requestFocus();
}
}
public boolean inttobool(int h)
{
if(!(h==0))return true;else return false;
}
public int booltoint(boolean h)
{
if(h)return 1;else return 0;
}
@Override
public Dimension getPreferredSize()
{
return preferredsize;
}
public void addPaneltoFrame(Container cont)
{
cont.setLayout(new BorderLayout());
cont.add("Center", this);
}
}
これが Const インターフェイスのコードです
package display;
public interface Const {
public final int GAME_STARTED=10;
public final int GAME_TOSS_OVER=20;
public final int GAME_FIRST_INNINGS=30;
public final int GAME_SECOND_INNINGS=40;
public final int GAME_ENDED=50;
public final boolean TOSS_USER=true;
public final boolean TOSS_CPU=false;
public final boolean FIRST_USER=true;
public final boolean FIRST_CPU=false;
public final int BOARD_WIDTH=1150;
public final int BOARD_HEIGHT=680;
public final int HAND_WIDTH=360;
public final int HAND_HEIGHT=425;
public final boolean GAME_NEEDS_SAVING=true;
public final boolean GAME_NEEDNOT_SAVE=false;
public int booltoint(boolean h);
public boolean inttobool(int h);
}
ここにメインのコードがあります
package display;
import javax.swing.*;
public class Main {
public static void main(String []args){
JFrame Fram=new JFrame("Hand Cricket By S.GOVIND BALAJI");
Fram.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Board h=new Board();
h.repaint();
h.addPaneltoFrame(Fram.getContentPane());
Fram.pack();
Fram.setVisible(true);
}
}
これがStartのコードです
package display;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import Working.*;
public class Start implements Const, ActionListener, KeyListener {
private JFrame Final;
private JPanel pan, jan;
private JLabel Text;
private JButton Odd, Even;
private ENGINE child;private boolean ISOdd, ISBat, Waiting=false, ISToss;
private int UNum, CNum;
public Start(ENGINE child)
{
this.child=child;
pan=new JPanel(new BorderLayout());jan=new JPanel(new BorderLayout());
Text=new JLabel("It's time for the toss.\n\t\t\t Odd or Even?");
Odd=new JButton("Odd");
Even=new JButton("Even");
Odd.addActionListener(this);
Even.addActionListener(this);
jan.add("West", Odd);
jan.add("East", Even);
pan.add("North", Text);
pan.add("South", jan);
Final=new JFrame("Welcome, Toss");
Final.setContentPane(pan);
Final.pack();
Final.setBackground(Color.pink);
Text.setForeground(Color.GREEN);Odd.setForeground(Color.GREEN);Odd.setForeground(Color.GREEN); Final.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent src) {
JButton main;
main=(JButton)src.getSource();
if(!Waiting){
if(main==Odd)
ISOdd=true;
else
ISOdd=false;
Waiting=true;
Final.setVisible(false);
Text.setText("Ok, Type the number for toss");
Odd.setVisible(false);
Even.setVisible(false);
Final.setVisible(true);
}else
{
ISBat=false;
if(main==Odd)
ISBat=true;
this.child.child.isBat=ISBat;
child.child.isToss=ISToss;
child.child.isOdd=ISOdd;
Final.setVisible(false);try{
this.child.Toss_OVER=true;
}catch(Throwable th){}
}
}
@Override
public int booltoint(boolean h) {
if(h)
return 1;else return 0;
}
@Override
public boolean inttobool(int h) {
if(h==1)return true;else return false;
}
@Override
public void keyPressed(KeyEvent arg) {
if(!Waiting)
return;
UNum=Static.CharToInt(arg.getKeyChar());
CNum=Static.Fluke(0, 6);
ISToss=false;
if(CNum+UNum%2==booltoint(ISOdd)){
JOptionPane.showConfirmDialog(null, "You won the Toss\nComputer put"+CNum+"\nYou Put"+UNum+"\nYour Call"+(ISOdd?"Odd":"Even")
+"\nActuall Result"+(ISOdd?"Odd":"Even")
, "Toss Result", JOptionPane.PLAIN_MESSAGE);
ISToss=true;
Final.setVisible(false);
Text.setText("Choose to bat or bowl");
Odd.setText("Bat");
Even.setText("Bowl");
Odd.setVisible(true);
Even.setVisible(true);
Final.setVisible(true);
}else{
JOptionPane.showConfirmDialog(null, "You lost in the Toss\nComputer put"+CNum+"\nYou Put"+UNum+"\nYour Call"+(ISOdd?"Odd":"Even")
+"\nActuall Result"+(ISOdd?"Even":"Odd")
, "Toss Result", JOptionPane.PLAIN_MESSAGE);
ISToss=false;
ISBat=inttobool(Static.Fluke(0, 1));
JOptionPane.showConfirmDialog(null, "Computer decided to "+(ISBat?"bat":"bowl")+"first", "Choosed", JOptionPane.PLAIN_MESSAGE);
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
画像は右のディレクトリにありますエンジンのコードは次のとおりです
package Working;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JOptionPane;
import display.*;
public class ENGINE implements KeyListener, Const {
//DATA MEMBERS
public Board child;public Start Toss;
public boolean Toss_OVER=false;
//heloo
public ENGINE(Board paremnt){
this.child=paremnt;
}
public void StartGame(){
Toss=new Start(this);
while(!Toss_OVER){}
return;
}
/*INHERITED INTERFACE IMPLEMENTATION*/
@Override
public int booltoint(boolean h) {
if(h)
return 1;else return 0;
}
@Override
public boolean inttobool(int h) {
if(h==1)return true;else return false;
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent KE) {
int Key=Static.CharToInt(KE.getKeyChar()), CKey;
//User first batting& First Innings
//*/*/*/*/*/*/*/***-*-*-*-*-*-*-*-*-*-
//-*---*--*--*-*-*--*-*-*-*-*-*-*-*-*-*-
//@Minimize
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
if(child.isBat&&child.STATUS==GAME_FIRST_INNINGS){
switch(Key)
{
case '0':if(child.UDocks==0){JOptionPane.showConfirmDialog(null, "You have no docks remaining.Type Again", "Warning", JOptionPane.WARNING_MESSAGE);return;}
child.UDocks--;
child.UHand=child.dock;
break;
case '1':
child.UHand=child.one;
break;
case '2':
child.UHand=child.two;
break;
case '3':
child.UHand=child.three;
break;
case '4':
child.UHand=child.four;
break;
case '5':
child.UHand=child.five;
break;
case '6':
child.UHand=child.six;
break;
}CKey=Static.Fluke(0, 6);
switch(CKey)
{
case '0':
child.CHand=child.dock;
break;
case '1':
child.CHand=child.one;
break;
case '2':
child.CHand=child.two;
break;
case '3':
child.CHand=child.three;
break;
case '4':
child.CHand=child.four;
break;
case '5':
child.CHand=child.five;
break;
case '6':
child.CHand=child.six;
break;
}
child.repaint();
if((Key==CKey))
{
JOptionPane.showConfirmDialog(null, "You are out", "Wicket", JOptionPane.PLAIN_MESSAGE);
child.STATUS=GAME_SECOND_INNINGS;
child.repaint();
return;
}
else
{
if(Key==0)
child.UC=CKey;
else
child.UC=Key;
child.URun+=child.UC;child.repaint();
}
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
/*-*--*-*-*-*-*-*----*-*--*-*-**-*-*-*
User First Batting&&&&&&Second INNINGS
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*/
else if(child.isBat&&child.STATUS==GAME_SECOND_INNINGS)
{
switch(Key)
{
case '0':
child.UHand=child.dock;
break;
case '1':
child.UHand=child.one;
break;
case '2':
child.UHand=child.two;
break;
case '3':
child.UHand=child.three;
break;
case '4':
child.UHand=child.four;
break;
case '5':
child.UHand=child.five;
break;
case '6':
child.UHand=child.six;
break;
}
if(child.CDocks!=0)
CKey=Static.Fluke(0, 6);else CKey=Static.Fluke(1, 6);
switch(CKey)
{
case '0':
child.CHand=child.dock;
break;
case '1':
child.CHand=child.one;
break;
case '2':
child.CHand=child.two;
break;
case '3':
child.CHand=child.three;
break;
case '4':
child.CHand=child.four;
break;
case '5':
child.CHand=child.five;
break;
case '6':
child.CHand=child.six;
break;
}
child.repaint();
if((Key==CKey))
{
JOptionPane.showConfirmDialog(null, "Computer is out", "Wicket", JOptionPane.PLAIN_MESSAGE);
child.STATUS=GAME_ENDED;
child.repaint();
return;
}
else
{
if(Key==0)
child.CC=Key;
else
child.CC=CKey;
child.CRun+=child.CC;child.repaint();
if(child.CRun>child.URun)child.STATUS=GAME_ENDED;
}
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
/*-*--*-*-*-*-*-*----*-*--*-*-**-*-*-*
User Second Batting&&&&&&Second INNINGS
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*/
else if(!child.isBat && child.STATUS==GAME_SECOND_INNINGS){
switch(Key)
{
case '0':if(child.UDocks==0){JOptionPane.showConfirmDialog(null, "You have no docks remaining.Type Again", "Warning", JOptionPane.WARNING_MESSAGE);return;}
child.UDocks--;
child.UHand=child.dock;
break;
case '1':
child.UHand=child.one;
break;
case '2':
child.UHand=child.two;
break;
case '3':
child.UHand=child.three;
break;
case '4':
child.UHand=child.four;
break;
case '5':
child.UHand=child.five;
break;
case '6':
child.UHand=child.six;
break;
}CKey=Static.Fluke(0, 6);
switch(CKey)
{
case '0':
child.CHand=child.dock;
break;
case '1':
child.CHand=child.one;
break;
case '2':
child.CHand=child.two;
break;
case '3':
child.CHand=child.three;
break;
case '4':
child.CHand=child.four;
break;
case '5':
child.CHand=child.five;
break;
case '6':
child.CHand=child.six;
break;
}
child.repaint();
if((Key==CKey))
{
JOptionPane.showConfirmDialog(null, "You are out", "Wicket", JOptionPane.PLAIN_MESSAGE);
child.STATUS=GAME_ENDED;
child.repaint();
return;
}
else
{
if(Key==0)
child.UC=CKey;
else
child.UC=Key;
child.URun+=child.UC;child.repaint();
if(child.URun>child.CRun)child.STATUS=GAME_ENDED;
}
}
//User second batting& First Innings
//*/*/*/*/*/*/*/***-*-*-*-*-*-*-*-*-*-
//-*---*--*--*-*-*--*-*-*-*-*-*-*-*-*-*-
//@Minimize
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
else if(!child.isBat&&child.STATUS==GAME_FIRST_INNINGS)
{
switch(Key)
{
case '0':
child.UHand=child.dock;
break;
case '1':
child.UHand=child.one;
break;
case '2':
child.UHand=child.two;
break;
case '3':
child.UHand=child.three;
break;
case '4':
child.UHand=child.four;
break;
case '5':
child.UHand=child.five;
break;
case '6':
child.UHand=child.six;
break;
}
if(child.CDocks!=0)
CKey=Static.Fluke(0, 6);else CKey=Static.Fluke(1, 6);
switch(CKey)
{
case '0':
child.CHand=child.dock;
break;
case '1':
child.CHand=child.one;
break;
case '2':
child.CHand=child.two;
break;
case '3':
child.CHand=child.three;
break;
case '4':
child.CHand=child.four;
break;
case '5':
child.CHand=child.five;
break;
case '6':
child.CHand=child.six;
break;
}
child.repaint();
if((Key==CKey))
{
JOptionPane.showConfirmDialog(null, "Computer is out", "Wicket", JOptionPane.PLAIN_MESSAGE);
child.STATUS=GAME_SECOND_INNINGS;
child.repaint();
return;
}
else
{
if(Key==0)
child.CC=Key;
else
child.CC=CKey;
child.CRun+=child.CC;child.repaint();
}
}
}
}
これが静的のコードです
package Working;
public class Static {
public static double Fluke(double x, double y)
{
double h;
h=Math.random() * (y-x);
h+=x;
return h;
}
public static int Fluke(int x, int y)
{
int h;
h=(int)Math.random() * (y-x);
h+=x;
return h;
}
public static float Fluke(float x, float y)
{
float h;
h=(float)Math.random() * (y-x);
h+=x;
return h;
}
public static int CharToInt(char x)
{
switch(x)
{
case '0':
return 0;
case '1':
return 1;
case '2':return 2;
case '3':return 3;
case '4':return 4;
case '5':return 5;
case '6':return 6;
default:return 6;
}
}
}