How do I add images to an image array? - java

I am making an applet that displays a random collection of 10 cards based upon 10 random integers.
My idea was to make an array of the 52 displayable cards (not including jokers) and display each cards from the array based upon the random integer like this (sorry i don't know how to use code blocks):
for (int i = 0; i<cards.length; i++) { //cards being my image array
//code that displays each image
}
But I ran into trouble trying to add images into the array and I don't know how to display the images from the array either.
Should I be adding them like this:
Image[] cards = new Image[52];
cards[0] = c1; //name of the Ace of Clubs, I had used getImage() to already get it
The preceding statements throw up errors saying it's an illegal start.
I also need help with displaying the images once I incorporate the images since I don't think:
System.out.println(cards[x]);
Will work with images.
Thanks in advance and sorry for seeming so complicated, I've tried to water it down as much as possible!

So, here's my silly take on it...
public class RandomCards {
public static void main(String[] args) {
new RandomCards();
}
public RandomCards() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new RandomCardsPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception exp) {
exp.printStackTrace();
}
}
});
}
public class RandomCardsPane extends JPanel {
// A list is a collection of Image objects...
private List<Image> cardList;
private Image card = null;
public RandomCardsPane() throws IOException {
// My cards are stored in the default execution location of the program
// and are named "Card_1.png" through "Card_51.png"...
// You image loading process will be different, replace it here..
// ArrayList is a dynamic list (meaning it can grow and shrink
// over the life time of the list) and is backed by an array
// which shouldn't concern you, the only thing you really need to
// know is that it has excellent random access...
cardList = new ArrayList<Image>(51);
for (int index = 0; index < 51; index++) {
cardList.add(ImageIO.read(new File("Card_" + index + ".png")));
}
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
card = cardList.get(Math.min((int)Math.round(Math.random() * cardList.size()), 51));
repaint();
}
});
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (card != null) {
int x = (getWidth() - card.getWidth(this)) / 2;
int y = (getHeight() - card.getHeight(this)) / 2;
g.drawImage(card, x, y, this);
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(225, 315);
}
}
}
I would also prefer ImageIO over Toolkit.getImage or even ImageIcon, apart from the fact that it guarantees the image data is loaded BEFORE the method returns, it also supports a greater number of image formats and is extendable via plugins...

Related

Java Update & Refresh BufferedImage on JPanel dynamically

