So I've been working on a Tic Tac Toe App using MVC. I'm having difficulties implementing the mouse Listener in the views page. I need each panel of the board to be clickable. Can someone help me?
public class TicTacToeView extends JFrame{
private JButton oButton, xButton;
public JPanel board;
public ArrayList<Shape> shapes;
public TicTacToeView(){
shapes = new ArrayList<Shape>();
JPanel topPanel=new JPanel();
topPanel.setLayout(new FlowLayout());
add(topPanel, BorderLayout.NORTH);
add(board=new Board(), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
}
private class Board extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w=getWidth();
int h=getHeight();
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.WHITE);
g2d.fill(new Rectangle2D.Double(0, 0, w, h));
g2d.setPaint(Color.BLACK);
g2d.setStroke(new BasicStroke(4));
g2d.draw(new Line2D.Double(0, h/3, w, h/3));
g2d.draw(new Line2D.Double(0, h*2/3, w, h*2/3));
g2d.draw(new Line2D.Double(w/3, 0, w/3, h));
g2d.draw(new Line2D.Double(w*2/3, 0, w*2/3, h));
//draw circles and xs by visiting elements in the array List.
for(Shape shape : shapes){
g2d.setPaint(Color.BLUE);
g2d.draw(shape);
}
}
}
public void addMouseListener(MouseListener ml){
}
}
It would be easier if each cell was it's own Shape (like a Rectangle), but, with your current code, you could use something like...
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
int w = getWidth();
int h = getHeight();
selectedCell = null;
for (int col = 0; col < 3 && selectedCell == null; col++) {
for (int row = 0; row < 3; row++) {
int x = (w / 3) * col;
int y = (h / 3) * row;
Rectangle cell = new Rectangle(x, y, w / 3, h / 3);
if (cell.contains(e.getPoint())) {
System.out.println("In");
selectedCell = cell;
repaint();
break;
}
}
}
}
});
Now, all this does, is loops through each column and row, creating a Rectangle and checking to see if it contains the MouseEvent point. If it finds a match, it assigns the cell's rectangle to a instance field, which I used to highlight the in your paint method.
This is why it would be easier to have each cell stored as a Rectangle in a List
As a runnable example...
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TicTacToeView extends JFrame {
private JButton oButton, xButton;
public JPanel board;
public ArrayList<Shape> shapes;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new TicTacToeView();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public TicTacToeView() {
shapes = new ArrayList<Shape>();
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
add(topPanel, BorderLayout.NORTH);
add(board = new Board(), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class Board extends JPanel {
private Rectangle selectedCell = null;
public Board() {
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
int w = getWidth();
int h = getHeight();
selectedCell = null;
for (int col = 0; col < 3 && selectedCell == null; col++) {
for (int row = 0; row < 3; row++) {
int x = (w / 3) * col;
int y = (h / 3) * row;
Rectangle cell = new Rectangle(x, y, w / 3, h / 3);
if (cell.contains(e.getPoint())) {
System.out.println("In");
selectedCell = cell;
repaint();
break;
}
}
}
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.WHITE);
g2d.fill(new Rectangle2D.Double(0, 0, w, h));
if (selectedCell != null) {
g2d.setColor(Color.BLUE);
g2d.fill(selectedCell);
}
g2d.setPaint(Color.BLACK);
g2d.setStroke(new BasicStroke(4));
g2d.draw(new Line2D.Double(0, h / 3, w, h / 3));
g2d.draw(new Line2D.Double(0, h * 2 / 3, w, h * 2 / 3));
g2d.draw(new Line2D.Double(w / 3, 0, w / 3, h));
g2d.draw(new Line2D.Double(w * 2 / 3, 0, w * 2 / 3, h));
//draw circles and xs by visiting elements in the array List.
for (Shape shape : shapes) {
g2d.setPaint(Color.BLUE);
g2d.draw(shape);
}
}
}
}
So actually I made it harder than it really is. Basically, you just have to pass the MouseListener from the controller(which implements the MouseListener) into the views and add it to a JPanel which in this case, is "board".
So in the views page, looks something like this.
public void addMouseListener(MouseListener ml){
this.board.addMouseListener(ml);
}
You then put what you want to do for each mouse event in the controller.
Hope someone finds this helpful.
Related
Hi Everyone I want to draw lines to Jlabel Icon with DrawLines() class but the program isn't drawing .When I use to frame.add(m) the program is drawing lines to frame .....
DrawLine m = new DrawLine();
frame.add(m);
but when I use to label.add(m) method .The program isn't working .I need to figure out this problem Why can't I draw lines to jlabelIcon and How can I fix this ?
This is my DrawLine Class
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
public class DrawLine extends JComponent {
public void paint(Graphics g) {
super.paintComponents(g);
g.drawLine(300, 152, 63, 185);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
g.drawLine(63, 185, 120, 198);
}
}
This is my main class
package com.company;
import com.sun.source.tree.Tree;
import jdk.swing.interop.SwingInterOpUtils;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.awt.geom.Line2D;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class Main extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("Display Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel) frame.getContentPane();
frame.setSize(1000,560);
JLabel label = new JLabel();
label.setSize(1000,560);
label.setIcon(new ImageIcon("myimage path"));
DrawLine m = new DrawLine();
label.add(m);
panel.add(label);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
If your goal is to draw lines and images together, then your best bet for the money is to get rid of ImageIcon and JLabel and instead is to draw them all within a single paintComponent. The images can be drawn as image sprite, and the lines as lines by calling Graphics#drawLine(...) or as Line2D objects as you have using Graphics2D#draw(...)
For example, say we had two BufferedImage objects, upImg and dnImg, and two Point objects that determined the location of these sprites, upPt and dnPt
public class Foo01 extends JPanel {
// .....
private Point upPt = new Point(300, 100);
private Point dnPt = new Point(700, 650);
private BufferedImage upImg, dnImg;
And say we wanted to draw a line that connected the two image sprites, then these could all be draw within the paintComponent method like so:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // tell the JPanel to do its house-keeping painting
// make sure that neither image is null
if (upImg != null && dnImg != null) {
// draw both images at their respective locations
g.drawImage(upImg, upPt.x, upPt.y, this);
g.drawImage(dnImg, dnPt.x, dnPt.y, this);
// to get a smooth line, use rendering hiints
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// to give the line some thickness
g2.setStroke(new BasicStroke(5f));
// calculate the end-points of the line
int x1 = upPt.x + upImg.getWidth() / 2;
int y1 = upPt.y + upImg.getHeight() / 2;
int x2 = dnPt.x + dnImg.getWidth() / 2;
int y2 = dnPt.y + dnImg.getHeight() / 2;
// and then draw it
g.drawLine(x1, y1, x2, y2);
}
}
Here is an example program that does just this -- draws two images with a line connecting. I've also added a MouseAdapter to allow the user to move the first image, the green up-arrow, showing that the line will move as well, since it is calculated within the painting method:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Foo01 extends JPanel {
private static final String UP_IMG_PATH = "https://upload.wikimedia.org/wikipedia/commons/7/7d/Green_circle_icon.jpg";
private static final String DN_IMG_PATH = "https://upload.wikimedia.org/wikipedia/commons/b/bc/Red_circle_icon.jpg";
private static final int GUI_W = 1000;
private static final int GUI_H = 800;
private Point upPt = new Point(300, 100);
private Point dnPt = new Point(700, 650);
private BufferedImage upImg, dnImg;
public Foo01() {
MyMouse myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
setBackground(Color.WHITE);
try {
URL url = new URL(UP_IMG_PATH);
upImg = ImageIO.read(url);
url = new URL(DN_IMG_PATH);
dnImg = ImageIO.read(url);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(GUI_W, GUI_H);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (upImg != null && dnImg != null) {
g.drawImage(upImg, upPt.x, upPt.y, this);
g.drawImage(dnImg, dnPt.x, dnPt.y, this);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(5f));
int x1 = upPt.x + upImg.getWidth() / 2;
int y1 = upPt.y + upImg.getHeight() / 2;
int x2 = dnPt.x + dnImg.getWidth() / 2;
int y2 = dnPt.y + dnImg.getHeight() / 2;
g.drawLine(x1, y1, x2, y2);
}
}
private class MyMouse extends MouseAdapter {
private Point p1 = null;
#Override
public void mousePressed(MouseEvent e) {
if (e.getX() < upPt.x || e.getX() > upPt.x + upImg.getWidth()) {
return;
}
if (e.getY() < upPt.y || e.getY() > upPt.y + upImg.getHeight()) {
return;
}
p1 = new Point(e.getX(), e.getY());
}
#Override
public void mouseReleased(MouseEvent e) {
if (p1 != null) {
moveSprite(e);
p1 = null;
}
}
#Override
public void mouseDragged(MouseEvent e) {
if (p1 != null) {
moveSprite(e);
}
}
private void moveSprite(MouseEvent e) {
Point p2 = new Point(e.getX(), e.getY());
int x = upPt.x + p2.x - p1.x;
int y = upPt.y + p2.y - p1.y;
upPt = new Point(x, y);
p1 = p2;
repaint();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(()-> {
Foo01 foo01 = new Foo01();
JFrame frame = new JFrame("Draw Sprites");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(foo01);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
If the need is to add custom painting to a JLabel you can override its paintComponent:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Main extends JFrame {
private static final String BUG = "https://www.growtopiagame.com/forums/attachment.php?attachmentid=141847&d=1477126665";
public static void main(String[] args) throws MalformedURLException {
JFrame frame = new JFrame("Display Image");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawLine m = new DrawLine("A crossed bug", new ImageIcon(new URL(BUG)));
frame.add(m);
frame.pack();
frame.setVisible(true);
}
}
class DrawLine extends JLabel {
DrawLine(String text, Icon icon) {
super(text, icon, SwingConstants.CENTER);
setVerticalTextPosition(SwingConstants.BOTTOM);
setHorizontalTextPosition(SwingConstants.CENTER);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.YELLOW);
g2d.setStroke(new BasicStroke(10));
int w = getWidth(); int h = getHeight();
g2d.drawLine(0, 0, w, h); //draw right to left diagonal
g2d.drawLine(0, h, w, 0); //draw left to right diagonal
}
}
Swing is a single Thread library. All painting tasks are executed in the Event Dispatcher Thread (EDT).
Running long processes (such as sleep) on the EDT makes keeps this thread busy, so it does not do other things like updating the gui. The gui becomes unresponsive (freezes).
If you want to add a line after a certain delay, use swing Timer for the job:
public class Main extends JFrame {
private static final String BUG = "https://www.growtopiagame.com/forums/attachment.php?attachmentid=141847&d=1477126665";
public static void main(String[] args) throws MalformedURLException {
JFrame frame = new JFrame("Display Image");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawLine m = new DrawLine("A crossed bug", new ImageIcon(new URL(BUG)));
frame.add(m);
frame.pack();
frame.setVisible(true);
}
}
class DrawLine extends JLabel {
private static final int DELAY = 1500; //millies
private boolean isDrawSecondDialgonal = false;
DrawLine(String text, Icon icon) {
super(text, icon, SwingConstants.CENTER);
setVerticalTextPosition(SwingConstants.BOTTOM);
setHorizontalTextPosition(SwingConstants.CENTER);
//use timer to enable painting of a second diagonal
javax.swing.Timer timer = new javax.swing.Timer(DELAY, e-> {
isDrawSecondDialgonal = true;
repaint();
});
timer.setRepeats(false);
timer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.YELLOW);
g2d.setStroke(new BasicStroke(10));
int w = getWidth(); int h = getHeight();
g2d.drawLine(0, 0, w, h); //draw right to left diagonal
if(isDrawSecondDialgonal) {
g2d.drawLine(0, h, w, 0);//draw left to right diagonal
}
}
}
I am currently experimenting with the Graphics2D and the KeyListener, and I am currently testing out rotating objects (in this case a pentagon). Now, it's working in the sense that it rotates, except it is supposed to rotate by 1 radian each time VK_RIGHT or VK_LEFT are pressed.
However, currently it does that for the first key press only. From then on, it creates a pattern of rotating it by 1, 2, 3, 4, 5... and so on radians each time (the nth keypress rotates it by nth radians) instead of just 1 radian per keypress.
Creating the JFrame:
import javax.swing.JFrame;
public class Main {
public Main() {
JFrame window = new JFrame("Rotating Hexagons");
window.setSize(800,600);
window.setLocationRelativeTo(null);
window.setResizable(false);
window.setContentPane(new RotatingHexagon());
window.pack();
window.setVisible(true);
}
public static void main(String[]args) {
new Main();
}
}
RotatingHexagon Class:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class RotatingHexagon extends JPanel implements KeyListener {
private Polygon poly;
private int[] xpoints = { 0, -10, -7, 7, 10 };
private int[] ypoints = { -10, -2, 10, 10, -2 };
private int rotation = 0;
public RotatingHexagon() {
setPreferredSize(new Dimension(800,600));
setFocusable(true);
requestFocus();
}
public void init() {
poly = new Polygon(xpoints, ypoints, xpoints.length);
addKeyListener(this);
}
public void paint(Graphics g) {
init();
Graphics2D g2d = (Graphics2D) g;
int width = getSize().width;
int height = getSize().height;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.WHITE);
g2d.drawString(rotation + " radians", 10, 20);
g2d.translate(width / 2, height / 2);
g2d.scale(20, 20);
g2d.rotate(Math.toRadians(rotation));
g2d.setColor(new Color(255, 100, 100));
g2d.fill(poly);
g2d.setColor(Color.WHITE);
g2d.draw(poly);
}
public void keyPressed(KeyEvent k) {
switch(k.getKeyCode()) {
case KeyEvent.VK_LEFT:
rotation--;
if (rotation < 0) rotation = 359;
repaint();
break;
case KeyEvent.VK_RIGHT:
rotation++;
if (rotation > 360) rotation = 0;
repaint();
break;
}
}
public void keyReleased(KeyEvent k) {}
public void keyTyped(KeyEvent k) {}
}
I really don't have any idea why it isn't just rotating 1 radian each time, so any help is appreciated.
Thanks.
the reason is calling the init() function in the paint() method again and again. So the KeyListener is added multiple times, causing that it is called multiple times, incrementing your counter more every time you press the key.
Move it to the constructor:
public RotatingHexagon() {
setPreferredSize(new Dimension(800,600));
setFocusable(true);
requestFocus();
addKeyListener(this);
}
public void init() {
poly = new Polygon(xpoints, ypoints, xpoints.length);
}
Andy
You should probally just use a persistant AffineTransform to do the rotation. They are a lot more powerfull.
I also saw several issues in your code, you are calling the init method each frame - this could be 60 times per second. In this you are adding a new keylistener each frame. You are also creating a new polygon which would slow down performance.
I've made some changes to your code and and i've used AffineTransforms as an example. Have a look and see if this helps.
package com.joey.testing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AffineTransformTest extends JPanel implements KeyListener {
private Polygon poly;
private int[] xpoints = { 0, -10, -7, 7, 10 };
private int[] ypoints = { -10, -2, 10, 10, -2 };
private int rotation = 0;
AffineTransform transform;
AffineTransform rotationTransform;
AffineTransform translateTransform;
public AffineTransformTest() {
setPreferredSize(new Dimension(800,600));
setFocusable(true);
requestFocus();
//Do Init here - no point in creating new polygon each frame.
//It also adds the key listener each time
init();
updateTransforms();
}
public void init() {
poly = new Polygon(xpoints, ypoints, xpoints.length);
transform = new AffineTransform();
rotationTransform = new AffineTransform();
translateTransform = new AffineTransform();
addKeyListener(this);
}
//Use Paint Compoent
#Override
public void paintComponent(Graphics g) {
//Always call super to clear the screen
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int width = getSize().width;
int height = getSize().height;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.WHITE);
g2d.drawString(rotation + " radians", 10, 20);
//Store old transform so we can apply it
AffineTransform old = g2d.getTransform();
//Add Transform and move polygon
g2d.setTransform(transform);
g2d.setColor(new Color(255, 100, 100));
g2d.fill(poly);
g2d.setColor(Color.WHITE);
g2d.draw(poly);
g2d.setTransform(old);
}
public void keyPressed(KeyEvent k) {
switch(k.getKeyCode()) {
case KeyEvent.VK_LEFT:
rotation--;
if (rotation < 0) rotation = 359;
repaint();
break;
case KeyEvent.VK_RIGHT:
rotation++;
if (rotation > 360) rotation = 0;
repaint();
break;
}
updateTransforms();
}
public void updateTransforms(){
//Resets transform to rotation
rotationTransform.setToRotation(Math.toRadians(rotation));
translateTransform.setToTranslation(getWidth()/2, getHeight()/2);
//Chain the transforms (Note order matters)
transform.setToIdentity();
transform.concatenate(translateTransform);
transform.concatenate(rotationTransform);
}
public void keyReleased(KeyEvent k) {}
public void keyTyped(KeyEvent k) {}
public static void main(String input[]){
JFrame f= new JFrame("AffineTransform");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(100, 100);
f.setResizable(true);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(new AffineTransformTest());
f.show();
}
}
I'm making my first steps with desktop GUI design. What I'm trying to achieve is displaying in one of table columns. I want them to be visually attractive and also clickable.
At first, I thought that it's possible to do that with JButton with some hacks, as suggested in this thread. But then I realized that I don't know how to manage dynamic width of labels and stretching the graphics to fill buttons in 100%.
I want to achieve something similar to image below. In HTML+CSS it's very easy, each tag:
has left edge as graphics
has repeatable 1px wide background
has right edge as graphics
But how to do that in Java?
info: I don't care if this is a JButton or not, as long as it's nice, clickable and draggable.
UPDATE
Mockup below represents what I want to do. It involves JTable or multiple JLists as well as those tags floating left next to the word. It's easy to do in HTML+CSS, but almost impossible for me to do that in Java.
I have no doubt there are multitudes of ways to achieve this, a better way would be to simply write a UI delegate, but let's try and keep it basic for the moment...
(I've updated the button code, so I've included it in the update)
The problem you have is JTable just isn't designed to do what you are trying to. Grouped rows and variable row heights are not easily achieved.
This is just an example, but what I've done is generate a series of compound components which "mimic" your basic requirements...
The following makes use `WrapLayout' written by own camrik and SwingLabs, SwingX libraries
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.DefaultButtonModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
import org.jdesktop.swingx.*;
public class TestTagButton {
public static void main(String[] args) {
new TestTagButton();
}
public TestTagButton() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JPanel wordsPane = new JPanel(new VerticalLayout());
wordsPane.setBackground(Color.WHITE);
Word word = new Word("Hand");
word.addTag("body parts", 55);
List<String> translations = new ArrayList<>(3);
translations.add("Reka");
translations.add("Dion");
translations.add("Garsec");
wordsPane.add(new WordGroupPane(word, translations));
word = new Word("Roof");
word.addTag("house", 17);
word.addTag("architecture", 8);
translations = new ArrayList<>(3);
translations.add("Dach");
translations.add("Zadaszenie");
wordsPane.add(new WordGroupPane(word, translations));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
// frame.add(new JScrollPane(wordsPane));
frame.add(wordsPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class WordGroupPane extends JPanel {
public WordGroupPane(Word word, List<String> translations) {
setOpaque(false);
setLayout(new GridLayout(0, 2));
add(new WordPane(word));
add(new TranslationsPane(translations));
}
}
public static class TranslationsPane extends JPanel {
protected static final Border SPLIT_BORDER = new MatteBorder(0, 0, 1, 0, Color.GRAY);
public TranslationsPane(List<String> translations) {
setOpaque(false);
setLayout(new GridLayout(0, 1));
for (String translation : translations) {
JLabel lbl = new JLabel(translation);
lbl.setHorizontalAlignment(JLabel.LEFT);
lbl.setBorder(SPLIT_BORDER);
add(lbl);
}
}
}
public static class WordPane extends JPanel {
public WordPane(Word word) {
setBorder(new MatteBorder(0, 0, 1, 1, Color.GRAY));
setOpaque(false);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(10, 8, 10, 8);
JLabel label = new JLabel(word.getWord());
add(label, gbc);
gbc.gridx = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 0, 0);
JPanel tagsPane = new JPanel(new WrapLayout(WrapLayout.LEFT));
tagsPane.setOpaque(false);
for (Tag tag : word.getTags()) {
TagButton tb = new TagButton(tag.getText());
Font font = tb.getFont();
font = font.deriveFont(font.getSize() - 3f);
tb.setFont(font);
tb.setTag(Integer.toString(tag.getCount()));
tb.setSelected(true);
tagsPane.add(tb);
}
add(tagsPane, gbc);
}
}
public class Word {
private String word;
private List<Tag> tags;
public Word(String word) {
this.word = word;
this.tags = new ArrayList<>(25);
}
public void addTag(String text, int count) {
addTag(new Tag(text, count));
}
public String getWord() {
return word;
}
public List<Tag> getTags() {
return tags;
}
public void addTag(Tag tag) {
tags.add(tag);
}
}
public class Tag {
private String text;
private int count;
public Tag(String text, int count) {
this.text = text;
this.count = count;
}
public String getText() {
return text;
}
public int getCount() {
return count;
}
}
public static class TagButton extends AbstractButton {
private JLabel renderer;
private String tag;
public TagButton(String text) {
this();
setText(text);
}
public TagButton() {
setModel(new DefaultButtonModel());
setMargin(new Insets(8, 8, 8, 8));
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
getModel().setPressed(true);
}
#Override
public void mouseReleased(MouseEvent e) {
getModel().setPressed(false);
getModel().setSelected(!isSelected());
}
});
setFont(UIManager.getFont("Button.font"));
}
public void setTag(String value) {
if (tag == null ? value != null : !tag.equals(value)) {
String old = tag;
tag = value;
firePropertyChange("tag", old, tag);
revalidate();
}
}
public String getTag() {
return tag;
}
protected JLabel getRenderer() {
if (renderer == null) {
renderer = new JLabel(getText());
}
return renderer;
}
#Override
public Dimension getPreferredSize() {
Insets margin = getMargin();
Dimension size = new Dimension();
size.width = margin.left + margin.right;
size.height = margin.top + margin.bottom;
JLabel renderer = getRenderer();
renderer.setText(getText());
size.width += renderer.getPreferredSize().width;
size.height += renderer.getPreferredSize().height;
size.width += getTagWidth();
return size;
}
protected int getTagWidth() {
JLabel renderer = getRenderer();
renderer.setText(getTag());
renderer.setFont(getFont());
return renderer.getPreferredSize().width + 16;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// int fullWidth = getTagWidth() + 8;
int width = getTagWidth() + 8;
int height = getHeight() - 3;
int tagWidth = getWidth() - 1 - width;
Shape insert = new TagInsert(width, height);
int x = getWidth() - width - 1;
if (!isSelected()) {
x -= getTagWidth();
}
x -= 4;
g2d.translate(x, 1);
g2d.setPaint(new Color(242, 95, 0));
g2d.fill(insert);
g2d.setPaint(new Color(222, 83, 0));
g2d.draw(insert);
Stroke stroke = g2d.getStroke();
BasicStroke stitch = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f, new float[]{3f}, 0f);
g2d.setStroke(stitch);
g2d.setColor(new Color(167, 65, 1));
g2d.drawLine(0, 2, width, 2);
g2d.drawLine(0, height - 2, width, height - 2);
g2d.setColor(new Color(249, 127, 50));
g2d.drawLine(0, 3, width, 3);
g2d.drawLine(0, height - 1, width, height - 1);
g2d.setStroke(stroke);
if (isSelected()) {
JLabel renderer = getRenderer();
renderer.setFont(getFont());
renderer.setText(getTag());
renderer.setSize(width - 8, renderer.getPreferredSize().height);
renderer.setForeground(Color.WHITE);
renderer.setHorizontalAlignment(JLabel.CENTER);
int xPos = 4;//((tagWidth - renderer.getWidth()) / 2);
int yPos = (height - renderer.getHeight()) / 2;
g2d.translate(xPos, yPos);
renderer.printAll(g2d);
g2d.translate(-xPos, -yPos);
}
g2d.translate(-x, -1);
height = getHeight() - 1;
Shape baseShape = new TagShape(tagWidth, height);
LinearGradientPaint lgpFill = new LinearGradientPaint(
new Point(0, 0),
new Point(0, getHeight() - 1),
new float[]{0f, 1f},
new Color[]{new Color(248, 248, 248), new Color(241, 241, 241)}
);
g2d.setPaint(lgpFill);
g2d.fill(baseShape);
LinearGradientPaint lgpOutline = new LinearGradientPaint(
new Point(0, 0),
new Point(0, getHeight() - 1),
new float[]{0f, 1f},
new Color[]{UIManager.getColor("Button.shadow"), UIManager.getColor("Button.darkShadow")}
);
g2d.setPaint(lgpOutline);
g2d.draw(baseShape);
JLabel renderer = getRenderer();
renderer.setFont(getFont());
renderer.setText(getText());
renderer.setSize(renderer.getPreferredSize());
renderer.setForeground(getForeground());
x = (tagWidth - renderer.getWidth()) / 2;
int y = (height - renderer.getHeight()) / 2;
renderer.setLocation(x, y);
g2d.translate(x, y);
renderer.printAll(g2d);
g2d.translate(-x, -y);
g2d.setColor(Color.RED);
// g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g2d.dispose();
}
}
protected static class TagInsert extends Path2D.Float {
public TagInsert(float width, float height) {
float gap = 3;
float radius = (height - (gap * 5)) / 4f;
moveTo(0, 0);
lineTo(width, 0);
float yPos = 0;
lineTo(width, 1);
float topY = gap;
for (int index = 0; index < 4; index++) {
float bottomY = topY + radius;
float x = width - (radius / 2);
lineTo(width, topY);
curveTo(x, topY, x, bottomY, width, bottomY);
topY += radius;
topY += gap;
lineTo(width, topY);
}
lineTo(width, height);
lineTo(0, height);
lineTo(0, 0);
}
}
protected static class TagShape extends Path2D.Float {
protected static final float RADIUS = 8;
public TagShape(float width, float height) {
moveTo(RADIUS, 0);
lineTo(width, 0);
float clip = RADIUS / 2f;
float topY = (height / 2f) - clip;
float bottomY = (height / 2f) + clip;
lineTo(width, topY);
curveTo(width - clip, topY, width - clip, bottomY, width, bottomY);
lineTo(width, height);
lineTo(RADIUS, height);
curveTo(0, height, 0, height, 0, height - RADIUS);
lineTo(0, RADIUS);
curveTo(0, 0, 0, 0, RADIUS, 0);
}
}
}
use javafx buttons rather than swing.
javafx buttons have css properties.
Based on your requirements it does seem like JButton is a good starting point because it has most of what you want.
As far as the width goes, you will most likely need to subclass JButton so that you can you can update the width in the constructor, something like this:
private static class FancyButton extends JButton{
public FancyButton(String name) {
super(name);
//Calculate new width
setPreferredSize(new Dimension(newWidth, 30));
}
}
Or have a method that sets up your buttons that determines and sets the width.
As you want to use these in a JTable, you'll need a custom renderer that draws the button with or without the number showing. The numeric value and state should be stored in your TableModel. To get the behavior of a button, you'll need a custom editor, perhaps one returning an instance of JToggleButton. In this example, an instance of JCheckBox (a subclass of JToggleButton) is used to display both state and value in a column.
I am trying to make a piece of a JPanel transparent, but I cannot quite get it to work. Is it possible to do this?
import java.awt.*;
import javax.swing.*;
public class ClearPanel extends JPanel{
public static void main(String[] args) {
ClearPanel c = new ClearPanel();
c.setPreferredSize(new Dimension(200, 200));
c.setOpaque(false);
JPanel backPanel = new JPanel();
backPanel.setBackground(Color.CYAN);
backPanel.add(c);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(backPanel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(0, 0, 200, 200);
g.clearRect(45, 45, 50, 50);
Graphics2D g2 = (Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f));
g2.fillRect(75, 75, 50, 50);
}
}
The oval should be opaque, but the rectangles I would like to be transparent. By transparent, I mean that I should be able to see the panel behind the ClearPanel.
Going off of MadProgrammer's answer, is there any way to make that gray box draw where it is outside of the area, but remain transparent where it is in the area?
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle fill = new Rectangle(getWidth(), getHeight());
Graphics2D g2d = (Graphics2D) g.create();
Rectangle hole = new Rectangle(0, 0, 100, 100);
Area area = new Area(fill);
area.subtract(new Area(hole));
g2d.setColor(getBackground());
g2d.fill(area);
g2d.setColor(Color.RED);
g2d.setComposite(AlphaComposite.SrcOver.derive(0.0f));
g2d.fill(hole);
g2d.setComposite(AlphaComposite.SrcOver.derive(1.0f));
g2d.setColor(Color.DARK_GRAY);
if(area.contains(0,0,100,200))
g2d.fillRect(0, 0, 100, 200);
g2d.dispose();
}
The problem you have is, by default, JPanel is opaque, meaning that the repaint will NOT paint anything under it.
You need to set the the panel to transparent and then take over the painting of the background.
Now, the real trick begins. If you simply fill the component and then try and paint transparent section over the top of it, you will simply be painting a transparent section over a opaque background...not very helpful.
What you need to do is not fill the area you want to remain transparent.
You can accomplish this by using a Area shape, which has a neat trick of been able to append/add and remove shapes from it.
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TransparentPane {
public static void main(String[] args) {
new TransparentPane();
}
public TransparentPane() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
BackgroundPane backgroundPane = new BackgroundPane();
backgroundPane.setBackground(Color.RED);
backgroundPane.setLayout(new BorderLayout());
backgroundPane.add(new TranslucentPane());
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(backgroundPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BackgroundPane extends JPanel {
private BufferedImage bg;
public BackgroundPane() {
try {
bg = ImageIO.read(new File("/path/to/your/image.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return bg == null ? super.getPreferredSize() : new Dimension(bg.getWidth(), bg.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null) {
int width = getWidth() - 1;
int height = getHeight() - 1;
int x = (width - bg.getWidth()) / 2;
int y = (height - bg.getHeight()) / 2;
g.drawImage(bg, x, y, this);
}
}
}
public class TranslucentPane extends JPanel {
public TranslucentPane() {
setOpaque(false);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle fill = new Rectangle(getWidth(), getHeight());
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth() - 1;
int height = getHeight() - 1;
int radius = Math.min(width, height) / 2;
int x = (width - radius) / 2;
int y = (height - radius) / 2;
Ellipse2D hole = new Ellipse2D.Float(x, y, radius, radius);
Area area = new Area(fill);
area.subtract(new Area(hole));
g2d.setColor(getBackground());
g2d.fill(area);
g2d.setColor(Color.RED);
g2d.setComposite(AlphaComposite.SrcOver.derive(0.25f));
g2d.fill(hole);
g2d.dispose();
}
}
}
Update
Well, that took a little longer the I expected...
Basically, we need to create a mask of the shape that subtracts the hole from the rectangle we want to display, then subtract that result from the rectangle we want to diplay
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TransparentPane {
public static void main(String[] args) {
new TransparentPane();
}
public TransparentPane() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
BackgroundPane backgroundPane = new BackgroundPane();
backgroundPane.setBackground(Color.RED);
backgroundPane.setLayout(new BorderLayout());
backgroundPane.add(new TranslucentPane());
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(backgroundPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BackgroundPane extends JPanel {
private BufferedImage bg;
public BackgroundPane() {
try {
bg = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/Evil_Small.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return bg == null ? super.getPreferredSize() : new Dimension(bg.getWidth(), bg.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null) {
int width = getWidth() - 1;
int height = getHeight() - 1;
int x = (width - bg.getWidth()) / 2;
int y = (height - bg.getHeight()) / 2;
g.drawImage(bg, x, y, this);
}
}
}
public class TranslucentPane extends JPanel {
public TranslucentPane() {
setOpaque(false);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle fill = new Rectangle(getWidth(), getHeight());
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth() - 1;
int height = getHeight() - 1;
int radius = Math.min(width, height) / 2;
int x = (width - radius) / 2;
int y = (height - radius) / 2;
Ellipse2D hole = new Ellipse2D.Float(x, y, radius, radius);
Area area = new Area(fill);
area.subtract(new Area(hole));
g2d.setColor(getBackground());
g2d.fill(area);
g2d.setColor(Color.RED);
g2d.setComposite(AlphaComposite.SrcOver.derive(0.0f));
g2d.fill(hole);
g2d.dispose();
g2d = (Graphics2D) g.create();
// Basically, we create an area that is subtraction of the window/rectangle
// from the whole. This leaves us with a rectangle (with a hole in it)
// that doesn't include the area where the whole is...
Rectangle win = new Rectangle(
x + (radius / 2),
y + (radius / 2), radius, (radius / 4));
area = new Area(win);
area.subtract(new Area(hole));
// Then we create a area that is a subtraction of the original rectangle
// from the one with a "hole" in it...
Area actual = new Area(win);
actual.subtract(area);
g2d.setColor(Color.BLUE);
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
g2d.fill(actual);
g2d.dispose();
}
}
}
I'm trying to write the game of life. However, no matter how hard I try, I can't seem to add Cell components onto the frame with the buttons. They do appear if I make a new frame and add them, but even so, I can't seem to add more than one component.
So I how I add the components to the frame with all the stuff on it and how do I add multiple components?
Thanks!
Here's my code:
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Visuals extends JFrame implements ActionListener{
private static Board b;
private GameOfLife g;
private JButton nextGen;
private JButton random;
public Visuals(Board b){
super("The Game of Life");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
nextGen = new JButton("Next Generation");
panel.add(nextGen);
nextGen.addActionListener(this);
random = new JButton("Random");
panel.add(random);
random.addActionListener(this);
this.add(panel);
this.setSize(800, 700);
this.setVisible(true);
/*for(int i=0, yShift=80; i<b.getRow(); i++, yShift+=20){
for (int j=0, xShift=30; j<b.getCol(); j++, xShift+=20){
Cell c= new Cell(xShift,yShift,i,j);
this.add(c);
}
}*/
Cell c= new Cell(100,100,1,1);
Cell d= new Cell(200,200,1,1);
this.add(c);
this.add(d);
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 700);
frame.setVisible(true);
frame.add(c);
frame.add(d);
}
public void paint(Graphics g){
super.paint(g);
for(int i=0, yShift=80; i<b.getRow(); i++, yShift+=20){
for (int j=0, xShift=30; j<b.getCol(); j++, xShift+=20){
if(b.getSpot(i, j)==1){
g.setColor(Color.red);
g.fillRect(xShift, yShift, 10, 10);
}
else{
g.setColor(Color.white);
g.fillRect(xShift, yShift, 10, 10);
}
}
}
}
public static void main(String[] cnbe){
b= new Board (30,30);
Visuals v = new Visuals(b);
}
public void actionPerformed(ActionEvent event) {
Object cause = event.getSource();
if(cause == nextGen){
GameOfLife g=new GameOfLife(b);
g.nextGeneration();
System.out.println(g);
this.repaint();
}
if(cause == random){
GameOfLife g=new GameOfLife(b);
g.random();
System.out.println(g);
this.repaint();
}
}
public class Cell extends JComponent{
int row;
int col;
int x;
int y;
Rectangle r;
public Cell(int x, int y, int row, int col){
r= new Rectangle(x,y,10,10);
this.row=row;
this.col=col;
this.x=x;
this.y=y;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("DRAW ME",200,200);
Graphics2D g2= (Graphics2D) g;
g2.setColor(Color.green);
r= new Rectangle(x,y,100,100);
g2.fill(r);
/*if(b.getSpot(row, col)==1){
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
}
else{
g.setColor(Color.white);
g.fillRect(x, y, 10, 10);
}*/
}
class MousePressListener implements MouseListener{
public void mousePressed(MouseEvent event) {
/*int x= event.getX();
int y= event.getY();
if(x>r.getX() && x<r.getX()+10 && y>r.getY() && y<r.getY()+10){
if(b.getSpot(row, col)==1){
b.setSpot(row, col, 0);
repaint();
}
else{
b.setSpot(row, col, 1);
repaint();
}
}*/
}
public void mouseReleased(MouseEvent arg0) {
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
}
}
}
You have an issue with Layout Managers. Each time you add a component to the JFrame you overwrite what was already there. I'm not too familiar with Game of Life, but I don't think you want your cells to be JComponents. You can represent your board as an array of booleans, representing the alive and dead state, so your Visual constructor becomes:
public Visuals(Board b) {
super("The Game of Life");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = getContentPane(); // 1
JPanel panel = new JPanel(new FlowLayout()); // 2
nextGen = new JButton("Next Generation");
panel.add(nextGen);
nextGen.addActionListener(this);
random = new JButton("Random");
panel.add(random);
random.addActionListener(this);
container.add(panel); // 3
setSize(800, 700);
setVisible(true);
}
In your Board class you would represent the board as an array:
public class Board{
private boolean[][] board;
public Board(int x, int y) {
board = new boolean[x][y];
}
}
Then in the nextGeneration method of your GameOfLife class, perform whatever calculations you're doing and set the true/false (or alive/dead) state of the positions on the Board.
Your Visuals.paint method then becomes something like:
public void paint(Graphics g) {
super.paint(g);
for (int i = 0, yShift = 80; i < b.getRow(); i++, yShift += 20) {
for (int j = 0, xShift = 30; j < b.getCol(); j++, xShift += 20) {
if (b.isAlive(i, j)) {
g.setColor(Color.red);
g.fillRect(xShift, yShift, 10, 10);
} else {
g.setColor(Color.white);
g.fillRect(xShift, yShift, 10, 10);
}
}
}
}
iaAlive is just returning the value of the board array at that position:
public boolean isAlive(int i, int j) {
return board[i][j];
}
But like I say, I'm not familiar with the game.