How to use JTextArea with this and how to append? - java

Please Help. When I run this GUI the numbers run off the frame. I know I have to use JTextArea and append but where do I put that in my code. can someone explain to me in simple terms and show me? I want to make it scroll vertically and horizontally?
import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class prime extends JFrame
{
public static void main(String[] args)
{
prime frame = new prime();
}
private TextPanel panel;
private JPanel inPanel;
private JTextField inField;
public prime()
{
final int width = 500;
final int height = 500;
setSize(width, height);
setTitle("Find Prime Numbers");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new TextPanel();
add(panel, "Center");
inPanel = new JPanel();
inPanel.add(new JLabel("Enter Your Number", SwingConstants.RIGHT));
inField = new JTextField(20);
ActionListener inListener = new TextListener();
inField.addActionListener(inListener);
inPanel.add(inField);
add(inPanel, "South");
setVisible(true);
}
private class TextListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String message = inField.getText();
inField.setText("");
panel.setMessage(message); }
}
class TextPanel extends JPanel
{
private String message;
private Color backGroundColor;
public TextPanel()
{
message = "";
backGroundColor = Color.white;
}
public TextPanel(String x, Color background)
{
message = x;
backGroundColor = background;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
int width = getWidth();
int height = getHeight();
setBackground(backGroundColor);
g2.setColor(Color.black);
Font x = new Font("TimesNewRoman", Font.BOLD,20);
g2.setFont(x);
FontMetrics fm = g2.getFontMetrics(x);
g2.drawString(message,50, 50);
if(!(message.equals("")))
g2.drawString(previousPrime(message),50,78);
}
public void setMessage(String message) {
if (isPrime(Integer.parseInt(message))){
this.message = message + " is a prime number.";
}
else
this.message = message + " is not a prime number.";
repaint();
}
public boolean isPrime(int num){
for(int i = 2; i < num; i++){
if (num % i == 0)
return false;
}
if(num < 2)
return false;
return true;
}
public String previousPrime(String message){
String totalPrimeNum = "";
int finalNum = Integer.parseInt(message.substring(0,message.indexOf(" ")));
int count = 0;
for(int i = 2; i < finalNum; i++){
if(isPrime(i)) {
totalPrimeNum += " " + i;
count++;
}
if(count == 10) {
totalPrimeNum += "\n";
count = 0;
}
}
if (isPrime(Integer.parseInt(message.substring(0,message.indexOf(" ")))))
totalPrimeNum += " " + finalNum;
System.out.println(totalPrimeNum);
return totalPrimeNum;
}}}

Replace your TextPanel with JTextArea, wrap the JTextArea in a JScrollPane
private JTextArea panel;
//...
panel = new JTextArea(20, 10);
add(new JScrollPane(panel), "Center");
Use either setText or append to update the JTextArea. You will need to extract your calculation code from your existing TextPanel and re-use it
See How to Use Text Areas and How to Use Scroll Panes for more details

Related