Very new here. Trying to learn Java.
I figured I'd write a very simple "Game Of Life" auto-algorithm to see if I could grasp some of the basics - (The syntax and concepts are pretty straightforward from other languages etc.)
I've managed to muddle through and seem to be "getting the hang of it" but I've reached a pretty steep hill now.
The code described here will compile and is executable, but as you will see, despite the #override to DrawComponent and the repaint() call, the actual image displayed on-screen does not appear to change...
DIRECT QUESTION IS:
How can I ensure the display is updated every "loop"?
FLUFF & SAMPLE SOURCE:
I appreciate the procedural and non-distributed // rigid single encapsulated class here is not the ideal approach for Java, nor is this "game of life" a particularly appropriate use fothe language - BUT Iwanted to focus on getting core elements and understanding first before I 'advance' onto other things. Secondly, I appreciate this is not a very efficient solution either, but again, I wanted to learn by trying something straightforward and readable so I could easily SEE what was happening and ensure it was working properly as expected.
Third, my naming or formatting conventions may be unusual or non-standard, for this I apologise and again appreciate there are preferred ways. This was not necessarily a priority for me in learning how the language works at this time.
I welcome any advice and tips but please consider I am a complete newcomer to using Java so may not be familiar with concepts and terminologies etc.
/*
Project.java
By: PJ Chowdhury
Entire program encapsulated in single class
Created 29-Oct-2018
Updated: 07-Nov-2018
Added graphics library
*/
//Import the basic required graphics classes.
import java.awt.image.BufferedImage;
import java.awt.*;
//Import the basic applet classes.
import javax.swing.*;
//Core class
public class project
{
//Control value - determines when overpopualted or extinct
private static int supercount;
//Control value - how many surrounding cells must be alive for the central cell to survive
private static byte thrive=4;
//Define & declare effective constant size values
private static byte size=64;
private static byte cellsize=4;
//Declare effective singleton arrays of cells
private static boolean[][] cells;
private static boolean[][] prolif;
//Declare Window Frame
public static JFrame frame;
//Declare Graphics
public static JPanel panel;
//main entry-point. Execution must include parameter argument.
public static void main(String[] args)
{
initialise();
do
{
runtime();
defaultcells();
}
while (1>0); //Bad form of course. I wanted an infinite loop. The window can be closed at user request.
}
//Initialises window & graphics frame
public static void initialiseframe()
{
//Create Window
frame = new JFrame("Life Cells");
//Define window parameters
frame.setSize((int)cellsize*size,(int)cellsize*size);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Create a window panel to accept graphics
panel = new JPanel()
{
//Overload PaintComponent method to redraw image when the frame panel is redrawn
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
};
//attach this panel as a gadget to the frame window
frame.add(panel);
//frame.pack();// Deprecated as this resizes frame window to a minimal size
frame.validate(); // required since panel was added after setVisible was called
frame.repaint(); // required since panel was added after setVisible was called
}
//Initialises & defaults cells
public static void initialisecells()
{
//Define array sizes
cells = new boolean[size][size];
prolif = new boolean[size][size];
// Populate with defaults
defaultcells();
}
//Sets randomised state for each cell
public static void defaultcells()
{
byte x;
byte y;
for (y=0;y<size;y++)
{
for (x=0;x<size;x++)
{
if (Math.random()>=0.5)
{
}
else
{
}
}
}
}
//Wraps initialisation routines
public static void initialise()
{
initialiseframe();
initialisecells();
}
//iterates cells (twice) to determine if they survive or decline and draw to image
public static void process()
{
//Prepare image for cell drawing
Graphics g=panel.getGraphics();
byte x;
byte y;
supercount=0;
//First pass - check if cell will thrive
for (y=0;y<size;y++)
{
for (x=0;x<size;x++)
{
checkcell(x,y);
}
}
//Second pass - apply thrive or wither
for (y=0;y<size;y++)
{
for (x=0;x<size;x++)
{
if (updatecell(x,y))
{
}
if (cells[x][y])
{
}
}
}
}
//sets prolif equivalent depending on status of surrounding cells. This is used in update to set these cells to thrive
public static void checkcell(byte x, byte y)
{
byte count=getsurrounding((int)x,(int)y);
if (count>thrive)
{
prolif[x][y]=true;
}
else
{
if (count<thrive)
{
prolif[x][y]=false;
}
else
{
prolif[x][y]=cells[x][y];
}
}
}
//updates cell with prolif equivalent and returns true if cahnged
public static boolean updatecell(byte x, byte y)
{
if (cells[x][y]!=prolif[x][y])
{
cells[x][y]=prolif[x][y];
return true;
}
return false;
}
//returns number of thriving "cells" surrounding cell at given coords
public static byte getsurrounding(int x, int y)
{
int u=(x-1);
int v=(y-1);
int ux;
int vy;
byte count=0;
for (v=(y-1);v<(y+2);v++)
{
//Use wraparound for edge cells
vy=(v+size) % size;
for (u=(x-1);u<(x+2);u++)
{
//Use wraparound for edge cells
ux=(u+size) % size;
//Only for surrounding cells, not this cell
if ((ux!=x) & (vy!=y))
{
}
}
}
return count;
}
//Draw cell at x,y : not the most efficient nor elegant method...
public static void drawcell(Graphics g, int x, int y, boolean live)
{
if (live)
{
// Draw this cell alive
//g.setColor(Color.GREEN);
}
else
{
// Draw this cell dead
g.setColor(Color.BLACK);
}
g.fillRect(x*cellsize, y*cellsize,cellsize,cellsize);
panel.repaint(x*cellsize, y*cellsize,cellsize,cellsize);
}
//Returns true if population is healthy. False if extinct or overcrowded
public static boolean populationanalysis()
{
return ((supercount<thrive)||(supercount>(int)(size*size)<<1));
}
//Main Loop method
public static void runtime()
{
int sanity=5000;
int loopcount=0;
do
{
process();
loopcount++;
if (populationanalysis())
{
break;
}
}
while (loopcount<sanity);
}
}

Why do my buttons wont show up?

