我在学校为我的java阶级玩.,需要写上档案。 我从我以前提出的一项工作方案中抄录了一些法典,但每当我操作该守则时,我就会发现一个“接触”错误。 我的java教师说,我完全读写,可以查阅档案,但它仍然发现错误。
他的分数。 确实存在 t,其具体细节就是这样。
错误在于马克·马克特菲利的方法,在印刷作者的初创情况下是很奇怪的。 也可在支票上进行,但目前正在对这种方法发表意见。
我对该网站说几句新话,因此我不敢肯定如何制定法典,因此结果将十分dy。
Exception in thread "AWT-EventQueue-1" java.security.AccessControlException:
access denied ("java.io.FilePermission" "Hiscore.txt" "write")
Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TowerDefence extends JApplet implements MouseListener {
int xpos, ypos, money, attackspawn, attackcount, lives, attackerhealth, hiscore,
laser; // x,y location of current square
private DrawingArea canvas; // JPanel canvas on which to draw grid
private int[][] numbers;
private int[][] damage;
JPanel startscreen;
File ifile, ofile, qfile;
Scanner input, output, questionreader;
boolean start, play, checkhiscore, fileexists, end, inquestionstate;
long totaldamage;
Timer timer;
boolean [][] moved;
JButton button;
public File fargle;
PrintWriter makesOutput;
//JButton begin;
// Constructor
public TowerDefence( ) {
inquestionstate = false;
JButton begin = new JButton ("BEGIN");
//begin = new JButton("BEGIN");
checkhiscore = false;
xpos = ypos = attackcount = laser = 4;
button = new JButton("Double Damage for 15 seconds");
money = 200;
attackspawn = 0;
totaldamage = 0;
lives = 10;
attackerhealth = 24;
fileexists = true;
numbers = new int[30][30];
damage = new int [20][20];
moved = new boolean [20][20];
Timer timer;
end = false;
for (int i = 0; i< 20; i++){
for (int j = 0; j< 20; j++){
moved[i][j] = false;
damage [i][j] = 0;
}
}
for (int i = 0; i<30; i++){
for (int j = 0; j<30; j++){
numbers[i][j] = 0;
}
}
start = false;
play = false;
ifile = new File("map.txt");
ofile = new File ("hiscore.txt");
qfile = new File("questions.txt");
RepaintAction action = new RepaintAction();
timer = new Timer(1000, action);
timer.start();
}
private class RepaintAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
repaint(); // Call the repaint() method in the panel class.
}
}// MANDATORY: required initialization of JApplet
// MANDATORY: required initialization of JApplet
public void init ( ) {
canvas = new DrawingArea(); // create a new Drawing Area
setContentPane(canvas); // connect the canvas to JApplet
canvas.setBackground(Color.lightGray); // make the background lightGray
canvas.addMouseListener(this);
canvas.requestFocus(); // focus keyboard on JApplet
}
// MANDATORY: Define DrawingArea as a JPanel
class DrawingArea extends JPanel {
JButton begin = new JButton ("BEGIN");
JButton upgrade = new JButton ("upgrade");
JComboBox selectmap = new JComboBox();
String op1, op2 = "yodel";
String askquestion;
String option = "ocean";
String findplace = "";
JRadioButton option1, option2;
JLabel question;
ButtonGroup group = new ButtonGroup();
JFrame questionframe = new JFrame();
public void paintComponent(Graphics g) {
super.paintComponent(g); // MANDATORY: Must be first method called.
if (!play&&!end){
ReadIt1();
StartScreen(g);
StartButton();
}
if (start){
ReadIt();
}
if (play){
ChangeAttackers();
drawGrid(g);
Attack(g);
DamageCheck();
if (attackspawn % 15 == 0){
SpawnAttackers();
ReadQuestions();
}
LifeCheck(g);
}
// CheckOutputFile();
//if (fileexists){
MakeOutputFile();
//}
if (end){
LifeCheck(g);
}
}
public void ReadQuestions(){
if(!inquestionstate){
try{
questionreader = new Scanner(qfile);
}catch(FileNotFoundException e){
System.out.println("ERROR: cannot open file map.txt");
System.exit(1);
}
while (!findplace.equalsIgnoreCase(op2)){
findplace = questionreader.nextLine();
}
askquestion = questionreader.nextLine();
op1 = questionreader.nextLine();
op2 = questionreader.nextLine();
System.out.println(askquestion);
System.out.println(op1);
System.out.println(op2);
question = new JLabel(askquestion);
option1 = new JRadioButton(op1);
option2 = new JRadioButton(op2);
questionframe.setLayout(new GridLayout(0,1));
questionframe.removeAll();
questionframe.getContentPane().add(question);
questionframe.getContentPane().add(option1);
questionframe.getContentPane().add(option2);
questionframe.setVisible(true);
inquestionstate = true;
}
}
public void ReadIt1(){
if (option.equalsIgnoreCase("ocean")){
ifile = new File("oceanmap.txt");
}
else if (option.equalsIgnoreCase("canyon")){
ifile = new File("map.txt");
}
try{
input = new Scanner(ifile);
}catch(FileNotFoundException e){
System.out.println("ERROR: cannot open file map.txt");
System.exit(1);
}
for (int i = 0; i<20; i++){
for (int j = 0; j < 20; j++){
numbers [j+5][i+5] = input.nextInt();
}
}
input.close();
}//end method ReadIt
public void StartScreen(Graphics g){
for (int row = 0; row < 20; row ++){
for (int col = 0; col < 20; col ++){
if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("canyon")){
g.setColor(Color.green);
}
else if (option.equalsIgnoreCase("canyon")){
g.setColor(Color.black);
}
else if (option.equalsIgnoreCase("ocean")&&numbers[row+5][col+5] == 0){
g.setColor(Color.yellow);
}
else if (option.equalsIgnoreCase("ocean")){
g.setColor(Color.cyan);
}
g.fillRect(30 + col *22, 30 + row * 22, 21, 21);
g.setFont( new Font ("Serif", Font.BOLD, 48));
g.setColor(Color.red);
g.drawString("TOWER DEFENCE", 10,100);
}
}
}
public void StartButton(){
selectmap.addItem("Ocean");
selectmap.addItem("Canyon");
begin.setVisible(true);
begin.addActionListener(new ButtonListener());
this.setLayout(null);
this.add(begin);
begin.setBounds(0,468,500,32);
selectmap.setVisible(true);
this.add(selectmap);
selectmap.setBounds(300,300, 100,25);
}
// Draw the numbers on the grid
// Draw the Sudoku grid of rectangles
public void LifeCheck(Graphics g){
if(!inquestionstate){
JButton upgrade = new JButton ("upgrade");
upgrade.addActionListener(new ButtonListener());
for (int i = 0; i<20; i++){
for (int j = 19; j<20; j++){
if (numbers [i+5][j+5] == 2){
lives--;
numbers[i+5][j+5] = 1;
}
}
}
if (lives <=0){
play = false;
end = true;
for (int row = 0; row < 20; row ++){
for (int col = 0; col < 20; col ++){
if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("Canyon")){
g.setColor(Color.green);
}
else if (numbers [row+5][col+5] == 1&&option.equalsIgnoreCase("Canyon")){
g.setColor(Color.black);
}
else if (numbers [row+5][col+5] == 2){
g.setColor(Color.red);
}
else if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("Ocean")){
g.setColor(Color.yellow);
}
else if (numbers [row+5][col+5] == 1&&option.equalsIgnoreCase("Ocean")){
g.setColor(Color.cyan);
}
else if (numbers [row+5][col+5] == 3){
g.setColor(Color.gray);
}
g.fillRect(30 + col *22, 30 + row * 22, 21, 21);
g.setColor(Color.black);
g.drawString("Money: "+money, 420,480);
g.drawString("Lives: "+lives, 360,480);
g.drawString("Laser: "+laser, 240,480);
System.out.print(damage[row][col]);
}
}
this.setLayout(null);
this.add(upgrade);
upgrade.setBounds(0, 475, 100, 25);
Font myFont = new Font ("Arial", Font.BOLD, 25);
g.setFont(myFont);
if (option.equalsIgnoreCase("Ocean")){
g.setColor(Color.red);
}
else if (option.equalsIgnoreCase("Canyon")){
g.setColor(Color.cyan);
}
g.drawString("You have lost. You lasted for " + attackspawn + "seconds.",10,100);
}
}
}
public void SpawnAttackers(){
if(!inquestionstate){
int currentspawn = 0;
attackcount++;
attackerhealth+=3;
if (attackerhealth % 10 == 0){
attackerhealth += (int)(attackerhealth*1.1);
}
if (attackerhealth % 17 == 0){
attackerhealth += (int)(attackerhealth*1.2);
}
if (attackerhealth % 12 == 0){
attackerhealth += (int)(attackerhealth*1.3);
}
if (attackerhealth % 2 == 0){
attackerhealth += 1+(int)(attackerhealth * 1.05);
}
for (int i = 0; i < 20; i++){
for (int j = 0; j< 20; j++){
if (numbers [j+5][i+5] == 1){
numbers [j+5][i+5] = 2;
currentspawn++;
if (currentspawn == attackcount){
i = j = 100;
}
}
}
}
}
}
public void drawGrid(Graphics g) {
if(!inquestionstate){
money++;
attackspawn++;
}
upgrade.addActionListener(new ButtonListener());
for (int row = 0; row < 20; row ++){
for (int col = 0; col < 20; col ++){
if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("Canyon")){
g.setColor(Color.green);
}
else if (numbers [row+5][col+5] == 1&&option.equalsIgnoreCase("Canyon")){
g.setColor(Color.black);
}
else if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("Ocean")){
g.setColor(Color.yellow);
}
else if (numbers [row+5][col+5] == 1&&option.equalsIgnoreCase("Ocean")){
g.setColor(Color.cyan);
}
else if (numbers [row+5][col+5] == 2){
g.setColor(Color.red);
}
else if (numbers [row+5][col+5] == 3){
g.setColor(Color.gray);
}
g.fillRect(30 + col *22, 30 + row * 22, 21, 21);
g.setColor(Color.black);
g.drawString("Money: "+money, 420,480);
g.drawString("Lives: "+lives, 360,480);
g.drawString("Laser: "+laser, 240,480);
g.drawString("Wave: " + (int)(attackspawn/15), 170,480);
g.drawString("Total Damage: " + totaldamage, 170,20);
System.out.print(damage[row][col]);
}
System.out.println();
}
this.setLayout(null);
this.add(upgrade);
upgrade.setBounds(0, 475, 100, 25);
System.out.println();
}
public void ReadIt(){
if(!inquestionstate){
if (option.equalsIgnoreCase("ocean")){
ifile = new File("oceanmap.txt");
}
else if (option.equalsIgnoreCase("canyon")){
ifile = new File("map.txt");
}
try{
input = new Scanner(ifile);
}catch(FileNotFoundException e){
System.out.println("ERROR: cannot open file map.txt");
System.exit(1);
}
for (int i = 0; i<20; i++){
for (int j = 0; j < 20; j++){
numbers [j+5][i+5] = input.nextInt();
}
}
start = false;
play = true;
input.close();
}
}//end method ReadIt
public void DamageCheck(){
if(!inquestionstate){
for (int i = 0; i<20; i++){
for (int j = 0; j<20; j++){
if (damage[i][j] >=attackerhealth){
numbers[i+5][j+5] = 1;
damage[i][j] = 0;
money+=25;
}
}
}
}
}
public void Attack(Graphics g){
if(!inquestionstate){
for (int i = 0; i<20; i++){
for (int j = 0; j<20; j++){
if (numbers [i+5][j+5] == 3){
for (int o = -3; o<4; o++){
for (int e = -3; e<4; e++){
if (numbers [i+o+5][j+e+5] == 2){
damage [i+o][j+e] +=laser;
totaldamage+=laser;
g.setColor(Color.white);
g.drawLine(40+j*22, 40+i*22, 40+(j+e)*22, 40+(i+o)*22);
}
}
}
}
}
}
}
}
public void ChangeAttackers(){
if(!inquestionstate){
for (int i = 0; i<20; i++){
for (int j = 0; j<20; j++){
moved[i][j] = false;
}
}
for (int i = 0; i<20; i++){
for (int j = 0; j<20; j++){
if (numbers[i+5][j+5] == 2){
if (numbers[i+5][j+6] == 1&&moved[i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+5][j+6] = 2;
damage[i][j+1] = damage[i][j];
damage[i][j] = 0;
moved [i][j+1] = true;
System.out.println("Move Right");
}
else if ((i>13)||(i<9&&i>=7)){
if (numbers[i+4][j+5] == 1&&moved [i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+4][j+5] = 2;
moved [i-1][j] = true;
damage[i-1][j] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Up");
}
else if (numbers [i+6][j+5] == 1&&moved[i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+6][j+5] = 2;
moved [i+1][j] = true;
damage[i+1][j] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Down");
}
}
else if ((i<=13&&i>=10)||(i<7)){
if (numbers [i+6][j+5] == 1&&moved[i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+6][j+5] = 2;
moved [i+1][j] = true;
damage[i+1][j] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Down");
}
else if (numbers[i+4][j+5] == 1&&moved [i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+4][j+5] = 2;
moved [i-1][j] = true;
damage[i-1][j] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Up");
}
}
else if (j!=0){
if (numbers[i+5][j+4] == 1&&j!=0&&moved[i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+5][j+4] = 2;
moved[i][j-1] = true;
damage[i][j-1] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Left");
}
}
}
}
}
}
}
public void CheckOutputFile(){
int number;
try{
output = new Scanner(ofile);
}catch(FileNotFoundException e){
System.out.println("ERROR: cannot open file hiscore.txt");
fileexists = true;
// System.exit(1);
}
if (fileexists){
hiscore = output.nextInt();
checkhiscore = true;
output.close();
}
}
public void MakeOutputFile ( ) {
fargle = new File ("Hiscore.txt");
try {
makesOutput = new PrintWriter ( fargle );
}catch ( IOException e ){
System.out.println("Cannot create file to be written to");
System.exit(1);
}// end catch
makesOutput.println("
Hiscore : " + attackspawn);
makesOutput.println ( );
System.exit(1);
}// end method MakeOutput File
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
System.out.println("RAWR");
if (evt.getSource() == begin){
System.out.println("Button Pressed");
begin.setVisible(false);
selectmap.setVisible(false);
play = true;
start = true;
option = (String)selectmap.getSelectedItem();
System.out.println(option);
}
if (evt.getSource() == upgrade){
if (money>=50){
money+=-50;
laser+=1+(int)(laser*.1);
}
}
}
}
}
public void mousePressed(MouseEvent m){
int x = m.getX();
int y = m.getY();
for (int row = 0; row < 20; row ++){
for (int col = 0; col < 20; col ++){
if (x>30+col*22&&x<51+col*22&&y>30+row*22&&y<51+row*22){
if (numbers [row+5][col+5] == 1&&money>200){
numbers [row+5][col+5] = 0;
money+=-200;
repaint();
}
else if (numbers [row+5][col+5] == 0&&money>50){
System.out.println(row+" " +col);
money+=-50;
numbers [row+5][col+5] = 3;
repaint();
}
}
}
}
}
public void mouseClicked(MouseEvent m){}
public void mouseReleased(MouseEvent m){}
public void mouseEntered(MouseEvent m){}
public void mouseExited(MouseEvent m){}
}