Java Swing图形界面编程基础

显示一个窗体

public class Breakout{
  public static void main(String[] args) {
    BreakoutFrame frame = new BreakoutFrame();
    frame.setVisible(true);
  }
}

class BreakoutFrame extends JFrame {
  public static final int HEIGHT = 900;
  public static final int WIDTH = 1600;
  public static final int LOCATION_X = 50;
  public static final int LOCATION_Y = 100;

  public BreakoutFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(LOCATION_X, LOCATION_Y);
    setSize(WIDTH, HEIGHT);
    setResizable(false);
  }
}

画一个圆

不能将图形直接画在一个JFrame上,需要先放一个JPanel到JFrame上,然后把图形画在JPanel上。通过paintComponent方法;paintComponent当窗口发生变化时自动重绘。

package org.example;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import javax.imageio.*;

public class Breakout{
  public static void main(String[] args) {
    BreakoutFrame frame = new BreakoutFrame();
    frame.setVisible(true);
  }
}

class BreakoutFrame extends JFrame {
  public static final int HEIGHT = 900;
  public static final int WIDTH = 1600;
  public static final int LOCATION_X = 50;
  public static final int LOCATION_Y = 100;

  public BreakoutFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(LOCATION_X, LOCATION_Y);
    setSize(WIDTH, HEIGHT);
    setResizable(false);
    BreakoutPanel panel = new BreakoutPanel();
    add(panel);
  }
}

class BreakoutPanel extends JPanel {
  private Ball ball = new Ball(Color.red);
  public BreakoutPanel() {
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    ball.draw(g2);
  }
}

class Ball {
  public static final int SIZE = 30;
  public static final int START_X = 200;
  public static final int START_Y = 600;
  private Color color;
  private int x, y;

  public Ball(Color color) {
    this.color = color;
    x = START_X;
    y = START_Y;
  }

  public void draw(Graphics2D g2) {
    g2.setPaint(color);
    Ellipse2D e = new Ellipse2D.Double(x, y, SIZE, SIZE);
    g2.fill(e);
  }
}

显示更多图形

package org.example;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import javax.imageio.*;

public class Breakout{
  public static void main(String[] args) {
    BreakoutFrame frame = new BreakoutFrame();
    frame.setVisible(true);
  }
}

class BreakoutFrame extends JFrame {
  public static final int HEIGHT = 900;
  public static final int WIDTH = 1600;
  public static final int LOCATION_X = 50;
  public static final int LOCATION_Y = 100;

  public BreakoutFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(LOCATION_X, LOCATION_Y);
    setSize(WIDTH, HEIGHT);
    setResizable(false);
    BreakoutPanel panel = new BreakoutPanel();
    add(panel);
  }
}

class BreakoutPanel extends JPanel {
  public static final int NUM_BRICK_ROWS = 12;
  public static final int NUM_BRICK_COLUMNS = 8;
  private Ball ball = new Ball(Color.red);
  private ArrayList<Brick> bricks = new ArrayList<>();
  private Paddle paddle = new Paddle(Color.BLUE);
  private Player player = new Player();

  public BreakoutPanel() {
    for (int row = 0; row < NUM_BRICK_ROWS; row++) {
      for (int col = 0; col < NUM_BRICK_COLUMNS; col++) {
        bricks.add(new Brick(row, col, getRandomColor()));
      }
    }
  }

  public Color getRandomColor() {
    Color color = new Color((int) (Math.random() * 256),
(int) (Math.random() * 256), (int) (Math.random() * 256));
    if (getBackground().equals(color)) {
      return Color.RED;
    }
    return color;
  }

  public void showMessage(String s, Graphics2D g2) {
    Font myFont = new Font("SansSerif", Font.BOLD + Font.ITALIC, 40);
    g2.setFont(myFont);
    g2.setColor(Color.RED);
    Rectangle2D textBox = myFont.getStringBounds(s,
                g2.getFontRenderContext());
    g2.drawString(s, (int) (getWidth() / 2 - textBox.getWidth()/ 2),
                (int) (getHeight() / 2 - textBox.getHeight()));
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    if (bricks.size() == 0) {
      showMessage("YOU WIN!", g2);
    } else if (!player.isAlive()) {
       showMessage("GAME OVER!", g2);
    } else {
      ball.draw(g2);
      paddle.draw(g2);
      for (Brick brick : bricks) {
        brick.draw(g2);
      }
    }
    player.draw(g2);
  }
}

class Ball {
  public static final int SIZE = 30;
  public static final int START_X = 200;
  public static final int START_Y = 600;
  private Color color;
  private int x, y;

  public Ball(Color color) {
    this.color = color;
    x = START_X;
    y = START_Y;
  }

  public void draw(Graphics2D g2) {
    g2.setPaint(color);
    Ellipse2D e = new Ellipse2D.Double(x, y, SIZE, SIZE);
    g2.fill(e);
  }
}

class Paddle {
  public static final int WIDTH = 150;
  public static final int HEIGHT = 30;
  public static final int START_X = 200;
  public static final int START_Y = 830;
  private Color color;
  private int x, y;

  public Paddle(Color color) {
    this.color = color;
    x = START_X;
    y = START_Y;
  }

  public void draw(Graphics2D g2) {
    g2.setPaint(color);
    Rectangle2D r = new Rectangle2D.Double(x, y, WIDTH, HEIGHT);
    g2.fill(r);
  }
}

class Brick  {
  public static final int HEIGHT = 40;
  public static final int WIDTH = 100;
  public static final int BRICK_H_GAP = 2;
  public static final int BRICK_V_GAP = 2;
  private int x, y;
  private Color   color;

  public Brick(int row, int col, Color color) {
    this.color = color;
    x = BRICK_H_GAP + col * (BRICK_H_GAP + Brick.WIDTH);
    y = BRICK_V_GAP + row * (BRICK_V_GAP + Brick.HEIGHT);
  }

  public void draw(Graphics2D g2) {
    g2.setPaint(color);
    Rectangle2D r = new Rectangle2D.Double(x, y, WIDTH, HEIGHT);
    g2.fill(r);
  }
}

class Player {
  public static int INITIAL_NUM_LIVES = 3;
  public static int IMAGE_DISTANCE = 45;
  public static int IMAGE_Y = 750;
  private int numLives;

  public Player() {
    this.numLives = INITIAL_NUM_LIVES;
  }

  public void killPlayer() {
    numLives--;
  }

  public boolean isAlive() {
    return (numLives > 0);
  }

  public void draw(Graphics2D g2) {
    try {
      Image image = ImageIO.read(new File("cart.jpg"));
      for (int x = 0; x < numLives; x++) {
        g2.drawImage(image, x * IMAGE_DISTANCE+1200, IMAGE_Y, null);
      }
    } catch (Exception my) {}
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值