想用一周时间跟大家探讨一下我的小项目,知道所有问题都问大家会遭到鄙视,所以我会不断努力,跟他家共同交流,谢谢。
每天我会更新我的进展。
已经做了这些,在jpenel里画了图,100×100的格子,有红(资源),黄(土地),黑(障碍物)和绿(宝藏)四种颜色,不同的颜色代表不同的意思。
以下是我的代码,现在我的问题是,如何将不同的颜色的格子里放入不同的图片,我知道这么做肯定不是用ImageIcon可以实现的。
另外我觉得我的代码写的很糟糕,每种颜色是否应该用不同的类来编写,不应该直接用画笔画出来,还望请教。
剩下的问题很多,每天会更新。谢谢您的关注。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;public class Espace extends JFrame {
public static final int GAME_WIDTH = 930;
public static final int GAME_HEIGHT = 768;
public static final int Explorateur = 10;
public static final int ressource = 3000;
public static final int obstacle = 1000;
public static final int tresor = 1;
public void lauchFrame() {
this.setLocation(130, 0);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setBackground(Color.WHITE);
this.setTitle("Cherche le tresor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
LeftPanel leftPanel = new LeftPanel();
RightPanel rightPanel = new RightPanel(); this.getContentPane().add(rightPanel, BorderLayout.EAST);
this.getContentPane().add(leftPanel, BorderLayout.WEST);
this.setVisible(true);
} public static void main(String[] args) {
Espace tc = new Espace();
tc.lauchFrame();
}
class RightPanel extends JPanel{
public RightPanel() {
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
new Color(111, 0, 0), //color of highlight
new Color(111, 0, 0))); //color of shadow
this.setBackground(Color.LIGHT_GRAY);
this.setPreferredSize(new Dimension(GAME_WIDTH-GAME_HEIGHT-5,600));
//create un image d'un image file et ajoute quelque texte
ImageIcon img = new ImageIcon("D:\\projet\\GUI\\src\\cat.jpg");
JLabel pitLabel = new JLabel("How?",img, SwingConstants.CENTER);
pitLabel.setHorizontalTextPosition(SwingConstants.CENTER);
pitLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
pitLabel.setIconTextGap(10);
this.add(pitLabel);
JPanel button = new JPanel();
button.setLayout(new GridLayout(4,1,20,20));
button.add(new JButton("Start"));
button.add(new JButton("Pause"));
button.add(new JButton("Exit"));
button.add(new JButton("About"));
this.add(button);
}
} class LeftPanel extends JPanel{
public LeftPanel() {
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
new Color(0, 0, 0), // color of highlight
new Color(0, 0, 0))); // color of shadow
this.setPreferredSize(new Dimension(GAME_HEIGHT,77));
}
int num = 100;
int[][] grille = new int [num][num];
int i , j;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int border = 33, top = 20;
int width = 6,height = 6;
g.setColor(Color.ORANGE); //dessiner le sol
for( int i = 0; i < num; i++){
for(int j = 0;j < num; j++){
g.fillRect(i*(width+1)+border, j*(height+1)+top, width, height);
}
}
g.setColor(Color.RED); //dessiner le resources
for (int x = 0; x < 3000; x++) {
i = (int) (Math.random() * 100);
j = (int) (Math.random() * 100);
if(grille[i][j]==1){
x--;
continue;
}
else{
grille[i][j]=1;
g.fillRect(i * (width + 1) + border, j * (height + 1) + top, width, height);
}
}
g.setColor(Color.BLACK); //dessiner l'obstacle
for (int y = 0; y < 1000; y++) {
i = (int) (Math.random() * 100);
j = (int) (Math.random() * 100);
if(grille[i][j]==1){
y--;
continue;
}
else{
grille[i][j]=1;
g.fillRect(i * (width + 1) + border, j * (height + 1) + top, width, height);
}
}
g.setColor(Color.GREEN);//dessiner le tresor
i = (int) (Math.random() * 100);
j = (int) (Math.random() * 100);
grille[i][j]=1;
g.fillRect(i * (width + 1) + border, j * (height + 1) + top, width, height);
}
}
}
每天我会更新我的进展。
已经做了这些,在jpenel里画了图,100×100的格子,有红(资源),黄(土地),黑(障碍物)和绿(宝藏)四种颜色,不同的颜色代表不同的意思。
以下是我的代码,现在我的问题是,如何将不同的颜色的格子里放入不同的图片,我知道这么做肯定不是用ImageIcon可以实现的。
另外我觉得我的代码写的很糟糕,每种颜色是否应该用不同的类来编写,不应该直接用画笔画出来,还望请教。
剩下的问题很多,每天会更新。谢谢您的关注。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;public class Espace extends JFrame {
public static final int GAME_WIDTH = 930;
public static final int GAME_HEIGHT = 768;
public static final int Explorateur = 10;
public static final int ressource = 3000;
public static final int obstacle = 1000;
public static final int tresor = 1;
public void lauchFrame() {
this.setLocation(130, 0);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setBackground(Color.WHITE);
this.setTitle("Cherche le tresor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
LeftPanel leftPanel = new LeftPanel();
RightPanel rightPanel = new RightPanel(); this.getContentPane().add(rightPanel, BorderLayout.EAST);
this.getContentPane().add(leftPanel, BorderLayout.WEST);
this.setVisible(true);
} public static void main(String[] args) {
Espace tc = new Espace();
tc.lauchFrame();
}
class RightPanel extends JPanel{
public RightPanel() {
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
new Color(111, 0, 0), //color of highlight
new Color(111, 0, 0))); //color of shadow
this.setBackground(Color.LIGHT_GRAY);
this.setPreferredSize(new Dimension(GAME_WIDTH-GAME_HEIGHT-5,600));
//create un image d'un image file et ajoute quelque texte
ImageIcon img = new ImageIcon("D:\\projet\\GUI\\src\\cat.jpg");
JLabel pitLabel = new JLabel("How?",img, SwingConstants.CENTER);
pitLabel.setHorizontalTextPosition(SwingConstants.CENTER);
pitLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
pitLabel.setIconTextGap(10);
this.add(pitLabel);
JPanel button = new JPanel();
button.setLayout(new GridLayout(4,1,20,20));
button.add(new JButton("Start"));
button.add(new JButton("Pause"));
button.add(new JButton("Exit"));
button.add(new JButton("About"));
this.add(button);
}
} class LeftPanel extends JPanel{
public LeftPanel() {
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
new Color(0, 0, 0), // color of highlight
new Color(0, 0, 0))); // color of shadow
this.setPreferredSize(new Dimension(GAME_HEIGHT,77));
}
int num = 100;
int[][] grille = new int [num][num];
int i , j;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int border = 33, top = 20;
int width = 6,height = 6;
g.setColor(Color.ORANGE); //dessiner le sol
for( int i = 0; i < num; i++){
for(int j = 0;j < num; j++){
g.fillRect(i*(width+1)+border, j*(height+1)+top, width, height);
}
}
g.setColor(Color.RED); //dessiner le resources
for (int x = 0; x < 3000; x++) {
i = (int) (Math.random() * 100);
j = (int) (Math.random() * 100);
if(grille[i][j]==1){
x--;
continue;
}
else{
grille[i][j]=1;
g.fillRect(i * (width + 1) + border, j * (height + 1) + top, width, height);
}
}
g.setColor(Color.BLACK); //dessiner l'obstacle
for (int y = 0; y < 1000; y++) {
i = (int) (Math.random() * 100);
j = (int) (Math.random() * 100);
if(grille[i][j]==1){
y--;
continue;
}
else{
grille[i][j]=1;
g.fillRect(i * (width + 1) + border, j * (height + 1) + top, width, height);
}
}
g.setColor(Color.GREEN);//dessiner le tresor
i = (int) (Math.random() * 100);
j = (int) (Math.random() * 100);
grille[i][j]=1;
g.fillRect(i * (width + 1) + border, j * (height + 1) + top, width, height);
}
}
}
虽然panel可以开双缓冲,但是通常情况下paint过程里面只有一句代码就可以了
就是把内存画布的内容更新到UI。无论你调用何种设备(画笔、刷子、材质图片…)都是画到内存画布的。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;public class Espace extends JFrame {
private static final long serialVersionUID = 1L;
public static final int GAME_WIDTH = 930;
public static final int GAME_HEIGHT = 768;
public static final int Explorateur = 10;
public static final int ressource = 3000;
public static final int obstacle = 1000;
public static final int tresor = 1; public void lauchFrame() { this.setLocation(130, 0);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setBackground(Color.WHITE);
this.setTitle("Cherche le tresor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true); LeftPanel leftPanel = new LeftPanel(); RightPanel rightPanel = new RightPanel(); this.getContentPane().add(rightPanel, BorderLayout.EAST);
this.getContentPane().add(leftPanel, BorderLayout.WEST);
this.setVisible(true);
} public static void main(String[] args) {
Espace tc = new Espace();
tc.lauchFrame();
} class RightPanel extends JPanel {
private static final long serialVersionUID = 1L; public RightPanel() {
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
new Color(111, 0, 0), // color of highlight
new Color(111, 0, 0))); // color of shadow
this.setBackground(Color.LIGHT_GRAY);
this.setPreferredSize(new Dimension(GAME_WIDTH - GAME_HEIGHT - 5,
600)); // create un image d'un image file et ajoute quelque texte
ImageIcon img = new ImageIcon("D:\\projet\\GUI\\src\\cat.jpg");
JLabel pitLabel = new JLabel("How?", img, SwingConstants.CENTER);
pitLabel.setHorizontalTextPosition(SwingConstants.CENTER);
pitLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
pitLabel.setIconTextGap(10);
this.add(pitLabel); JPanel button = new JPanel();
button.setLayout(new GridLayout(4, 1, 20, 20));
button.add(new JButton("Start"));
button.add(new JButton("Pause"));
button.add(new JButton("Exit"));
button.add(new JButton("About"));
this.add(button);
}
} class LeftPanel extends JPanel {
private static final long serialVersionUID = 1L; public LeftPanel() {
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
new Color(0, 0, 0), // color of highlight
new Color(0, 0, 0))); // color of shadow
this.setPreferredSize(new Dimension(GAME_HEIGHT, 77));
} int num = 100;
int[][] grille = new int[num][num];
int i, j; @Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int border = 33, top = 20;
int width = 6, height = 6; g.setColor(Color.ORANGE); // dessiner le sol
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
g.fillRect(i * (width + 1) + border,
j * (height + 1) + top, width, height); }
}
g.setColor(Color.RED); // dessiner le resources
for (int x = 0; x < 3000; x++) {
i = (int) (Math.random() * 100);
j = (int) (Math.random() * 100);
if (grille[i][j] == 1) {
x--;
continue;
} else {
grille[i][j] = 1;
g.fillRect(i * (width + 1) + border,
j * (height + 1) + top, width, height);
}
} g.setColor(Color.BLACK); // dessiner l'obstacle
for (int y = 0; y < 1000; y++) {
i = (int) (Math.random() * 100);
j = (int) (Math.random() * 100);
if (grille[i][j] == 1) {
y--;
continue;
} else {
grille[i][j] = 1;
//在这里我已经把颜色改为图片了,不过格子太小,图片看的不是很清楚。----------------------
Image img = Toolkit.getDefaultToolkit().getImage(
"image/3.jpg");
g.drawImage(img, i * (width + 1) + border, j * (height + 1)
+ top, width, height, this);
}
} g.setColor(Color.GREEN);// dessiner le tresor
i = (int) (Math.random() * 100);
j = (int) (Math.random() * 100);
grille[i][j] = 1;
g.fillRect(i * (width + 1) + border, j * (height + 1) + top, width,
height); }
}}
2,格子小没关系,可以用滚动条设计一个扩大缩小的功能。
首先有一个基类格子(推荐是借口),之后有几个子类(红,绿你的几个颜色),每个类中都有自己加载的一个图片,当然还会把自己的个子图成相应的颜色,之后用builder生成你的整个游戏试图。
至于你说的 “每种颜色是否应该用不同的类来编写,不应该直接用画笔画出来” 这个一半对,确实需要用不同的类,一半错,铁定用画笔,不用画笔怎么往面板上画图啊
但是J2EE我还是行了!
虽然这个贴子我帮不了忙!
但是我还要 友情 up 的 !
SSH、Ant、IBatis、jsf、seam、portal、设计模式、
ZK、DWR、ajax、CSS 、oracle
群号:65670864 欢迎加入
也没时间学了。
不好意思