I tried making two rectangles using the class below: DrawRect but when I create a new DrawRect object it replaces old one's width and height.
package MemDiagramVisualizer;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class DrawRect extends JPanel {
private static int RECT_X;
private static int RECT_Y;
private static int RECT_WIDTH;
private static int RECT_HEIGHT;
public DrawRect(int w, int h) {
RECT_X = 20;
RECT_Y = 20;
RECT_WIDTH = w;
RECT_HEIGHT = h;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(RECT_WIDTH + 2 * RECT_X, RECT_HEIGHT + 2 * RECT_Y);
}
}
JPanel visDisplay = new JPanel();
visDisplay.setLayout(new GridLayout(1,3));
DrawRect rec2 = new DrawRect(40,60);
visDisplay.add(rec2);
DrawRect rec = new DrawRect(100,600);
visDisplay.add(rec);
The code above when added to frame content pane creates two rectangles with 100,600 dimensions
You have a couple of issues in your program:
All these 4 variables:
private static int RECT_X;
private static int RECT_Y;
private static int RECT_WIDTH;
private static int RECT_HEIGHT;
They all are static, but you're trying to change them inside the program, this will apply for all the instances of your program and they all are gonna share the value. In this case I'd suggest you to remove that and you should be good. This is the reason why when you create another class with the same code, it worked.
RECT_X = 20; and RECT_Y = 20; inside the constructor, if these are constants, then initialize them at the top and don't set them in every instance of the class.
Not an error, but depending on your requirements, you might want to stop using multiple JPanels and instead use the Shape API as shown in this answer to create an Array of Shapes that you can draw in a single JPanel. Again, this all depends on your needs.
After removing the static keyword from those constants above, we have this:
Related
Why is only one rectangle being painted here even though I call the repaint() method multiple times in my for() loop? Additionally I want to display 8 rectangles per row and 8 rectangles per column with 2 pixels space in between. Any ideas or help?
package PROG2;
import java.awt.*;
import java.util.*;
import javax.swing.*;
class Model {
int m_width;
int m_height;
int m_x1 = 50;
int m_y1 = 50; //Information about the model
int m_x2 = 100;
int m_y2 = 30;
int counter = 0; //Assisting variable
Model(int width,int height) {
m_width = width;
m_height = height;
}
void math() {
counter = counter + 2;
counter = counter + m_x2;
m_x1 = counter;
}
}
class View extends JComponent {
private Model m_Mod;
View(Model mod) {
m_Mod = mod;
}
#Override
protected void paintComponent(Graphics g) {
//super.paintComponent(g);
g.setColor(Color.green);
g.drawRect(m_Mod.m_x1,m_Mod.m_y1,
m_Mod.m_x2,m_Mod.m_y2);
g.fillRect(m_Mod.m_x1,m_Mod.m_y1,
m_Mod.m_x2,m_Mod.m_y2);
}
}
class Controller {
private Model m_Mod;
private View m_View;
Controller(){
m_Mod = new Model(500,500);
m_View = new View(m_Mod);
JFrame frame = new JFrame();
frame.add(m_View);
frame.setSize(m_Mod.m_width,m_Mod.m_height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Graphics g = frame.getGraphics();
}
void simulate(int right, int down){
for( int i = 0; i < right; i++) {
m_Mod.math();
m_View.repaint();
}
}
}
class Übung2{
public static void main(String[] args) throws Exception {
Controller c = new Controller();
c.simulate(8, 8);
}
}
Even when you call repaint() multiple times, the actual drawing will take place only once at a later time (until swing will trigger a repaint again). At that time, the content of the paintComponent() method is executed. The code you have in your method only draws one rectangle (drawRect()) and one filled rectangle (fillRect()) and that's it, nothing more. So you don't get more than what you wrote in your method.
You can use arrays (or lists) to store the multiple XY coordinates of your rectangles and then iterate over the array/list to draw multiple rectangles while inside the single call of paintComponent(). The code can look like this:
#Override
protected void paintComponent(Graphics g) {
//super.paintComponent(g);
g.setColor(Color.green);
foreach (RectModel rect: m_Mod.getRectangles()) {
g.drawRect(rect.x1, rect.y1, rect.x2, rect.y2);
}
}
This assumes you have such a class RectModel which holds the data of one rectangle (x1, y1, x2, y2) and that you have a method m_Mod.getRectangles() which returns an array/list of RectModel instances. When you execute the simulate() method in your controller, you will calculate all the rectangles you want to draw and save them in the this.rectangles array/list of the Model class. After that the paintComponent() method fill use that array/list and draws the rectangles.
I am trying to create a program in Java that is sort of like a board game. I have gameTiles, currently with only one color as I try to get the layout right. I want the tiles to appear about halfway of the width of the window and extend to the bottom, maybe anywhere from 9x9 or 11x11 different tiles. I have tried to use the grid layout to get these to be close together, not necessarily touching but close enough to look like a board game. However, no matter what I try, the tiles are space so far apart, and change shape when I resize window. I have been using the GridLayout manager to try to achieve this. Here is my code.
import java.awt.GridLayout;
import javax.swing.JFrame;
public class GameWindow extends JFrame {
public GameWindow(String title){
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200,400);
}
/**
* #param args
*/
public static void main(String[] args) {
GameWindow gameWindow = new GameWindow("Game");
gameWindow.setLayout(new GridLayout(0,2));
GameTile greenTile = new GameTile(0,true,0,10);
GameTile greenTile2 = new GameTile(0,true,0,10);
gameWindow.add(greenTile);
gameWindow.add(greenTile2);
gameWindow.setVisible(true);
}
}
This is the GameWindow.java file. The GameTile.java I have so far (which is still mainly not finished just for testing) is as follows:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
* #author Zachary Parks
*
*/
public class GameTile extends JPanel {
private Color color;
private Color[] colors = {Color.BLUE, Color.YELLOW, Color.BLACK};
private int score, multiplier,initialX,initialY;
private boolean positiveEffect;
public GameTile(int colorTile, boolean effect, int initX, int initY){
color = colors[colorTile];
score = getScore(color);
multiplier = getMultiplier(color);
positiveEffect = effect;
initialX = initX;
initialY = initY;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Image image = null;
try {
image = ImageIO.read(new File("greentile.png"));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(image,initialX,initialY,this);
}
private int getMultiplier(Color color2) {
return 0;
}
private int getScore(Color color2) {
return 0;
}
/**
* Method that returns the data from the tile in
* array of int. 0 index = added score, 1 index = tile effect score
* #return
*/
public int[] getData() {
int [] scoreData = null;
scoreData[0] = score*multiplier;
return null;
}
}
A lot of the code is still in progress, like the GameTile's properties, all I'm trying at this point is get the tiles next to each other.
This is what I am getting:
To add tiles like a grid. A tileSize variable is great to have. Let's say the image/tile is 32 pixels.
public static final tileSize = 32;
With this, we can now add tiles using a for loop:
for(int x = 0; x < SCREEN_WIDTH / GameTile.tileSize; x++) { // loop through as many tiles fit the x-axis.
for(int y = 0; y < SCREEN_HEIGHT / GameTile.tileSize; y++) { // do the same with y-axis
GameTile greenTile = new GameTile(0,true, x * GameTile.tileSize , y * GameTile.tileSize);
gameWindow.add(greenTile);
}
}
SCREEN_WIDTH and SCREEN_HEIGHT is the size of your JFrame.
Keep in mind this loops through the whole screen, you wanted the half but it's easily configurable.
Please format your code next time (tab in), much easier to read and help you.
I highly recommend moving image = ImageIO.read(new File("greentile.png")); into the constructor, right now you're loading the image every framerate for every gameTile which will cause performance drop.
Also do not have a JPanel for every GameTile. Instead keep it in your main drawing class and loop through all GameTiles using an ArrayList
public static ArrayList<GameTile> gameTiles = new ArrayList<GameTile>();
protected void paintComponent(Graphics g) {
for(int i = 0; i < gameTiles.size(); i++) {
gameTiles.get(i).draw(g);
}
So instead of adding a JPanel to the JFrame for every gameTile, we draw gameTiles at the specified coordinates. Good luck!
To answer your question in the comment field: the code will look similar to this
public class GameWindow extends JPanel {
public static ArrayList<GameTile> gameTiles = new ArrayList<GameTile>();
public static void main(String[] args) {
new GameWindow();
}
public GameWindow() {
JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setVisible(true);
// add all the tiles (I just use x and y as parameters here)
gameTiles.add(new GameTile(10, 10));
gameTiles.add(new GameTile(50, 10));
}
public void paintComponent(Graphics g) {
for(int i = 0; i < gameTiles.size(); i++) {
gameTiles.get(i).draw(g);
}
}
}
And inside your GameTile class, remove the extends JPanel. And rename the paintComponent as draw or something alike.
However, no matter what I try, the tiles are space so far apart, and change shape when I resize window
A GridLayout expands each cell to fill the space available to the component. If you only have two cells each cell will take up half the width of the frame.
So the solution is to wrap your tile panel into another panel that will respect the preferred size of the tiles. So don't set the layout of the frame, set the layout of a panel holding the tiles:
Something like:
JPanel tilePanel = new JPane( new GridLayout(0, 2, 5, 5) );
tilePanel.add( new GameTile(...) );
tilePanel.add( new GameTile(...) );
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(tilePanel, new GridBagConstraints() );
frame.add(wrapper, BorderLayout.CENTER );
The above code will cause the tiles to be centered in the frame.
frame.add(wrapper, BorderLayout.LINE_END);
The above will cause the tiles to display on the right of the frame vertically centered.
I'm making a chessboard for a project and I have to use JButtons. I'm trying to just set the board up with a blank image I have for each tile, this is the code I have:
Driver
public class Driver
{
public static void main (String[] args)
{
new ChessBoard();
}
}
ChessSquare
import javax.swing.*;
import java.awt.*;
public class ChessSquare
{
public ImageIcon pieceImage;
/** The square's location */
private int xCoord;
private int yCoord;
/** Constructor for the squares */
public ChessSquare(ImageIcon thePieceImage, int theXCoord, int theYCoord)
{
pieceImage = thePieceImage;
xCoord = theXCoord;
yCoord = theYCoord;
}
public int getXCoord()
{
return xCoord;
}
public int getYCoord()
{
return yCoord;
}
}
ChessBoard
public class ChessBoard
{
public ChessBoard()
{
JFrame board = new JFrame();
board.setTitle("Chess Board!");
board.setSize(500,500);
board.setLayout(new GridLayout(8,8));
JPanel grid[][] = new JPanel[8][8];
ImageIcon empty = new ImageIcon("/pieces/EmptySquare.jpg");
for(int x = 0; x<8; x++)
{
for(int y = 0; y<8; y++)
{
ChessSquare s = new ChessSquare(empty, x, y);
JButton square = new JButton(s.pieceImage);
grid[x][y].add(square);
board.setContentPane(grid[x][y]);
}
}
board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board.setVisible(true);
}
}
My code compiles fine but when I run it I get this error:
Exception in thread "main" java.lang.NullPointerException at
ChessBoard.(ChessBoard.java:23) at Driver.main(Driver.java:8)
I don't know what to do to fix this error. Thanks for any help :)
One of the likely causes is ImageIcon empty = new ImageIcon("/pieces/EmptySquare.jpg");...
The path of the image suggest that you are using embedded resources, but ImageIcon(String) treats the value as if it were a file, you can't do this with embedded resources, they aren't files.
Instead, you need to use something more like ImageIcon empty = new ImageIcon(getClass().getResource("/pieces/EmptySquare.jpg"));.
Personally, I'd recommend that you should be using ImageIO.read(getClass().getResource("/pieces/EmptySquare.jpg")) as this will throw an exception if the resource can not be loaded from some reason, rather then failing silently.
grid[x][y].add(square); also won't work, as you've not assigned anything to grid[x][y]
grid[x][y] = new JPanel();
grid[x][y].add(square);
Might work better, but I don't know why you're doing this, when doing something like...
JButton grid[][] = new JButton[8][8];
//...
grid[x][y] = square;
Would seem to be more logical for what you are trying to achieve...
Updated...
Instead of board.setContentPane(grid[x][y]); you should be using board.add(grid[x][y]);, other wise you will replace the content pane with the button...since there can only be a single content pane, you'll only get one button...
In grid[x][y].add(square); you are actually calling JPanel.add(), because each element in the array is a JPanel.
So the error is because grid[x][y] is still null you will get a NullPointerException if you call a method on it, like add() in this case.
You want to assign the value since you are using an array
grid[x][y] = square;
I want to define the size of the window, but I did not find a clean way to do it. SetSize() gives a strange result:
public class Test extends GraphicsProgram {
public void run() {
setSize(400, 600);
add(new GLabel("Width: " + getWidth(), 30, 30));
add(new GLabel("Height: " + getHeight(), 30, 50));
}
}
The result is 384 x 542. The gap is always the same (-16 x -58), so it's easy to build a work around. Is there a way to define the size in useful pixels directly?
I found a solution to this while studying the code of a Stanford course project (breakout). GraphicsProgram is not constructed according to its fields, WIDTH, HEIGHT, EAST, CENTER etc, automatically. Therefore we need a resize() to set the window size which acm.program.GraphicsProgram inherits from java.applet.Applet.
Simply adding a resize() and pause() will do the work.
public class Test extends GraphicsProgram {
private static final int WIDTH = 400;
private static final int HEIGHT = 600;
private static final int PAUSE = 10; // or whatever interval you like
public void run() {
this.resize(WIDTH,HEIGHT);
pause(PAUSE);
// game logic here
...
}
}
The pause() is necessary as the resize takes time. It avoids the mis-displacement of components if you are adding them right after resizing.
public void init() {
setSize(400, 600);
}
instead of setting the size in the run method do it on the init!
After class:
public static final int APPLICATION_WIDTH = 900; // x size of window
Before run:
public static final int APPLICATION_HEIGHT = 540; // y size of window
No need for setSize or resize.
You just need to add this two public variables:
/** Width and height of application window in pixels */
public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
and these two if you need exactly WIDTH and HEIGHT to use:
/** Dimensions of window (usually the same) */
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
I am trying to make an original game in Eclipse and I am having some trouble writing code that will remove GObjects and change the properties of others, using the mouseClicked method.
The problem seems to be that the private instant variables are not being recognised in the mousePressed method.
Can someone please advise on what I'm getting wrong here? I have spent a whole day on this and some help will be greatly appreciated.
Kind regards,
Mehul
*/*
* File: Linesv1.java
* 07/08/2012
*
*/
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.MenuEvent;
public class Linesv1 extends GraphicsProgram {
/** Width and height of application window in pixels */
public static final int BACKGROUND_WIDTH = 400;
public static final int BACKGROUND_HEIGHT = 600;
/** Dimensions of game board (usually the same) */
private static final int WIDTH = BACKGROUND_WIDTH;
private static final int HEIGHT = BACKGROUND_HEIGHT;
/** Dimensions of triangle*/
private static final int LENGTH_OF_TRIANGLE_SIDE = 100;
/** Dimensions of arc height*/
private static final int ARC_HEIGHT = 100;
/** Dimensions of radius of switches*/
private static final int SWITCH_RADII = 5;
// private instant variables
private GObject switchA;
private GOval switchB;
private GObject triangle;
private GObject bottomArc;
public void run() {
addMouseListeners();
setUpGame();
}
public void setUpGame(){
//add central triangle
GPolygon triangle = new GPolygon (WIDTH/2,HEIGHT/2);
triangle.addVertex(0,-LENGTH_OF_TRIANGLE_SIDE*2/3);
triangle.addVertex(LENGTH_OF_TRIANGLE_SIDE/2,+LENGTH_OF_TRIANGLE_SIDE*1/3);
triangle.addVertex(-LENGTH_OF_TRIANGLE_SIDE/2,+LENGTH_OF_TRIANGLE_SIDE*1/3);
triangle.setFilled(true);
triangle.setFillColor(Color.green);
add(triangle);
//add topArc
GArc bottomArc = new GArc (WIDTH/2-LENGTH_OF_TRIANGLE_SIDE/2, HEIGHT/2+LENGTH_OF_TRIANGLE_SIDE*1/3-ARC_HEIGHT/2, ARC_HEIGHT,ARC_HEIGHT,0,-180);
bottomArc.setFilled(true);
bottomArc.setFillColor(Color.green);
add(bottomArc);
//add switches to the bottom of the triangle
GOval switchA = new GOval (WIDTH/2-LENGTH_OF_TRIANGLE_SIDE/2-SWITCH_RADII, HEIGHT/2+LENGTH_OF_TRIANGLE_SIDE*1/3-SWITCH_RADII,SWITCH_RADII*2,SWITCH_RADII*2);
switchA.setFilled(true);
switchA.setFillColor(Color.black);
add(switchA);
//add switches to the bottom of the triangle
GOval switchB = new GOval (WIDTH/2+LENGTH_OF_TRIANGLE_SIDE/2-SWITCH_RADII, HEIGHT/2+LENGTH_OF_TRIANGLE_SIDE*1/3-SWITCH_RADII,SWITCH_RADII*2,SWITCH_RADII*2);
switchB.setFilled(true);
switchB.setFillColor(Color.black);
add(switchB);
}
public void mousePressed(MouseEvent e) {
findObject(e.getX(),e.getY());
GObject check = findObject(e.getX(),e.getY());
if (check!=null){
remove(triangle);
switchA.setColor(Color.cyan);
}
}
private GObject findObject(int a, int b){
if(getElementAt(a,b) != null) {
return getElementAt(a,b);
} else {
return null;
}
}
}*
The triangle you are trying to remove is not the one you added. You created a local GPolygon called triangle in setupGame() and added that, not the private member.
private GObject triangle; // this is uninitialized
public void setUpGame(){
GPolygon triangle = new GPolygon (WIDTH/2,HEIGHT/2); // this is hiding your member variable
// THIS IS LOCAL COPY, NOT MEMBER VARIABLE
add(triangle);
And then later you try to remove the uninitialized member variable, so nothing happens. You probably don't want a separate local copy of triangle. You probably want
public void setUpGame() {
triangle = new GPolygon (WIDTH/2, HEIGHT/2);
// ...
add(triangle); // Now, you are adding the member
}