I want to build a bingo got the following source code, which should create a JFrame with 25 buttons placed in a 5x5 matrix. But none of my button gets drawn on the window in any kind.
I ve created a Jpanel on which the buttons are placed, the locations and such are not specific, finetuning will come later, first thing is to even get them drawn on the window.
Bingo Buttons is a class which extends JFrame and simply adds two methods, one to toggle its status from true to false and the other way around and also an method (isSet) to check if the buttons is currently true or false.
bingoField is an String Array which holds nothing but the data which the buttons should get.
I dont get why it does nothing, please help me out. Any kind of help is highly appreciated!
public class BingoFrame extends JFrame {
public static final int BINGOSIZE=25;
public static final int BUTTON_X=50;
public static final int BUTTON_Y=50;
public BingoFrame() {
setResizable(false);
String[] bingoField = null;
BingoButton[] buttons=new BingoButton[25];
try {
bingoField = Utils.getRandomBingoField("Test");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.setTitle("BS Bingo");
this.setResizable(false);
this.setLocation(50, 50);
this.setSize(600, 800);
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JPanel buttonPanel = new JPanel();
buttonPanel.setBounds(0, 0, 594, 772);
getContentPane().add(buttonPanel);
buttonPanel.setLayout(null);
for(int i=0;i<BINGOSIZE;i++) {
buttons[i] = new BingoButton("Text");
}
//decorate buttons and add an action listener
for(int i=0;i<BINGOSIZE;i++) {
final BingoButton temp = buttons[i];
temp.setText(bingoField[i]);
temp.setBackground(Color.white);
temp.setForeground(Color.blue);
temp.setPreferredSize(new Dimension(BUTTON_X,BUTTON_Y));
temp.addActionListener(new ActionListener() {
boolean toggle = false;
#Override
public void actionPerformed(ActionEvent e) {
if (!temp.isSet()) {
temp.setBackground(Color.blue);
temp.setForeground(Color.white);
} else {
temp.setBackground(Color.white);
temp.setForeground(Color.blue);
}
temp.toggle();
}
});
buttons[i]=temp;
}
//set Location for the buttons
for(int i=0;i<5;i++) {
buttons[i].setLocation(100,(50*i)+10*(i+1));
}
for(int i=5;i<10;i++) {
buttons[i].setLocation(160,(50*i)+10*(i+1));
}
for(int i=10;i<15;i++) {
buttons[i].setLocation(220,(50*i)+10*(i+1));
}
for(int i=15;i<20;i++) {
buttons[i].setLocation(280,(50*i)+10*(i+1));
}
for(int i=20;i<25;i++) {
buttons[i].setLocation(340,(50*i)+10*(i+1));
}
//add buttons to the panel
for(int i=0;i<BINGOSIZE;i++) {
buttonPanel.add(buttons[i]);
}
this.setVisible(true);
I got the answer.
I ve changed the Layout of the Panel to Grid Layout. This alligns the buttons just where they should be in a 5x5 matrix and also with the wanted gap between. This makes also the code for the positioning completly obsolete.
By simply changing the Layout to GridLayout all of my Problems were gone.

Why my GUI won't update even tho repaint() is being called?

I am having some difficulties using swing workers, timers, and I am actually a little confused.
As far as my understanding goes, I have to put on a timer to set-up recurring tasks that have to be called by the EDT.
I'm trying to make a program that shows graphically a sorting alghoritm (like this : https://www.youtube.com/watch?v=kPRA0W1kECg )
I just don't understand why the GUI won't refresh. I am quite sure the repaint method is being called since I put a sysout showing me the ordered values and it seems to work , but the GUI just... doesn't change.
Here's my code:
public class MainWindow {
private JFrame frame;
JPanel panel;
public final static int JFRAME_WIDTH = 800;
public final static int JFRAME_HEIGHT = 600;
public final static int NELEM = 40;
ArrayList<Double> numbers;
ArrayList<myRectangle> drawables = new ArrayList<myRectangle>();
Lock lock = new ReentrantLock();
Condition waitme = lock.newCondition();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainWindow() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, JFRAME_WIDTH + 20, JFRAME_HEIGHT + 40);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new myPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
Timer timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
lock.lock();
try{
//Updating the gui
panel.repaint();
panel.revalidate();
//Giving the OK to the sorting alghoritm to proceed.
waitme.signal();
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
});
timer.start();
SwingWorker<Integer, String> sw = new SwingWorker<Integer, String>(){
#Override
protected Integer doInBackground() throws Exception {
mapAndCreate();
bubbleSort();
return null;
}
};
sw.execute();
}
private void bubbleSort() throws InterruptedException{
for(int i=0; i < NELEM; i++){
for(int j=1; j < (NELEM-i); j++){
if(drawables.get(j-1).wid > drawables.get(j).wid){
//swap the elements!
myRectangle temp = drawables.get(j-1);
drawables.set(j-1, drawables.get(j));
drawables.set(j, temp);
lock.lock();
try{
//Wait for the GUI to update.
waitme.await();
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
}
}
}
/***
* Function that maps values from 0 to 1 into the rectangle width.
*/
private void mapAndCreate() {
double max = 0;
numbers = new ArrayList<Double>(NELEM);
//Finding maximum.
for(int i = 0; i < NELEM; i++){
Double currElem = Math.random();
if(currElem > max) max = currElem;
numbers.add(currElem);
}
//Mapping process
int offset = 0;
for(int j = 0; j < NELEM; j++){
Integer mapped = (int) (( JFRAME_WIDTH * numbers.get(j) ) / max);
myRectangle rect = new myRectangle(offset , mapped);
drawables.add(rect);
offset += JFRAME_HEIGHT / NELEM;
}
}
private class myRectangle{
int myy , wid , colorR,colorG,colorB;
public myRectangle(int y , int wid){
this.myy = y;
this.wid = wid;
Random r = new Random();
colorR = r.nextInt(255);
colorG = r.nextInt(255);
colorB = r.nextInt(255);
}
}
private class myPanel extends JPanel{
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(myRectangle rectan : drawables){
Graphics2D graphics2D = (Graphics2D)g;
System.out.println(rectan.wid);
Rectangle2D.Double rect = new Rectangle2D.Double(0,rectan.myy,rectan.wid,JFRAME_HEIGHT / NELEM);
graphics2D.setColor(new Color(rectan.colorR,rectan.colorG,rectan.colorB));
graphics2D.fill(rect);
}
System.out.println("====================================================================================================");
}
}
}
Most OSs (or rather the UI frameworks which they use) don't support concurrent access. Simply put, you can't render two strings of text at the same time.
That's why Swing runs all rendering operations in the UI thread. Calling rendering functions (like paint()) outside of the UI thread can cause all kinds of problems. So when you do it, Swing will just remember "I should repaint" and return (instead of doing any actual work). That way, Swing protects you but most people would prefer to get an error with a useful message.
A timer always also means that there is a thread somewhere which executes when the timer runs out. This is not the UI thread of Swing. So any paing operations there must be wrapped with EventQueue.invokeLater() or similar.
Another common bug is to hog the UI thread (so no rendering happens because you do complex calculations there). That's what the SwingWorker is for. Again, in most methods of the SwingWorker, calling methods which would render something is forbidden (-> use invokeLater()).
So my guess is that the UI thread waits for the lock and the lock simply isn't unlocked early or often enough. See this demo how to do a simple animation in Swing.
public class TimerBasedAnimation extends JPanel implements ActionListener {
public void paint(Graphics g) {
// setup
// do some first-run init stuff
// calculate the next frame
// render frame
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("TimerBasedAnimation");
frame.add(new TimerBasedAnimation());
...
}
}
As you can see in the code doesn't lock. Instead, you just send "render now" events from actionPerformed to Swing. Some time later, Swing will call paint(). There is no telling (and no way to make sure or force Swing) when this will happen.
So good animation code will take the current time, calculate the animation state at that time and then render it. So it doesn't blindly step through N phases in M seconds. Instead, it adjusts for every frame to create the illusion that the animation is smooth when it really isn't.
Related:
Java: Safe Animations with Swing
How to Use Swing Timers

How can I make the DocumentListener compare after every line and not only once the program is run?

I started doing the assignment number #3 by just adding a basic code to understand how it works. But I can’t get out of this problem. I just added an “if” so that if the input text is equal to “hr”, then the turtle would move 2 squares to the right every time. But when I run the code, it is as if it only checks the first characters. If the first two characters are “hr” then it marks a point, but if not, it never again checks the input. So for example if I write:
re
Fd
hr
It never marks the point even though “hr” is there. What can I do so that the TurtleRenderer reads the line every time a \n is inserted and not only once the code is run?
My code:
package turtle;
public class BoardMaker {
private static int MAX = 100;
private boolean[][] board = new boolean[MAX][MAX];
int previousX = 0;
int previousY = 0;
public boolean[][] makeBoardFrom(String description) {
if(description.contentEquals("hr")){
previousX+=2;
board[previousX][previousY]=true;
}
return board;
}
public boolean[][] initialBoard() {
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
board[i][j]=false;
}
return board;
}
}
The TurtleRenderer class:
package turtle;
public class TurtleRenderer extends Panel implements DocumentListener {
private static final long serialVersionUID = 1;
static final Dimension WINDOW_SIZE = new Dimension(1150, 1150);
boolean [][] board;
final BoardMaker boardMaker;
public TurtleRenderer() {
boardMaker = new BoardMaker();
board = boardMaker.initialBoard();
}
static public void main(String args[]) throws Exception {
JFrame frame = new JFrame("Display image");
JPanel panel = new JPanel();
TurtleRenderer image = new TurtleRenderer();
image.setPreferredSize(WINDOW_SIZE);
JScrollPane textArea = makeTextArea(image);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(image);
buildRightPanel(panel, textArea);
frame.setSize(WINDOW_SIZE);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}});
frame.getContentPane().add(panel);
frame.setVisible(true);
}
static void buildRightPanel(JPanel panel,JComponent textArea) {
JLabel label = new JLabel("Your program:");
label.setPreferredSize(new Dimension(150, 20));
JPanel right = new JPanel();
textArea.setPreferredSize(new Dimension(150,500));
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
right.add(label);
right.add(textArea);
panel.add(right);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.white);
g.fillRect(0, 0, WINDOW_SIZE.width, WINDOW_SIZE.width);
if(board == null)
return;
g2d.setColor(Color.red);
for(int i=0;i<board.length;i++) {
for(int j=0;j<board.length;j++) {
if(board[i][j])
g2d.fillRect(9*i+1, 9*j+1, 6, 6);
}
}
}
static JScrollPane makeTextArea(TurtleRenderer image) {
JTextArea textArea = new JTextArea();
textArea.getDocument().addDocumentListener(image);
textArea.setVisible(true);
JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setBorder(BorderFactory.createLineBorder(Color.black));
return areaScrollPane;
}
#Override
public void insertUpdate(DocumentEvent e) {
changed(e);
}
#Override
public void removeUpdate(DocumentEvent e) {
changed(e);
}
#Override
public void changedUpdate(DocumentEvent e) {
changed(e);
}
void changed(DocumentEvent de) {
String description;
Document document = de.getDocument();
try {
description = document.getText(0, document.getLength());
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
try {
board = boardMaker.makeBoardFrom(description);
} catch(ParserException pe) {
board = null;
}
this.repaint();
}
}
Your problem is that you're currently testing if the whole text held by the JTextArea holds "hr". This may be true if hr is the only text in the JTextArea, but once more text is added, this will always be false. What you need to check is if the last line is "hr".
Since this is homework, I won't post a solution, but a pseudo-code logic solution for your DocumentListener could be:
try
get the text String from the document
get the last char from this String
if this last Char == carriage return which is (char)10
split the text into lines using the carriage return as the split's delimiter
get the last line held by this array and check it
if it is hr do something
end if last char == carriage return
end try
catch for BadLocationException
From the Javadocs,
public boolean contentEquals(CharSequence cs)
Compares this string to the specified CharSequence.
The result is true if and only if this String represents the same sequence of char values as the specified sequence.
public boolean contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.
Thus String.contentEquals will function more of a type of String.equals method. There are some differences though.
As with this problem you would require String.contains method to check whether the text contains the String "hr"
One more advice with regards to code efficiency :
You don't have to perform any action in the changedUpdate(DocumentEvent e) method within the DocumentListener. This method is only called when an attribute or a set of attributes has changed, i.e. the style of the document has changed which is not possible in a JTextArea as it does not support styled text.
I hope I have understood your problem correctly.
First, as in a previous comment, the method makeBoardFrom will every time receive
the entire program. If is up to you to split that program into individual commands, and then execute each command. You should not attempt to change the TurtleRenderer class to behave differently.
Second, if you want to move the turtle to the left by two squares you have to mark both
squares as visited, not just the destination square. Right now in your solution, by only using previousX+=2; you only mark the destination square as visited.
Third, in the initialBoard method you also have to actually mark the initial square of the turtle with true. In your case that will be the square at position (0, 0).