Only one JButton appears on GridLayout unless getRandom(); updateNumber(); are added after the for-loop in startGame()

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
it is a simple summation math game. The sum is a random number within range 2 to 18. User needs to click 2 number buttons (1-9) to come up with this random sum.
public class MyFrame extends JFrame implements ActionListener{
JFrame gameFrame;
JPanel gamePanelGeneral, gamePanelMiddle, gamePanelBottom;
JButton startButton, endGame;
JButton[]numberButtons = new JButton[9];
JLabel welcome, gameRule, pointsCounter;
JTextArea dataAnalysis;
ImageIcon image = new ImageIcon("logo.png");
int min = 2;
int max = 18;
int num1,puzzleNumber;
ArrayList<Integer> win = new ArrayList<Integer>();
ArrayList<Integer> lose = new ArrayList<Integer>();
class roundButton extends JButton {
Color col = Color.yellow;
public roundButton(String text){
super(text);
setContentAreaFilled(false);
}
protected void paintComponent(Graphics g) {
g.setColor(this.col);
g.fillOval(0, 0, getSize().width-1, getSize().height-1);
super.paintComponent(g);
}
}
the constructor makes a frame
MyFrame(){
gameFrame = new JFrame();
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setTitle("Math Game");
gameFrame.setIconImage(image.getImage());
gameFrame.setSize(500,500);
gameFrame.setVisible(true);
gameFrame.setLayout(null);
}
this is to make a button
JButton gameButtons(int xPosition, int yPosition, int width, int height, String text, JPanel panel) {
roundButton button = new roundButton(text);
button.setBounds(xPosition, yPosition, width, height);
button.addActionListener(this);
button.setFocusable(false);
button.setBorder(null);
panel.add(button);
return button;
}
this is to make a label
JLabel gameLabels(int xPosition, int yPosition, int width, int height, String text, JPanel panel) {
JLabel label = new JLabel(text);
label.setBounds(xPosition, yPosition, width, height);
label.setVerticalAlignment(JLabel.TOP);
panel.add(label);
return label;
}
and this makes a panel
JPanel gamePanels(int xPosition, int yPosition, int width, int height, JFrame frame) {
JPanel panel = new JPanel();
panel.setBounds(xPosition, yPosition, width, height);
panel.setLayout(null);
frame.add(panel);
frame.repaint();
return panel;
}
JTextArea gameTxArea(int xPosition, int yPosition, int width, int height, String text, JPanel panel) {
JTextArea textArea = new JTextArea();
textArea.setBounds(xPosition, yPosition, width, height);
textArea.setText(text);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setOpaque(false);
panel.add(textArea);
return textArea;
}
this just returns a random number
int getRandom() {
int x = (int)Math.floor(Math.random()*(max-min+1)+min);
if(x == puzzleNumber) {
x = getRandom();
} else {
puzzleNumber = x;
}
return puzzleNumber ;
}
this is the intro page (window) before the game starts
void launchScreen() {
gamePanelGeneral = gamePanels(0,0,500,500, gameFrame);
welcome = gameLabels(150,50,300,50,"Welcome to mini math game!", gamePanelGeneral);
startButton = gameButtons(180, 100, 100, 30,"Start", gamePanelGeneral);
startButton.setBounds(180, 100, 100, 30);
}
this is the part where all happens the game works like it is supposed to be
void startGame() {
gameFrame.remove(gamePanelGeneral);
gamePanelGeneral = gamePanels(0,0,500,60, gameFrame);
gamePanelMiddle = gamePanels(120,80,250,250, gameFrame);
gamePanelMiddle.setLayout(new GridLayout(3,3,10,10));
gamePanelBottom = gamePanels(20,350,450,100, gameFrame);
this.puzzleNumber = getRandom();
gameRule = gameLabels(150,15,300,15, "Select two numbers that add up " + this.puzzleNumber, gamePanelGeneral);
pointsCounter = gameLabels(200,40,200,15, "Win: " + win.size() + " " + "Lose: " + lose.size(), gamePanelGeneral);
endGame = gameButtons(180, 0, 100, 50,"End Game", gamePanelBottom);
for(int i=0; i<numberButtons.length; i++) {
numberButtons[i] = gameButtons(0, 0,45,30, String.valueOf(i+1), gamePanelMiddle);
}
here is the bit I can't figure out. When i run these 2 methods (getRandom() and updateNUmber()), there are 9 buttons populated on the grid. If they are excluded, only one button appears on the grid.
getRandom();
updateNumber();
}
void updateNumber() {
gameRule.setText("Select two numbers that add up " + this.puzzleNumber);
pointsCounter.setText("Win: " + win.size() + " " + "Lose: " + lose.size());
}
double equal_above10(ArrayList<Integer>arrList, boolean b) {
double accumulator = 0;
if (b) {
for (int i: arrList) {
if(i >=10) {
accumulator++;
}
}
} else {
for (int i: arrList) {
if(i < 10) {
accumulator++;
}
}
} return accumulator;
}
String percentOfWin(ArrayList<Integer>arrList1, ArrayList<Integer>arrList2) {
String printMessage = " ";
double x = equal_above10(arrList1, true);
double y = equal_above10(arrList2, true);
double w = equal_above10(arrList1, false);
double z = equal_above10(arrList2, false);
if (x == 0 && y == 0) {
} else {
printMessage = "Percentage of win for numbers greater than or equal to 10: "
+ Math.round((equal_above10(arrList1,true)/((equal_above10(arrList1,true)+(equal_above10(arrList2,true))))*100))
+ "\n";
}
if (w == 0 && z == 0) {
return printMessage;
}
else {
printMessage += "Percentage of win for numbers less than 10: "
+ Math.round((equal_above10(arrList1,false)/((equal_above10(arrList1,false)+(equal_above10(arrList2,false))))*100))
+"\n";
}
return printMessage;
}
void gameAnalysis() {
gameFrame.remove(gamePanelGeneral);
gameFrame.remove(gamePanelMiddle);
gameFrame.remove(gamePanelBottom);
gamePanelGeneral = gamePanels(0,0,500,500, gameFrame);
dataAnalysis = gameTxArea(20,30,460,300,"Game Sessions Data Analyzed: \n \n"
+ "Numbers you got correct: " + win + "\n"
+ "Numbers you got wrong: " + lose + "\n"
+ percentOfWin(win, lose), gamePanelGeneral);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==startButton) {
startGame();
}
for(int i=0;i<numberButtons.length;i++) {
if(e.getSource() == numberButtons[i]) {
if(num1 == 0) {
num1 = i+1;
} else {
if (num1+i+1 == this.puzzleNumber) {
win.add(puzzleNumber);
}else {
lose.add(puzzleNumber);
}
num1 = 0;
puzzleNumber = getRandom();
updateNumber();
}
}
}
if(e.getSource()==endGame) {
gameAnalysis();
}
}
public static void main(String[]args) throws Exception{
MyFrame mathGame = new MyFrame();
mathGame.launchScreen();
}
}

