1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
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.JPanel;
import javax.swing.SwingWorker;
public class MyWindow extends JFrame {
private static final long serialVersionUID = 8585544783492126617L;
public static MyWindow app;
public static final int APP_WIDTH = 500;
public static final int APP_HEIGHT = 500;
private JMenuBar menuBar;
private JMenu file;
private JMenuItem quit;
public MyWindow() {
createContent();
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
app = new MyWindow();
app.setVisible(true);
}
});
}
private void createContent() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuBar = new JMenuBar();
file = new JMenu("File");
menuBar.add(file);
quit = new JMenuItem("Quit");
file.add(quit);
quit.addActionListener(new ActionListener() {
/**
* {@inheritDoc}
*/
@Override
public void actionPerformed(final ActionEvent evento) {
System.exit(0);
}
});
menuBar.setVisible(true);
this.setJMenuBar(menuBar);
this.setSize(APP_WIDTH, APP_HEIGHT);
this.setTitle("Dale al Azúl");
this.setLayout(new BorderLayout());
final JPanel contentPane = new JPanel();
final Canvas canvas = new Canvas() {
private static final long serialVersionUID = -8299229889947898987L;
/**
* {@inheritDoc}
*/
@Override
public void paint(final Graphics paramGraphics) {
final int x = (int) (Math.random() * 4 + 1);
switch (x) {
case 1:
paramGraphics.setColor(Color.blue);
int width = paramGraphics.getClipBounds().width;
int height = (paramGraphics.getClipBounds().height);
paramGraphics.fillRect(0, 0, width / 2, height / 2);
break;
case 2:
paramGraphics.setColor(Color.red);
width = paramGraphics.getClipBounds().width;
height = (paramGraphics.getClipBounds().height);
paramGraphics.fillRect(250, 250, width, height);
break;
case 3:
paramGraphics.setColor(Color.yellow);
width = paramGraphics.getClipBounds().width;
height = (paramGraphics.getClipBounds().height);
paramGraphics.fillRect(250, 0, width / 2, height / 2);
break;
case 4:
paramGraphics.setColor(Color.green);
width = paramGraphics.getClipBounds().width;
height = (paramGraphics.getClipBounds().height);
paramGraphics.fillRect(0, 250, width / 2, height / 2);
break;
default:
break;
}
}
};
contentPane.setLayout(new BorderLayout());
contentPane.add(canvas, BorderLayout.CENTER);
this.getContentPane().add(contentPane, BorderLayout.CENTER);
final SwingWorker<Object, Object> test = new SwingWorker<Object, Object>() {
/**
* {@inheritDoc}
*/
@Override
protected Object doInBackground() throws Exception {
int z = 0;
while (z < 1000) {
Thread.sleep(500);
canvas.repaint();
z += 1;
}
return null;
}
};
test.execute();
}
} |