彩色砖块小游戏 头疼问题 qq空间 彩色砖块小游戏, 背景网格,用java怎么实现它.如果不行,我真不行了. 只求源代码了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 图片是个x,不知道什么情况给楼主个例子参考下吧import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.util.Random;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;import com.sun.awt.AWTUtilities;public class TestCube extends JFrame { private static final long serialVersionUID = 1L; private Random ran = new Random(); private MyPanel pane; public TestCube() { init(); } private void init() { this.setAlwaysOnTop(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(600, 600); this.setUndecorated(true); this.setLocationRelativeTo(null); ran = new Random(); pane = new MyPanel(); this.setContentPane(pane); AWTUtilities.setWindowOpaque(this, false); this.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new TestCube(); } }); } class MyPanel extends JPanel { private static final long serialVersionUID = 1L; public MyPanel() { super(); initialize(); } private void initialize() { } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; for (int i = 0; i < 30; i++) { for (int j = 0; j < 30; j++) { g2d.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255))); g2d.fillRect(i*15, j*15, 12, 12); } } } }} import com.sun.awt.AWTUtilities;这行编译出错了.. 我欲哭无泪啊.. 晕 你的JDK版本太低了,还没有这个包我去掉了现在给你画在可见窗体上了之前那个是画在透明窗体上的import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.util.Random;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;public class TestCube extends JFrame { private static final long serialVersionUID = 1L; private Random ran = new Random(); private MyPanel pane; public TestCube() { init(); } private void init() { this.setAlwaysOnTop(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(600, 600); this.setLocationRelativeTo(null); ran = new Random(); pane = new MyPanel(); this.setContentPane(pane); this.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new TestCube(); } }); } class MyPanel extends JPanel { private static final long serialVersionUID = 1L; public MyPanel() { super(); initialize(); } private void initialize() { } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; for (int i = 0; i < 30; i++) { for (int j = 0; j < 30; j++) { g2d.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255))); g2d.fillRect(i*15, j*15, 12, 12); } } } }} 哥么谢了... 我还有一个问题...如何把这个时间显示出来...最好可以变成倒计时这是以下代码import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.SwingUtilities;import javax.swing.Timer;import cn.elva.Settings;import cn.elva.model.Map;public class MainFrame extends JFrame{ private static final long serialVersionUID = 1L; //炸弹的次数 private int bombCount = Settings.BOMBCOUNT; private JPanel jContentPane = null; private JMenuBar menuBar = null; private JMenu fileMenu = null; private JMenuItem reloadItem = null; private JMenuItem startItem = null;//炸弹 private JMenuItem bombItem = null; private JMenuItem exitItem = null; private MapUI mapUI = null; // 游戏开始时间 private long startTime; // 结束时间 private long endTime; private Timer timer = null; // private JMenuItem ti private JMenuBar initMenuBar() { if (menuBar == null) { menuBar = new JMenuBar(); fileMenu = new JMenu("文件"); startItem = new JMenuItem("开始游戏"); startItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reload(); } }); reloadItem = new JMenuItem("重来一次"); reloadItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reload(); } }); bombItem = new JMenuItem("炸弹"); bombItem.addActionListener(new ActionListener(){ public void actionPerformed( ActionEvent e ) { if( bombCount==0 ) { JOptionPane.showMessageDialog(MainFrame.this,"你已经没有炸弹可用了!!!" ); bombItem.setEnabled(false); return; } mapUI.bomb(); bombCount--; } }); exitItem = new JMenuItem("退出"); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); fileMenu.add(startItem); fileMenu.add(reloadItem); fileMenu.add( bombItem ); fileMenu.add(exitItem); menuBar.add(fileMenu); } return menuBar; } /** * @param args */ public static void main(String[] args) { // PAN_TODO 自动生成方法存根 SwingUtilities.invokeLater(new Runnable() { public void run() { MainFrame thisClass = new MainFrame(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } /** * This is the default constructor */ public MainFrame() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(900, 900); this.setTitle("llk"); this.setJMenuBar(initMenuBar()); // this.setContentPane(getJContentPane()); this.setTitle("连连看"); } private void reload() { mapUI = new MapUI(); startTime = System.currentTimeMillis() / 1000; endTime = startTime + Settings.PERTIME; // jContentPane.setVisible(true); jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(mapUI); this.setContentPane(jContentPane); this.validate(); Map.LEFTCOUNT = Settings.ROWS * Settings.COLUMNS; initTimer(); bombItem.setEnabled(true); bombCount=Settings.BOMBCOUNT; } private void initTimer() { ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { startTime = System.currentTimeMillis() / 1000; if (startTime == endTime) { JOptionPane.showMessageDialog(MainFrame.this, "时间到了!!"); int result = JOptionPane.showConfirmDialog(MainFrame.this, "重玩一次?", "Again", JOptionPane.YES_NO_CANCEL_OPTION); if (result == JOptionPane.YES_OPTION) { reload(); } else { jContentPane.setVisible(false); validate(); } } } }; timer = new javax.swing.Timer(500, actionListener); timer.start(); }} notify问题 jpcap抓包的小问题,求解! swing中如何实现打开子窗口时禁用主窗口 高手指点 请教一个java.io.EOFException异常的解决 问java中随机输入参数的实现方法~~谢谢 一个I/O问题,比较特殊的文件 关于java程序发布的问题 !!大送分99分! 请教JAVA中读一个文件的时候 我用file.setReadOnly()后怎么解开? 关于Pattern与Matcher字符串匹配怎么用啊 JAVA RMI连接金蝶中间件APUSIC出现异常...但是jconsole连接是正常的 麻烦各位帮忙看下吧 Java一键搜索---根据关键字查找文本(附源代码)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;import com.sun.awt.AWTUtilities;public class TestCube extends JFrame {
private static final long serialVersionUID = 1L;
private Random ran = new Random();
private MyPanel pane;
public TestCube() {
init();
}
private void init() {
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 600);
this.setUndecorated(true);
this.setLocationRelativeTo(null);
ran = new Random();
pane = new MyPanel();
this.setContentPane(pane);
AWTUtilities.setWindowOpaque(this, false);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestCube();
}
});
}
class MyPanel extends JPanel {
private static final long serialVersionUID = 1L;
public MyPanel() {
super();
initialize();
}
private void initialize() { } @Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
g2d.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
g2d.fillRect(i*15, j*15, 12, 12);
}
}
}
}
}
现在给你画在可见窗体上了之前那个是画在透明窗体上的import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;public class TestCube extends JFrame {
private static final long serialVersionUID = 1L;
private Random ran = new Random();
private MyPanel pane;
public TestCube() {
init();
}
private void init() {
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 600);
this.setLocationRelativeTo(null);
ran = new Random();
pane = new MyPanel();
this.setContentPane(pane);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestCube();
}
});
}
class MyPanel extends JPanel {
private static final long serialVersionUID = 1L;
public MyPanel() {
super();
initialize();
}
private void initialize() { } @Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
g2d.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
g2d.fillRect(i*15, j*15, 12, 12);
}
}
}
}
}
哥么谢了... 我还有一个问题...
如何把这个时间显示出来...最好可以变成倒计时这是以下代码
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;import cn.elva.Settings;
import cn.elva.model.Map;public class MainFrame extends JFrame
{ private static final long serialVersionUID = 1L; //炸弹的次数
private int bombCount = Settings.BOMBCOUNT;
private JPanel jContentPane = null; private JMenuBar menuBar = null; private JMenu fileMenu = null; private JMenuItem reloadItem = null; private JMenuItem startItem = null;
//炸弹
private JMenuItem bombItem = null;
private JMenuItem exitItem = null; private MapUI mapUI = null;
// 游戏开始时间
private long startTime; // 结束时间
private long endTime; private Timer timer = null; // private JMenuItem ti
private JMenuBar initMenuBar()
{
if (menuBar == null)
{
menuBar = new JMenuBar();
fileMenu = new JMenu("文件"); startItem = new JMenuItem("开始游戏");
startItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reload();
}
});
reloadItem = new JMenuItem("重来一次");
reloadItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reload();
}
}); bombItem = new JMenuItem("炸弹");
bombItem.addActionListener(new ActionListener(){
public void actionPerformed( ActionEvent e )
{
if( bombCount==0 )
{
JOptionPane.showMessageDialog(MainFrame.this,"你已经没有炸弹可用了!!!" );
bombItem.setEnabled(false);
return;
}
mapUI.bomb();
bombCount--;
}
});
exitItem = new JMenuItem("退出");
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}); fileMenu.add(startItem);
fileMenu.add(reloadItem);
fileMenu.add( bombItem );
fileMenu.add(exitItem);
menuBar.add(fileMenu);
} return menuBar;
} /**
* @param args
*/
public static void main(String[] args)
{
// PAN_TODO 自动生成方法存根
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
MainFrame thisClass = new MainFrame();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
} /**
* This is the default constructor
*/
public MainFrame()
{
super();
initialize();
} /**
* This method initializes this
*
* @return void
*/
private void initialize()
{
this.setSize(900, 900);
this.setTitle("llk");
this.setJMenuBar(initMenuBar());
// this.setContentPane(getJContentPane());
this.setTitle("连连看");
} private void reload()
{
mapUI = new MapUI();
startTime = System.currentTimeMillis() / 1000; endTime = startTime + Settings.PERTIME; // jContentPane.setVisible(true);
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(mapUI); this.setContentPane(jContentPane);
this.validate();
Map.LEFTCOUNT = Settings.ROWS * Settings.COLUMNS;
initTimer();
bombItem.setEnabled(true);
bombCount=Settings.BOMBCOUNT;
} private void initTimer()
{
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
startTime = System.currentTimeMillis() / 1000; if (startTime == endTime)
{
JOptionPane.showMessageDialog(MainFrame.this, "时间到了!!");
int result = JOptionPane.showConfirmDialog(MainFrame.this,
"重玩一次?", "Again", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION)
{
reload();
}
else
{
jContentPane.setVisible(false);
validate();
}
}
}
};
timer = new javax.swing.Timer(500, actionListener);
timer.start();
}
}