会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132363个问题




我用了数组来画炮弹,炮弹发射的角度是任意的,但运行了好几次却发现炮弹就成一条直线了,是哪里出了问题呢

image.png


Shell.java


package zzu.baizhan.Feb.Day20th;

import java.awt.*;
import java.util.Random;

public class Shell extends GameObject {
   double degree;

   public Shell() {
       x = 200;
       y = 200;
       width = 10;
       height = 10;
       speed = 3;

       degree = Math.random()*Math.PI*2;
   }

   public void draw(Graphics g) {
       Color c = g.getColor();
       g.setColor(Color.YELLOW);
       g.fillOval((int) x, (int) y, width, height);
       //炮弹沿着任意角度去飞
       x += speed * Math.cos(degree);
       y += speed * Math.sin(degree);

       if (x < 0 || x > Constant.GAME_WIDTH - width) {
           degree = Math.PI - degree;
       }
       if (y < 30 || y < Constant.GAME_HEIGHT - height) {
           degree = -degree;
       }

       g.setColor(c);
   }

}


package zzu.baizhan.Feb.Day20th;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Feb20thPlane extends JFrame {

    Image ball = Feb20thGameUtil.getImage("Images/ball.png");
    Image bg = Feb20thGameUtil.getImage("Images/bg.jpg");
    Image plane = Feb20thGameUtil.getImage("Images/plane.png");
    Plane p=new Plane(plane,250,250);

    Shell []shells=new Shell[50];

    int planeX=250;
    int planeY=250;

    @Override
    public void paint(Graphics g) { //自动被调用,相当于画笔

        g.drawImage(bg, 0, 0, null);
        g.drawImage(plane, planeX, planeY, null);
        p.drawSelf(g);
        for (int i = 0; i < shells.length; i++) {
            shells[i].draw(g);
        }
//        Color c=g.getColor();
//
//        Font f=g.getFont();
//        g.setColor(Color.BLUE);
//
//        g.drawLine(100,100,300,300);
//        g.drawRect(100,100,300,300);
//        g.drawOval(100,100,300,300);
////        g.drawArc(100,100,200,200,300,300);
//        g.fillRect(100,100,100,100);
//        g.setColor(Color.RED);
//        g.setFont(new Font("宋体",Font.BOLD,50));
//        g.drawString("我是谁?",200,200);
//
//        g.drawImage(ball,250,250,null);
//
//        g.setColor(c);
//        g.setFont(f);


    }
    class keyMonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            p.addDirection(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            p.minusDirection(e);
        }
    }

    public void launchFrame() {
        this.setTitle("Feb20th");
        this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
        this.setLocation(100, 100);
        setVisible(true);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        new PaintThread().start();//启动重画窗口的线程
        addKeyListener(new keyMonitor());//给窗口增加键盘的监听

        //初始化50个炮弹
        for (int i = 0; i < shells.length; i++) {
            shells[i] = new Shell();
        }
    }


    public static void main(String[] args) {
        Feb20thPlane f = new Feb20thPlane();
        f.launchFrame();
    }


    class PaintThread extends Thread {
        @Override
        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(40); //1s=1000ms
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 38161楼

我用了数组来画炮弹,炮弹发射的角度是任意的,但运行了好几次却发现炮弹就成一条直线了,是哪里出了问题呢


image.png



Shell.java

zzu.baizhan.Feb.Day20th;

java.awt.*;
java.util.Random;

Shell GameObject {
    ;

    Shell() {
        = ;
        = ;
        = ;
        = ;
        = ;

        = Math.()*Math.*;
    }

    draw(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.);
        g.fillOval(() , () , , );
        += * Math.();
        += * Math.();

        (< || > Constant.- ) {
            = Math.- ;
        }
        (< || < Constant.- ) {
            = -;
        }

        g.setColor(c);
    }
}


main:

zzu.baizhan.Feb.Day20th;

javax.swing.*;
java.awt.*;
java.awt.event.KeyAdapter;
java.awt.event.KeyEvent;
java.awt.event.WindowAdapter;
java.awt.event.WindowEvent;

