import java.awt.Dimension;
創新互聯公司主要從事成都做網站、成都網站設計、網頁設計、企業做網站、公司建網站等業務。立足成都服務沐川,十年網站建設經驗,價格優惠、服務專業,歡迎來電咨詢建站服務:13518219792
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Painter extends JFrame{
/**
*
*/
private static final long serialVersionUID = 8160427604782702376L;
CanvasPanel canvas = new CanvasPanel();;
public Painter() {
super("Star");
this.add(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Painter();
}
}
class CanvasPanel extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -4642528854538741028L;
private JButton[] btn = new JButton[4];
private String[] btn_name = {"+", "-", "R", "L"};
private int center_x = 200, center_y = 200, radius = 100, degree = 0;
public CanvasPanel() {
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for(int i = 0; i 4; i++) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160 + i * 60, 425, 50, 50);
btn[i].addActionListener(this);
this.add(btn[i]);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i 5; i++) {
g.drawLine( (int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i))),
(int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i + 144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i + 144))));
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == "+") {
if(radius 200)
radius += 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius 0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "R") {
degree = (degree + 2) % 360;
repaint();
} else if(e.getActionCommand() == "L") {
degree = (degree - 2) % 360;
repaint();
}
}
}
貪吃蛇游戲 望采納
import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Snake extends JFrame implements KeyListener{
int Count=0;
Button[][] grid = new Button[20][20];
ArrayListPoint snake_list=new ArrayListPoint();
Point bean=new Point(-1,-1);//保存隨機豆子【坐標】
int Direction = 1; //方向標志 1:上 2:下 3:左 4:右
//構造方法
public Snake()
{
//窗體初始化
this.setBounds(400,300,390,395);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout f=new GridLayout(20,20);
this.getContentPane().setBackground(Color.gray);
this.setLayout(f);
//初始化20*20個按鈕
for(int i=0;i20;i++)
for(int j=0;j20;j++)
{
grid[i][j]=new Button();
this.add(grid[i][j]);
grid[i][j].setVisible(false);
grid[i][j].addKeyListener(this);
grid[i][j].setBackground(Color.blue);
}
//蛇體初始化
grid[10][10].setVisible(true);
grid[11][10].setVisible(true);
grid[12][10].setVisible(true);
grid[13][10].setVisible(true);
grid[14][10].setVisible(true);
//在動態數組中保存蛇體按鈕坐標【行列】信息
snake_list.add(new Point(10,10));
snake_list.add(new Point(11,10));
snake_list.add(new Point(12,10));
snake_list.add(new Point(13,10));
snake_list.add(new Point(14,10));
this.rand_bean();
this.setTitle("總分:0");
this.setVisible(true);
}
//該方法隨機一個豆子,且不在蛇體上,并使豆子可見
public void rand_bean(){
Random rd=new Random();
do{
bean.x=rd.nextInt(20);//行
bean.y=rd.nextInt(20);//列
}while(snake_list.contains(bean));
grid[bean.x][bean.y].setVisible(true);
grid[bean.x][bean.y].setBackground(Color.red);
}
//判斷擬增蛇頭是否與自身有碰撞
public boolean is_cross(Point p){
boolean Flag=false;
for(int i=0;isnake_list.size();i++){
if(p.equals(snake_list.get(i) )){
Flag=true;break;
}
}
return Flag;
}
//判斷蛇即將前進位置是否有豆子,有返回true,無返回false
public boolean isHaveBean(){
boolean Flag=false;
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
Point p=null;
if(Direction==1)p=new Point(x-1,y);
if(Direction==2)p=new Point(x+1,y);
if(Direction==3)p=new Point(x,y-1);
if(Direction==4)p=new Point(x,y+1);
if(bean.equals(p))Flag=true;
return Flag;
}
//前進一格
public void snake_move(){
if(isHaveBean()==true){//////////////有豆子吃
Point p=new Point(bean.x,bean.y);//【很重要,保證吃掉的是豆子的復制對象】
snake_list.add(0,p); //吃豆子
grid[p.x][p.y].setBackground(Color.blue);
this.Count++;
this.setTitle("總分:"+Count);
this.rand_bean(); //再產生一個豆子
}else{///////////////////無豆子吃
//取原蛇頭坐標
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
//根據蛇頭坐標推算出擬新增蛇頭坐標
Point p=null;
if(Direction==1)p=new Point(x-1,y);//計算出向上的新坐標
if(Direction==2)p=new Point(x+1,y);//計算出向下的新坐標
if(Direction==3)p=new Point(x,y-1);//計算出向左的新坐標
if(Direction==4)p=new Point(x,y+1);//計算出向右的新坐標
//若擬新增蛇頭碰壁,或纏繞則游戲結束
if(p.x0||p.x19|| p.y0||p.y19||is_cross(p)==true){
JOptionPane.showMessageDialog(null, "游戲結束!");
System.exit(0);
}
//向蛇體增加新的蛇頭坐標,并使新蛇頭可見
snake_list.add(0,p);
grid[p.x][p.y].setVisible(true);
//刪除原蛇尾坐標,使蛇尾不可見
int x1=snake_list.get(snake_list.size()-1).x;
int y1=snake_list.get(snake_list.size()-1).y;
grid[x1][y1].setVisible(false);
snake_list.remove(snake_list.size()-1);
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP Direction!=2) Direction=1;
if(e.getKeyCode()==KeyEvent.VK_DOWN Direction!=1) Direction=2;
if(e.getKeyCode()==KeyEvent.VK_LEFT Direction!=4) Direction=3;
if(e.getKeyCode()==KeyEvent.VK_RIGHT Direction!=3) Direction=4;
}
@Override
public void keyReleased(KeyEvent e) { }
@Override
public void keyTyped(KeyEvent e) { }
public static void main(String[] args) throws InterruptedException {
Snake win=new Snake();
while(true){
win.snake_move();
Thread.sleep(300);
}
}
}
視具體代碼情況而定,視是否引用外部包而定,視個人編程書寫習慣而定。
具體的來說,Python可以將任意長的代碼寫在一行上(其實好像java也可以這么干)。
所以行數說明不了什么問題。
平均來看,Java要打100行的代碼,Python大約需要50行代碼左右。
另外Python在某些問題上,處理比Java要更消耗資源,不過Python用了很多多線程優化,所以說起來,單機的運行速度不相上下,但在服務器上運行就能看出來Java是有明顯優勢的。
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public boolean equals(Object obj){
if( ! (obj instanceof Person)){
return false;
}
Person per1=this;
Person per2=(Person)obj;
boolean flag=false;
if(per1==per2){
flag=true;
}
else{
if(per1.name.equals(per2.name)per1.age==per2.age){
flag=true;
}
}
return flag;
}
public String toString(){
return "姓名:"+this.name+",年齡:"+this.age;
}
}
public class OODemo17{
public static void main(String []args){
Person p1=new Person("張三",23);
Person p2=new Person("張三",23);
System.out.println(p1.equals(p2));
Person p3=p2;
System.out.println(p2.equals(p3));
System.out.println(p1.equals("a string"));
}
}
輸出結果:
true
true
false
新聞名稱:100行Java代碼的簡單介紹
網站URL:http://m.kartarina.com/article4/dodeoie.html
成都網站建設公司_創新互聯,為您提供用戶體驗、靜態網站、品牌網站設計、外貿網站建設、網站改版、企業網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