Drawing a game board in java

I am referencing this link, however this won't ultimately fix my problem (i.e I run my program from someone else's computer). How to deal with "java.lang.OutOfMemoryError: Java heap space" error (64MB heap size). Right now, I have a game board that has 10x10 squares, but I need to increase this to 100x100, but when I do, I get this error. What is the best way to increase my game board size while avoiding this error? Current output is below, Code should compile and run. Thanks!
GameBoard Class:
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;
public class GameBoard {
private final JPanel board = new JPanel(new BorderLayout(3, 3));
private JButton[][] c1squares = new JButton[10][10];
private JPanel c1Board, c2Board;
private final JLabel messagec1 = new JLabel("Player 1 Board");
JToolBar tool = new JToolBar();
Insets Margin = new Insets(0,0,0,0);
int squares = 10;
int space = 100;
ImageIcon icon = new ImageIcon(new BufferedImage(space, space, BufferedImage.TYPE_INT_ARGB));
GameBoard() {
initializeGui();
}
public final void initializeGui() {
board.setBorder(new EmptyBorder(5, 5, 5, 5));
tool.setFloatable(false);
board.add(tool, BorderLayout.PAGE_START);
tool.add(messagec1);
c1Board = new JPanel(new GridLayout(0, 10));
c1Board.setBorder(new LineBorder(Color.BLACK));
board.add(c1Board);
for (int i = 1; i < c1squares.length; i++) {
for (int j = 0; j < c1squares[i].length; j++) {
JButton b = new JButton();
b.setMargin(Margin);
b.setIcon(icon);
if ((j % 2 == 1 && i % 2 == 1) || (j % 2 == 0 && i % 2 == 0)) {
b.setBackground(Color.WHITE);
} else {
b.setBackground(Color.BLACK);
}
c1squares[j][i] = b;
}
}
for (int i = 1; i < squares; i++) {
for (int j = 0; j < squares; j++) {
c1Board.add(c1squares[j][i]);
}
}
public final JComponent getGui() {
return board;
}
public final JComponent getGui2() {
return board2;
}
}
BattleShipFinal Class:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class BattleshipFinal {
public static void main(String[] args) {
GameBoard gb = new GameBoard();
JFrame frame = new JFrame("Battleship - Server");
frame.add(gb.getGui());
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(900,900));
frame.setMinimumSize(new Dimension(900,900));
frame.setLocation(50,50);
frame.pack();
frame.setVisible(true);
}
}
In case you are curious, and to illustrate what people said in the comments, you could have a custom JPanel painting the squares.
If you ever need to respond to mouse events, add a MouseListener to your panel, which will take care of the selected square (haven't added that part, but the selected field inside the Square class is a hint).
I removed stuff from your code, just to demonstrate this painting part.
GameBoard :
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class GameBoard {
private JPanel board;
private final Square[][] c1squares = new Square[10][10];
GameBoard() {
initializeGui();
}
public final void initializeGui() {
for (int i = 0; i < c1squares.length; i++) {
for (int j = 0; j < c1squares[i].length; j++) {
Square square = new Square();
if ((j % 2 == 1 && i % 2 == 1) || (j % 2 == 0 && i % 2 == 0)) {
square.setBackground(Color.WHITE);
} else {
square.setBackground(Color.BLACK);
}
c1squares[i][j] = square;
}
}
board = new BoardPanel(c1squares);
board.setBorder(new EmptyBorder(5, 5, 5, 5));
}
public final JComponent getGui() {
return board;
}
private class BoardPanel extends JPanel {
Square[][] squares;
public BoardPanel(final Square[][] squares) {
this.squares = squares;
}
#Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
for (int i = 0; i < squares.length; i++) {
for (int j = 0; j < squares[i].length; j++) {
Square currentSquare = squares[i][j];
System.out.println("Managing square " + i + " " + j);
g.setColor(currentSquare.getBackground());
g.fillRect(i * width / squares.length, j * height / squares.length, width / squares.length,
height / squares.length);
}
}
}
}
private class Square {
boolean isSelected;
Color background;
public boolean isSelected() {
return isSelected;
}
public void setSelected(final boolean isSelected) {
this.isSelected = isSelected;
}
public Color getBackground() {
return background;
}
public void setBackground(final Color background) {
this.background = background;
}
}
}
BattleshipFinal :
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BattleshipFinal {
public static void main(final String[] args) {
GameBoard gb = new GameBoard();
JFrame frame = new JFrame("Battleship - Server");
JComponent board = gb.getGui();
frame.add(board);
frame.setLocationByPlatform(true);
//frame.setMinimumSize(frame.getSize());
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
//frame.setPreferredSize(new Dimension(100, 100));
board.setMinimumSize(new Dimension(100, 100));
board.setPreferredSize(new Dimension(100, 100));
frame.setMinimumSize(new Dimension(100, 100));
frame.setLocation(50, 50);
frame.pack();
frame.setVisible(true);
}
}
Okay, I figured out the solution for this. Here is the output:
The way I fixed this was by changing these lines of code:
private JButton[][] c1squares = new JButton[100][100];
int squares = 100;
int space = 1000;
c1Board = new JPanel(new GridLayout(0, 100));