How long does it take for a JPanel to update it's height?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class dots extends JPanel implements KeyListener, ComponentListener{ //JPanel extended so that we can overwrite paintComponent and draw the dots, KeyListener so that we can capture keypresses
Random rand = new Random();
JFrame frame;
boolean haveSize = false; //this is used to tell paintComponent whether or not we have the size of the drawable area
int[] x = new int[18],y = new int[18]; //used to store x and y coordinates of the dots so that they don't change position every time paintComponent is called, which is unpredictable
final int OVALDIAM = 40;
public dots() {
frame = new JFrame("Dots");
frame.setSize(new Dimension(800,600)); //So that when we minimize, we don't get a little bar
frame.setExtendedState(Frame.MAXIMIZED_BOTH); //Maximize the window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Hiding the window is a dumb default close behavior. Oh well.
frame.getContentPane().add(this); //put the JPanel in the JFrame
frame.addComponentListener(this);
addKeyListener(this); //The next three lines allow the JPanel to capture key presses and subsequently move the dots when a key is pressed
setFocusable(true);
requestFocusInWindow();
frame.setVisible(true);
try { //for whatever reason, the JPanel (this) doesn't know it's height, which we need to accuratly place dots, immediately after calling setVisible, so we wait for a bit to let it figure things out
Thread.sleep(50);
}
catch (Exception e) {}
newPos();
System.out.println("Press any button to rearrange the dots");
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //draw background using the supers method
if (haveSize) { //if we have the size of the window, then newPos has run and we can paint the dots
for (int i = 0; i < 18; i++) { //loop to draw the dots
g.setColor(i < 12 ? Color.YELLOW : Color.BLUE); //if we have drawn fewer than 12 dots, color them yellow, else, color them blue
g.fillOval(x[i],y[i],OVALDIAM,OVALDIAM);
//System.out.println((i < 12 ? "Yellow":"Blue") + " oval drawn at " + x[i] + "," + y[i]); //Debugging that gave location of dots (some where getting lost)
}
}
}
public void newPos() { //this method generates new and random locations for the dots
for (int i = 0; i < 18; i++) {
x[i] = rand.nextInt(getSize().getWidth() > OVALDIAM ? (int)getSize().getWidth() - OVALDIAM : (int)getSize().getWidth()); // we subtract OVALDIAM so that the entire dot fits in the screen. We also check to make sure that the area is >OVALDIAM
y[i] = rand.nextInt(getSize().getHeight() > OVALDIAM ? (int)getSize().getHeight() - OVALDIAM : (int)getSize().getHeight());
}
if (!haveSize) { //if this method has run, then we can run paintComponent
haveSize = true; //we have locations for dots so we can start drawing dots
frame.repaint(); //ensure dots get painted
}
}
public void componentResized(ComponentEvent e){
newPos();
frame.repaint();
}
public void keyPressed(KeyEvent e) { //when a button is pressed, move the dots
newPos();
frame.repaint();
}
//interface methods we don't need
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new dots();
}
}
So I have this code, and it works fine. However, if you comment out
try { //for whatever reason, the JPanel (this) doesn't know it's height, which we need to accuratly place dots, immediately after calling setVisible, so we wait for a bit to let it figure things out
Thread.sleep(50);
}
catch (Exception e) {}
you get an error. Works fine if you press a key, which updates everything, but otherwise it is weird. Any ideas why?
On an unrelated note, this code is going to help get me a job, so if any of you see anything wrong, please feel free to speak up.
All drawing should be executed under the AWT thread as shown here and here you might want to get your painting code under dots() method to run under invokeLater() so it will manage to finish painting before querying for height or width.

Categories

Resources