Feb20thPlane JFrame {

    Image = Feb20thGameUtil.();
    Image = Feb20thGameUtil.();
    Image = Feb20thGameUtil.();
    Plane =Plane(,,);

    Shell []=Shell[];

    =;
    =;

    paint(Graphics g) { g.drawImage(, , , );
        g.drawImage(, , , );
        .drawSelf(g);
        (i = ; i < .; i++) {
            [i].draw(g);
        }
}
    keyMonitor KeyAdapter {
        keyPressed(KeyEvent e) {
            .addDirection(e);
        }

        keyReleased(KeyEvent e) {
            .minusDirection(e);
        }
    }

    launchFrame() {
        .setTitle();
        .setSize(Constant., Constant.);
        .setLocation(, );
        setVisible();

        .addWindowListener(WindowAdapter() {
            windowClosing(WindowEvent e) {
                System.();
            }
        });
        PaintThread().start();addKeyListener(keyMonitor());(i = ; i < .; i++) {
            [i] = Shell();
        }
    }


    main(String[] args) {
        Feb20thPlane f = Feb20thPlane();
        f.launchFrame();
    }


    PaintThread Thread {
        run() {
            () {
                repaint();
                {
                    Thread.(); } (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}



JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 38162楼

为什么System.out.println(id)打印的是[book: null],API上说返回null不是有效索引,怎么不是返回地址呢?

image.png

package cn.sxt.xmlproject2;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class TestDOMParse {
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException{
		//(1)创建一个DocumentBuilderFactory对象树的解析器
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		//(2)创建一个DocumentBuilder对象对象树的解析器
		DocumentBuilder db = dbf.newDocumentBuilder();
		//(3)通过DocumentBuilder的parse(...)方法将给定URL的内容解析为XML文档得到Document对象
		Document doc = db.parse("book.xml");
		//(4)通过getElementsByTagName(...)方法获取到子节点的列表
		NodeList bookList = doc.getElementsByTagName("book");
			System.out.println(bookList.getLength());
		//(5)通过for循环遍历每一个节点
			for(int i=0;i<bookList.getLength();i++){
				org.w3c.dom.Node  book = bookList.item(i);
				System.out.println(book);
				NamedNodeMap attrs = book.getAttributes();//得到属性的集合
				//(6)得到每个节点的属性和属性值
				for(int j=0;j<attrs.getLength();j++){
					org.w3c.dom.Node id = attrs.item(j);
					System.out.println(id);
					System.out.println("属性的名称:"+id.getNodeName());
				}
			}
			System.out.println("\n每个节点的名和节点的值");	
		
		//(7)得到每个节点的节点名和节点值
			for(int i=0;i<bookList.getLength();i++){
				org.w3c.dom.Node book = bookList.item(i);
				NodeList subNode = book.getChildNodes();
				System.out.println("子节点数的个数:"+subNode.getLength());
				for(int j=0;j<subNode.getLength();j++){
					org.w3c.dom.Node childNode = subNode.item(j);
					//System.out.println(childNode.getNodeName());
					short type = childNode.getNodeType();//获取代表基础类型的代码
					if(type==Node.ELEMENT_NODE){
						System.out.println("节点名称:"+childNode.getNodeName()+"\t"+childNode.getTextContent());
					}
				}
			}
		
	}

}

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 38163楼
Python 全系列/第一阶段:Python入门/序列 38164楼
Python 全系列/第一阶段:Python入门/序列 38165楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/Maven 38166楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 38168楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 38169楼

问题点:当不XML中的xsi:noNamespaceSchemalLocation="{books,xsd}",则报错元素 'books' 中不允许出现属性 'xsi:noNamespaceSchemalLocation',如后面图片,删除则正常

XML:

<?xml version="1.0" encoding="UTF-8"?>
<!--  <!DOCTYPE books[
 	<!ELEMENT books (book*)>
 	<!ELEMENT books (name,author,price)>
 	<!ELEMENT name (#PCDATA)>
 	<!ELEMENT author(#PCDATA)>
 	<!ELEMENT price (#PCDATA)>
 	<!ELEMENT book id CDATA #REQUIRED>
]> -->

<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemalLocation="{books,xsd}">
	<book id="1001">
		<name>java开发实战</name>
		<author>张小三</author>
		<price>98.5</price>
	</book>
	
	<book id="1002">
		<name>mysql删除库到跑路</name>
		<author>王一一</author>
		<price>88.5</price>
		
	</book>	
</books>

xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
	<xs:element name="books">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="book" maxOccurs="unbounded">
					<xs:complexType>
						<xs:sequence>
							
							<xs:element name="name" type="xs:string"></xs:element>
							<xs:element name="author" type="xs:string"></xs:element>
							<xs:element name="price" type="xs:double"></xs:element>
							
						</xs:sequence>
						<xs:attribute name="id" type="xs:positiveInteger" use="required"></xs:attribute>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

测试类:

package cn.sxt.xmlproject;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class Test {
	public static void main(String[] args) throws SAXException{
		//(1)创建SchemaFactory
		SchemaFactory sch = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		
		//(2)创建验证文件
		File schemaFile = new File("book.xsd");
		
		//(3)利用SchemaFactory工厂对象,接收验证的文件对象,生成Schema对象
		Schema schema = sch.newSchema(schemaFile);
		
		//(4)产生对此schema的验证器
		Validator validator = schema.newValidator();
		//(5)要验证的数据(准本数据源)
		Source source = new StreamSource("book.xml");
		//(6)开始验证
		try {
			validator.validate(source);
			System.out.println("成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("失败");
		}
		
	}

}


image.png

MyXML.rar


JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 38173楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 38174楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637