How would I resolve this error?

I am currently writing code for a program that, when it works, opens an external window that has a building and a scrolling banner on it. If you input a phrase into a textbox above the building, that phrase will scroll across the banner.
DisplayWindow
import javax.swing.*;
import java.awt.*;
public class DisplayWindow extends JFrame {
private Container c;
public DisplayWindow() {
super("Display");
c = this.getContentPane();
}
public void addPanel(JPanel p) {
c.add(p);
}
public void showFrame() {
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
MovingSignPanel
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MovingSignPanel extends JPanel implements ActionListener{
JMenuBar b;
JButton start = new JButton("Start");
JButton stop = new JButton("Stop");
JButton quit = new JButton("Quit");
JTextField phrase = new JTextField(20);
private int lVar = 200;
private int rVar = 600;
private int hVar = 200;
private int ground = 400;
private Timer scroll = new Timer(40,this);
private int xVel = 2;
private int xVal = lVar;
private int yVal = 150;
private String input = " ";
private int inputWidth = 0;
private Boolean scrolling = false;
public MovingSignPanel(JMenuBar b){
setPreferredSize(new Dimension(1000, 1000));
setBackground(Color.white);
this.add(phrase);
phrase.addActionListener(this);
this.add(start);
start.addActionListener(this);
this.add(stop);
stop.addActionListener(this);
this.add(quit);
quit.addActionListener(this);
}
public void drawBanner(Graphics g){
clearBanner(g);
drawBuilding(g);
int position = xVal;
while(position < rVar){
g.drawString(input,position,yVal);
position += inputWidth;
}
position = xVal - inputWidth;
while(position > lVar - inputWidth){
g.drawString(input,position,yVal);
position -= inputWidth;
}
if(xVal > rVar)
xVal -= inputWidth;
xVal += xVel;
drawBuilding(g);
}
public void drawBuilding(Graphics g){
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0,0,1000,1000);
g.setColor(Color.gray);
g.fillRect(lVar,200,rVar-lVar,hVar);
g.fillRect(lVar,100,rVar-lVar,hVar-800);
g.setColor(Color.lightGray);
g.fillRect(0, ground, 700, 400 - ground);
g.setColor(Color.blue);
for(int n = lVar + 20; n < rVar - 10; n += 40){
for(int m = 60; m < 150; m += 30){
g.fillRect(n,m,20,20);
}
}
for(int n = lVar + 20; n < rVar - 10; n += 40){
for(int m = 210; m < 350; m += 30){
g.fillRect(n,m,20,20);
}
}
g.setColor(Color.darkGray);
g.fillRect(0,0,lVar,ground);
g.fillRect(rVar,0,lVar,ground);
}
public void inputMsg(){
input = phrase.getText();
inputWidth = phrase.getText().length();
}
public void resetTimer(){
scroll.stop();
scrolling = false;
}
public void startMsg(){
inputMsg();
if(!scrolling){
scroll.start();
scrolling = true;
}
}
public void clearBanner(Graphics g){
g.clearRect(0,0,1000,1000);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == quit)
System.exit(0);
if (e.getSource() == start)
repaint();
startMsg();
if (e.getSource() == stop)
resetTimer();
}
}
SignDriver
import javax.swing.*;
public class SignDriver {
public static void main(String[] args) {
DisplayWindow d = new DisplayWindow();
JMenuBar menuBar = new JMenuBar();
d.setJMenuBar(menuBar);
MovingSignPanel p = new MovingSignPanel(menuBar);
d.addPanel(p);
d.showFrame();
}
}
I can organize this better if needed. I know the buttons don't work yet, but right now I'm more concerned with why nothing is being drawn when the program is run. The error occurs whenever I try to run the program and looks like this:
java.lang.NoSuchMethodError: MovingSignPanel.<init>(Ljavax/swing/JMenuBar;)V
at SignDriver.main(SignDriver.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Seems your SignDriver class at runtime has an old version of the class MovingSignPanel, a version that does not have this constructor MovingSignPanel(javax.swing.JMenuBar). Just try to clean and rebuild, and this error should disappear.

The method about ImageIcons does not work

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Cards extends JFrame {
private GridLayout grid1;
JButton []bt=new JButton[52];
ImageIcon tail=new ImageIcon(getClass().getResource("b1fv.png"));
ImageIcon ori;
public Cards(){
grid1=new GridLayout(7,9,2,2);
setLayout(grid1);
for(int i=0;i<bt.length;i++){
ImageIcon c=new ImageIcon(getClass().getResource(i+1+".png"));
bt[i]=new JButton(c);
bt[i].addActionListener(new RatingMouseListener(i));
add( bt[i]);
}
}
public static void main(String[] args){
Cards frame=new Cards();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1400,700);
frame.setVisible(true);
}
private class RatingMouseListener implements ActionListener {
private int index=0;
public RatingMouseListener(int index) {
this.index = index;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Mouse entered for rating " + index);
ori=new ImageIcon(getClass().getResource(index+1+".png"));
if (bt[index].getIcon()==ori)
bt[index].setIcon(tail);
else
bt[index].setIcon(ori);
}
}
}
When I run this, I expect that the ori and the tail should exchange. But they don't. Can someone help me?
This would be best done via a description tag.
Basically, set the description to the images like below, then swap them if they have the same description.
ori.setDescription("ori");
tail.setDescription("tail");
if ("ori".equals((ImageIcon)bt[index].getIcon()).getDescription())
// The rest is how you had it.
I'm guessing that you want to have playing cards that flip when clicked (but I'm not sure). Again, I recommend that you create your ImageIcons once and at the start of the program. Then you can easily compare if one icon is the same as another by using the equal(...) method or even in this situation the == operator. For example, please have a look at and run this code for an example of what I mean:
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
public class CardsDeck {
public static final String RANKS = "a23456789tjqk";
public static final String SUITS = "cdhs";
public static final String CARDS_IMG_PATH = "http://math.hws.edu/javanotes/source/cards.png";
private static final int BACK_RANK = 2;
private static final int BACK_SUIT = SUITS.length();
private static final String ICON = "icon";
private JPanel panel = new JPanel();
private List<ImageIcon> iconList = new ArrayList<ImageIcon>();
private ImageIcon cardBack;
public CardsDeck() {
try {
URL imgUrl = new URL(CARDS_IMG_PATH);
BufferedImage img = ImageIO.read(imgUrl);
double cardWidth = (double) img.getWidth() / RANKS.length();
double cardHeight = (double) img.getHeight() / (SUITS.length() + 1);
int w = (int) cardWidth;
int h = (int) cardHeight;
for (int rank = 0; rank < RANKS.length(); rank++) {
for (int suit = 0; suit < SUITS.length(); suit++) {
int x = (int) (rank * cardWidth);
int y = (int) (suit * cardHeight);
BufferedImage subImg = img.getSubimage(x, y, w, h);
ImageIcon icon = new ImageIcon(subImg);
iconList.add(icon);
}
}
int x = (int) (BACK_RANK * cardWidth);
int y = (int) (BACK_SUIT * cardHeight);
BufferedImage subImg = img.getSubimage(x, y, w, h);
cardBack = new ImageIcon(subImg);
int hgap = 5;
int vgap = hgap;
panel.setLayout(new GridLayout(SUITS.length(), RANKS.length(), hgap, vgap));
panel.setBorder(BorderFactory.createEmptyBorder(vgap, hgap, vgap, hgap));
MouseListener mouseListener = new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
JLabel label = (JLabel) e.getSource();
Icon currentIcon = label.getIcon();
if (currentIcon.equals(cardBack)) {
Icon icon = (Icon) label.getClientProperty(ICON);
label.setIcon(icon);
} else {
label.setIcon(cardBack);
}
}
};
Collections.shuffle(iconList);
for (int i = 0; i < iconList.size(); i++) {
JLabel label = new JLabel(cardBack);
label.putClientProperty(ICON, iconList.get(i));
label.addMouseListener(mouseListener);
panel.add(label);
}
} catch (MalformedURLException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
private JComponent getPanel() {
return panel;
}
private static void createAndShowGui() {
CardsDeck cardsDeck = new CardsDeck();
JFrame frame = new JFrame("CardsDeck");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(cardsDeck.getPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
If run, it will show a 13 x 4 array of cards that can be flipped by clicking on them:

SwingWorker process() updating gui Generics

Im trying to use SwingWorker to update my gui.
The part of my gui that I'm trying to update is a JPanel (gridPanel) with a GridLayout [50][50].
Each grid in the GridLayout has a custom GridGraphic JComponent.
In the doInBackground() of my SwingWorker, I update each GridGraphic which represents some color. Then, I publish it to a List that the process() uses to update the gui. Though, the gui isn't updating.
Is there a way to do this without calling repaint().
How do I fix my SwingWorker so the gui is responsive. What do I want to return in order for the gridPanel, which is a [50][50] GridLayout of GridGraphic components responsive to changes
The SwingWorker is executed in the stepButton.addActionListener(new ActionListener()...............in SlimeGui
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class SlimeGui extends JFrame{
private JPanel buttonPanel, populationPanel, velocityPanel;
private JPanel gridPanel = new JPanel(new GridLayout(50, 50));
private JButton setupButton, stepButton, goButton;
private JLabel populationNameLabel, velocityNameLabel, populationSliderValueLabel, velocitySliderValueLabel;
private JSlider populationSlider, velocitySlider;
private GridGraphic [] [] gridGraphic;
private GridGraphic test;
private int agents = 125;
private int velocity = 500;
private boolean resetGrid;
public SlimeGui() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//Set up JButtons
buttonPanel = new JPanel();
setupButton = new JButton("Setup");
stepButton = new JButton("Step");
goButton = new JButton("Go");
buttonPanel.add(setupButton);
buttonPanel.add(stepButton);
buttonPanel.add(goButton);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
add(buttonPanel, c);
//Set up population JSlider
populationPanel = new JPanel();
populationNameLabel = new JLabel(" Population");
populationSliderValueLabel = new JLabel(Integer.toString(agents));
populationSlider = new JSlider(JSlider.HORIZONTAL,0, 1000, 125);
populationSlider.setMajorTickSpacing(125);
populationSlider.setPaintTicks(true);
populationSlider.addChangeListener(new PopulationSliderListener());
populationPanel.add(populationNameLabel);
populationPanel.add(populationSlider);
populationPanel.add(populationSliderValueLabel);
c.gridx = 0;
c.gridy = 2;
add(populationPanel, c);
//Set up veolicty JSlider
velocityPanel = new JPanel();
velocityNameLabel = new JLabel(" Velocity");
velocitySliderValueLabel = new JLabel(Integer.toString(velocity));
velocitySlider = new JSlider(JSlider.HORIZONTAL,0, 1000, 500);
velocitySlider.setMajorTickSpacing(125);
velocitySlider.setPaintTicks(true);
velocitySlider.addChangeListener(new VelocitySliderListener());
velocityPanel.add(velocityNameLabel);
velocityPanel.add(velocitySlider);
velocityPanel.add(velocitySliderValueLabel);
c.gridx = 0;
c.gridy = 3;
add(velocityPanel, c);
//Set up grid with GridGraphic objects
gridGraphic = new GridGraphic[50][50];
for(int i = 0; i < 50; i++){
for(int j = 0; j < 50; j++){
gridGraphic[i][j] = new GridGraphic();
gridPanel.add(gridGraphic[i][j]);
}
}
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
add(gridPanel, c);
//Set up ActionListener for the 'Setup' JButton
setupButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int n1=0;
int n2=0;
//resets the grid so there are no agents
if(resetGrid){
for(int i = 0; i < 50; i++){
for(int j = 0; j < 50; j++){
gridGraphic[i][j].setDefault();
}
}
}
//sets a random number of positions for GridGraphics
for (int numOfAgenets = 0; numOfAgenets < agents; numOfAgenets++){
int lowerB = 0;
int upperB = 50;
n1 = (lowerB + (int)(Math.random()*(upperB-lowerB))); //random number 1
n2 = (lowerB + (int)(Math.random()*(upperB-lowerB))); //random number 2
System.out.println("Choosing random agent "+(numOfAgenets+1)+": "+n1 +" "+n2);
//sets the GridGraphic to an agent if it's available
if (gridGraphic[n1][n2].getIntensity() == 0)
gridGraphic[n1][n2].setAgent();
//if the GridGraphic is already an agent, it continues to search
else if(gridGraphic[n1][n2].getIntensity() == 5){
while(gridGraphic[n1][n2].getIntensity() == 5){
n1 = (lowerB + (int)(Math.random()*(upperB-lowerB)));
n2 = (lowerB + (int)(Math.random()*(upperB-lowerB)));
}
gridGraphic[n1][n2].setAgent();
}
}
repaint();
resetGrid = true;
}
});
//Set up ActionListener for the 'Step' JButton
stepButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
StepManager step = new StepManager(SlimeGui.this);
step.execute();
//repaint();
}
});
}
class PopulationSliderListener implements ChangeListener{
public void stateChanged(ChangeEvent e){
agents = ((JSlider)e.getSource()).getValue();
populationSliderValueLabel.setText(Integer.toString(agents));
System.out.println("Population of agents: " + agents);
}
}
class VelocitySliderListener implements ChangeListener{
public void stateChanged(ChangeEvent e){
velocity = ((JSlider)e.getSource()).getValue();
velocitySliderValueLabel.setText(Integer.toString(velocity));
System.out.println("Velocity(ms) of agents: " + velocity);
}
}
public Integer getVelocity(){
return velocity;
}
public GridGraphic getGridGraphic(int n1, int n2){
return gridGraphic[n1][n2];
}
public GridGraphic [][] getGridGraphic(){
return gridGraphic;
}
public void setGridGraphicArray(GridGraphic xxx, int n1, int n2 ){
gridGraphic[n1][n2] = xxx;
}
public void setGridPanel(GridGraphic[][] xxx){
for(int i = 0; i < 50; i++){
for(int j = 0; j < 50; j++){
gridPanel.add(xxx[i][j]);
}
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
SlimeGui slime = new SlimeGui();
slime.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
slime.pack();
slime.setVisible(true);
slime.setResizable(false);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
My SwingWorker
import java.util.List;
import java.awt.*;
import javax.swing.*;
import java.util.concurrent.ExecutionException;
public class StepManager extends SwingWorker<Void, GridGraphic>{
private SlimeGui gui;
public StepManager(SlimeGui sg){
gui=sg;
}
#Override
protected Void doInBackground() throws Exception{
for(int i = 0; i < 50; i++){
for(int j = 0; j < 50; j++){
if(gui.getGridGraphic(i,j).getIntensity()==5){
if (i==0){
gui.getGridGraphic(i,j).setDefault();
gui.getGridGraphic(49,j).setAgent();
}
else{
gui.getGridGraphic(i,j).setDefault();
gui.getGridGraphic(i-1,j).setAgent();
}
}
publish(gui.getGridGraphic(i,j));
}
}
return null;
}
#Override
protected void process(List <GridGraphic> gg){
int k=0;
for ( int i = 0; i < 50; i++ ){
for(int j = 0; j < 50; j++){
gui.setGridGraphicArray(gg.get(k),i,j);
k++;
}
}
gui.setGridPanel(gui.getGridGraphicArray());
System.out.println("process has completed");
}
#Override
protected void done(){
System.out.println("doInBackground has completed");
}
}
My GridGraphic
import java.awt.*;
import javax.swing.*;
public class GridGraphic extends JComponent {
private int intensity = 0;
public GridGraphic() {
//setBorder(BorderFactory.createLineBorder(Color.BLUE));
}
public void paintComponent(Graphics g) {
//paints the GridGraphic black
if (intensity == 0){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
}
//paints the GridGraphic black with a yellow dot
else if (intensity == 5){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.YELLOW);
g.fillOval(3, 3, getWidth()/2, getHeight()/2);
}
//paints the GridGraphic dark yellow (pheromone)
if (intensity == 2){
super.paintComponent(g);
g.setColor(Color.BLACK.brighter());
g.fillRect(0, 0, getWidth(), getHeight());
}
}
public Dimension getPreferredSize() {
return new Dimension(10, 10);
}
public void setAgent(){
intensity = 5;
}
public void setPheromone1(){
intensity = 2;
}
public void setDefault(){
intensity = 0;
}
public int getIntensity(){
return intensity;
}
}
The line number in the stack trace indicates where the exception occurs. Your gui attribute in StepManager is null. It's never initialized.

Categories