How to swap colors of 2 objects - java

Here's my code in question:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import sun.java2d.loops.DrawRect;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Board extends JPanel implements MouseListener
{
//instance variables
private int width;
private int height;
private Block topLeft;
private Block topRight;
private Block botLeft;
private Block botRight;
public Board() //constructor
{
width = 200;
height = 200;
topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
setBackground(Color.WHITE);
setVisible(true);
//start trapping for mouse clicks
addMouseListener(this);
}
//initialization constructor
public Board(int w, int h) //constructor
{
width = w;
height = h;
topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
setBackground(Color.WHITE);
setVisible(true);
//start trapping for mouse clicks
addMouseListener(this);
}
public void update(Graphics window)
{
paint(window);
}
public void paintComponent(Graphics window)
{
super.paintComponent(window);
topRight.draw(window);
topLeft.draw(window);
botRight.draw(window);
botLeft.draw(window);
}
public void swapTopRowColors()
{
Color temp = topLeft.getColor(topRight);
topRight.setColor(temp);
repaint();
}
public void swapBottomRowColors()
{
}
public void swapLeftColumnColors()
{
}
public void swapRightColumnColors()
{
}
How would I swap the colors of 2 of these "squares" using the .getColor() method? I'm thinking I'm on the right track to achieving it but haven't had to do something like this with colors before.

You're going to need to use setColor(), but before that you need to create a temp of one of the colours.
public void swapColors(Block g1, Block g2) {
Color c = g1.getColor();
g1.setColor(g2.getColor());
g2.setColor(c);
repaint();
}
Also using this method header, you can swap two colours from the Block objects without needing a different method for each combination, just pass through the two you want to swap as arguments.
EDIT:
It seems you need to add a getters and setters to your Block class for color, so just add:
public Color getColor() {
return this.color;
}
public void setColor(Color c) {
this.color = c;
}

public void swapTopRowColors()
{
Color temp = topLeft.getColor(topRight);
topLeft.setColor(topRight.getColor()); //<-- line you're missing
topRight.setColor(temp);
repaint();
}
=== Following comment ===
you'll need to add getter and setter in your Block class:
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}

You have 2 almost indentical constructors Board() and Board(h,w), su in default constructor call:
public Board() //constructor
{
Board(200,200);
}
If you use this method, in future you will need to edit only one constructor, not both.

Related

How to change the color of a panel when I pass over it with another one?

I'm creating a code where I need to change a Panel color with the one I'm still pressed on when I pass over it. For instance if let's say I pressed on a green panel and drag it over another one, this one should get the green color. However it doesn't work everytime, like sometimes it does change the color but sometimes it doesn't.
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.util.Random;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.*;
import javax.swing.event.MouseInputListener;
public class Etabli extends JFrame {
JPanel paneprinci;
CarrePanel selected;
public Etabli() {
this.setVisible(true);
setTitle("Cadre");
setDefaultCloseOperation(EXIT_ON_CLOSE);
paneprinci=new JPanel(null);
setSize(600,600);
selected=new CarrePanel();
for (int i=0;i<10;i++) {
paneprinci.add(new CarrePanel(new Carre()));
}
this.getContentPane().add(paneprinci);
}
public static void main (String[]args) {
javax.swing.SwingUtilities.invokeLater(
new Runnable() {
public void run() {
Etabli e=new Etabli();
}
});
}
public class CarrePanel extends JPanel implements MouseInputListener{
private Carre carre;
private boolean etat;
private int xprev;
private int yprev;
public CarrePanel(Carre c) {
setBounds(new Random().nextInt(500),new Random().nextInt(500), 50, 50);
addMouseListener(this);
addMouseMotionListener(this);
this.setBackground(c.getColor());
this.carre=c;
}
public CarrePanel() {
setBounds(new Random().nextInt(500),new Random().nextInt(500), 50, 50);
addMouseListener(this);
addMouseMotionListener(this);
}
public void setCarre(Carre c) {
carre=c;
}
public Carre getCarre() {
return carre;
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(this.carre.getColor());
}
#Override
public void mouseEntered(MouseEvent e) {
if (selected.getCarre()!=null) {
this.carre.setColor(selected.getCarre().getColor());
this.setBackground(selected.getCarre().getColor());
}
}
#Override
public void mousePressed(MouseEvent e) {
etat=true;
selected.setCarre(this.carre);
xprev=e.getXOnScreen();
yprev=e.getYOnScreen();
}
#Override
public void mouseReleased(MouseEvent e) {
etat=false;
if(selected.getCarre()==this.carre) {
selected.setCarre(null);;
}
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void mouseDragged(MouseEvent e) {
if(etat) {
int x=this.getX()+e.getXOnScreen()-xprev;
int y=this.getY()+e.getYOnScreen()-yprev;
this.setLocation(x,y);
xprev=e.getXOnScreen();
yprev=e.getYOnScreen();
}
}
#Override
public void mouseMoved(MouseEvent e) {
}
}
}
Here's the code for Carre ( which is square in French )
import java.awt.Color;
import java.util.Arrays;
import java.util.Random;
import java.awt.Color;
public class Carre {
private Color color;
public Carre() {
color=new Color(new Random().nextInt(255),new Random().nextInt(255),new Random().nextInt(255));
}
public Color getColor() {
return color;
}
public void setColor(Color c) {
color=c;
}
}
What I don't understand is why does it works sometimes, I don't know if the problem comes from how I did my drag event or if there's something wrong elsewhere.
Thank you for your awnser.
What I don't understand is why does it works sometimes,
When you drag a square to another square you will notice that sometimes the dragged square is painted:
below the other square
above the other square
When the square is painted above the other square a mouseEntered event is not generated because the default logic will only generate the event for the top component.
When the square is painted below the other square, then your mouseEntered event is generated and your logic works as expected.
The order of painting of a component is determined by its ZOrder. So you can adjust the ZOrder of the dragged component to be the last component painted.
In the mousePressed logic you can add:
JPanel parent = (JPanel)getParent();
parent.setComponentZOrder(this, parent.getComponentCount() - 1);
This will make sure that the dragged component is always painted below the other components.
Also note that your etat variable is not needed. The mouseDragged event is only generated while the mouse is pressed.
I created the following GUI.
I added a java.awt.Rectangle to the Carre class to holds the bounds of each rectangle. The Carre class now holds all of the information to draw a Carre instance.
I created a CarreModel class to hold a java.util.List of Carre instances. I create all of the Carre instances in the constructor of the CarreModel class.
I created one JFrame and one drawing JPanel. I left your JFrame extend, but generally, you should use Swing components. You only extend a Swing component, like JPanel in CarrePanel, when you want to override one of the class methods.
I separated the MouseListener and MouseMotionListener from the CarrePanel JPanel class. I find it helps to keep separate functions in separate methods and classes. It makes it much easier to focus on one part of the application at a time.
The MouseListener mousePressed method has a function at the bottom to move the selected carre to the top of the Z-order. This makes the carre move more visually appealing.
The MouseListener updateCarreColor method checks for carre intersections and changes the color of any carre that intersects the selected carre.
Here's the complete runnable code. I made all the classes inner classes so I can post the code as one block.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
public class Etabli extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Etabli();
}
});
}
private CarreModel model;
private CarrePanel paneprinci;
public Etabli() {
this.model = new CarreModel();
this.setTitle("Cadre");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
paneprinci = new CarrePanel();
this.add(paneprinci, BorderLayout.CENTER);
this.pack();
this.setLocationByPlatform(true);
this.setVisible(true);
}
public class CarrePanel extends JPanel {
private static final long serialVersionUID = 1L;
public CarrePanel() {
this.setPreferredSize(new Dimension(550, 550));
ColorListener listener = new ColorListener();
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Carre carre : model.getCarres()) {
g2d.setColor(carre.getColor());
Rectangle r = carre.getBounds();
g2d.fillRect(r.x, r.y, r.width, r.height);
}
}
}
public class ColorListener implements MouseInputListener {
private Carre selectedCarre;
private Point pressedPoint;
#Override
public void mouseClicked(MouseEvent event) {
}
#Override
public void mouseEntered(MouseEvent event) {
}
#Override
public void mousePressed(MouseEvent event) {
pressedPoint = event.getPoint();
selectedCarre = null;
for (Carre carre : model.getCarres()) {
if (carre.getBounds().contains(pressedPoint)) {
selectedCarre = carre;
break;
}
}
if (selectedCarre != null) {
List<Carre> carres = model.getCarres();
carres.remove(selectedCarre);
carres.add(selectedCarre);
}
}
#Override
public void mouseReleased(MouseEvent event) {
updateCarrePosition(event.getPoint());
updateCarreColor();
}
#Override
public void mouseExited(MouseEvent event) {
}
#Override
public void mouseDragged(MouseEvent event) {
updateCarrePosition(event.getPoint());
updateCarreColor();
}
#Override
public void mouseMoved(MouseEvent event) {
}
private void updateCarrePosition(Point point) {
int x = point.x - pressedPoint.x;
int y = point.y - pressedPoint.y;
if (selectedCarre != null) {
selectedCarre.incrementBounds(x, y);
paneprinci.repaint();
pressedPoint.x = point.x;
pressedPoint.y = point.y;
}
}
private void updateCarreColor() {
if (selectedCarre != null) {
for (Carre carre : model.getCarres()) {
if (!carre.equals(selectedCarre) &&
carre.getBounds().intersects(selectedCarre.getBounds())) {
carre.setColor(selectedCarre.getColor());
paneprinci.repaint();
}
}
}
}
}
public class CarreModel {
private final List<Carre> carres;
public CarreModel() {
this.carres = new ArrayList<>();
for (int i = 0; i < 10; i++) {
this.carres.add(new Carre());
}
}
public List<Carre> getCarres() {
return carres;
}
}
public class Carre {
private Color color;
private final Random random;
private Rectangle bounds;
public Carre() {
random = new Random();
int red = random.nextInt(128);
int green = random.nextInt(128);
int blue = random.nextInt(128);
color = new Color(red, green, blue);
int x = random.nextInt(500);
int y = random.nextInt(500);
bounds = new Rectangle(x, y, 50, 50);
}
public void incrementBounds(int x, int y) {
bounds.x += x;
bounds.y += y;
}
public Color getColor() {
return color;
}
public void setColor(Color c) {
color = c;
}
public Rectangle getBounds() {
return bounds;
}
}
}

Java Paint Application

I've been working on a Java Paint application for practice; however, the part I'm stuck at right now is how to change the color of my pen without changing the color of anything I have previously drawn? I've been advised to create another ArrayList and incorporate it into my paintComponent but now I'm confused and unsure of what to do. Can anyone help me? I didn't include my tester class but it has the buttons created already, this is just what my code does far.
package drawing;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Drawing extends JPanel {
private final ArrayList<Point> points = new ArrayList<>();
private boolean drawingInProgress;
private Color shapeColor = Color.BLACK;
public void setShapeColor(Color color)
{
this.shapeColor = color;
}
public Drawing(){
setBackground(Color.white);
drawingInProgress = false;
addMouseListener(
new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent ev)
{
if(!drawingInProgress)
{
drawingInProgress = true;
} else {
drawingInProgress = false;
}
}
}
);
addMouseMotionListener(
new MouseMotionAdapter(){
#Override
public void mouseMoved(MouseEvent event)
{
if (drawingInProgress){
points.add(event.getPoint());
repaint();
} else {
}
}
}
);
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//g.setColor(shapeColor); What I had before that was wrong.
for (Point point: points)
g.fillOval(point.x, point.y, 8, 8);
}
public void red() {
shapeColor = Color.RED;
repaint();
}
public void blue() {
shapeColor = Color.BLUE;
repaint();
}
public void green() {
shapeColor = Color.GREEN;
repaint();
}
}
You could create pseudo "shape" which carries not just the information it needs to paint it self, but also the color (and any other properties)
public interface PaintShape {
public Rectangle getBounds();
public Color getColor();
public void paint(Graphics2D g2d);
}
Then you can create what ever shapes you want...
public abstract class AbstractPaintShape implements PaintShape {
private final Rectangle bounds;
private final Color color;
public AbstractPaintShape(Rectangle bounds, Color color) {
this.bounds = bounds;
this.color = color;
}
#Override
public Rectangle getBounds() {
return bounds;
}
#Override
public Color getColor() {
return color;
}
}
public class OvalPaintShape extends AbstractPaintShape {
private Ellipse2D oval;
public OvalPaintShape(Rectangle bounds, Color color) {
super(bounds, color);
oval = new Ellipse2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight());
}
#Override
public void paint(Graphics2D g2d) {
g2d.setColor(getColor());
g2d.fill(oval);
}
}
Or something similar
You should take a closer look at 2D Graphics and Working with Geometry for more ideas
One way to go would be to create a class like MyShape consisting of a list of points (like you already have) and a color.
For the whole painting you would have a list of such MyShape objects.
Now whenever the user begins to draw a line (i.e. mouse-down) you'd create a new MyShape object with the current color and collect all the mouse movements in the points list of the object until mouse-up.
In repaint you'd paint all MyShape's in the list of shapes, each with it's own color.
In this case where you store the points and repaint the list of points, you would have to save the color of each Point with the point information, and then upon drawing ask each point what color it is. Perhaps create a class ColoredPoint that also has a Color.
for (ColoredPoint cp : points) {
g.setColor(cp.getColor());
...
You can refactor this to store the shapes separately to what draws them.
public class Shape {
private final ArrayList<Point> points = new ArrayList<>();
public final Color color;
public Shape() {
this(Color.BLACK);
}
public Shape(final Color color) {
this.color = color;
}
public void addPoint(final Point point) {
this.points.add(point);
}
public List<Point> getPoints() {
return points;
}
}
Your drawing class would then have
public class Drawing extends JPanel {
private final List<Shape> shapes = new ArrayList<>():
// ...
// a mouse listener that creates shapes and gives them points
// ...
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Shape shape: shapes
g.setColor(shape.color);
for (Point point: shape.getPoints()) {
g.fillOval(point.x, point.y, 8, 8);
}
}
}

Java infinite new objects

So what I want to achieve, is to create a simple program that lets the user create rectangles on the screen and then move them around.
I know that I can declare a new object in the code (i.e rectangle i = new rectangle(x,y,sizex,sizey)) however that will create only one, moreover it forces me to declare it in the code:
block1 = new block
block2 = new block
etc
Question is: How can I let the user create infinite rectangles with the use of lets say a button (not necessarily a button, it can be anything) and then let the user be capable of modyfing them (location/size etc).
Examples would be nice. I just feel there is a better way than declaring a gazillion objects in the code and then displaying them one by one. In C++ i could declare a malloc expandable container that would just hold the values, and then just display things using these values, not sure for java.
Simple code for reference:
public static void main(String[] args){
JFrame frame = new JFrame("A.L.T.E.S");
//Container container = frame.getContentPane();
frame.setSize(1024, 768);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Block object = new Block(10,10,20,20);
frame.add(object);
object.reDraw();
}
public class Block extends JPanel{
int yPos;
int xPos;
int xSize;
int ySize;
public Block(int xPos, int yPos, int xSize, int ySize){
this.xPos = xPos;
this.yPos = yPos;
this.xSize = xSize;
this.ySize = ySize;
}
public void setYPos(int yPos){
this.yPos = yPos;
}
public void setXPos(int xPos){
this.xPos = xPos;
}
public void setXSize(int xSize){
this.xSize = xSize;
}
public void setYSize(int ySize){
this.ySize = ySize;
}
public int getYPos(){
return yPos;
}
public int getXPos(){
return xPos;
}
public int getYSize(){
return ySize;
}
public int getXSize(){
return xSize;
}
public void reDraw(){
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(xPos, yPos, xSize, ySize);
}
}
Nothing in Java is simple.
First, Java already has a Rectangle class that holds the origin and size of a Rectangle. In your code, you're making all the rectangles blue. Suppose you want the user to set the color of a Rectangle. You can define your Block class like this.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Block {
private Color color;
private Rectangle rectangle;
public Block(int x, int y, int width, int height) {
this(new Rectangle(x, y, width, height));
}
public Block(Rectangle rectangle) {
this.rectangle = rectangle;
this.color = Color.BLUE;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Rectangle getRectangle() {
return rectangle;
}
public void draw(Graphics g) {
g.setColor(getColor());
g.fillRect(rectangle.x, rectangle.y,
rectangle.width, rectangle.height);
}
}
Next, you need a model class to hold all of the Blocks that your user defines.
Something like this BlockList class.
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
public class BlockList {
private List<Block> blockList;
public BlockList() {
this.blockList = new ArrayList<Block>();
}
public void init() {
this.blockList.clear();
}
public void addBlock(Block block) {
this.blockList.add(block);
}
public void draw(Graphics g) {
for (int i = 0; i < blockList.size(); i++) {
blockList.get(i).draw(g);
}
}
}
Now that you have your GUI model defined, you would build your GUI. Your drawing JPanel would call the draw method in your BlockList class.
I strongly suggest that you go through the Oracle tutorial on Swing. Go through the complete tutorial before you attempt to create a Swing GUI.
In java, use a Collection. In this case, use a List. You can create an ArrayList<Block>, then invoke add to add new rectangles. Then, you can iterate through this collection by using iterator, then calling hasNext and next on the iterator to find all the elements. I'm sure this brief hypnosis is going to be confusing, and if so, check out the Java Tutorial on Collections, which explains everything in all the detail you are going to need.

java animation trouble getting the x and y cordinates of the mouse

so i wrote this code but im not sure of where does the mouse listener goes and also how to loop the program.
im trying to achieve a particle explosion passing the x and y coordinates of the mouse when it's clicked. also i want to loop it till the program closes but i cant figure it out. so far all i got is one explosion
the main class (frame)
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class AnimationOfExplosingSquares extends JFrame implements MouseListener{
private int x;
private int y;
AnimationOfExplosingSquares(){
add(new ExplosingSquares(x,y));
setBackground(Color.BLACK);
}
public static void main (String[] args){
AnimationOfExplosingSquares frame = new AnimationOfExplosingSquares();
frame.setTitle("Squares");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1280, 800);
frame.setVisible(true);
}
#Override
public void mouseClicked(MouseEvent me) {
while(x>0){
this.x=me.getX();
this.y=me.getX();
}
}
#Override
public void mousePressed(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
#Override
public void mouseReleased(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
#Override
public void mouseEntered(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
#Override
public void mouseExited(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
the ExplosingSquares class
import java.awt.Graphics;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ExplosingSquares extends JPanel implements ActionListener
{
private int x;
private int y;
private Timer timer = new Timer(20, this);
private ArrayList<Square> squares=new ArrayList<Square>();
ExplosingSquares(int x,int y){
this.x=x;
this.y=y;
for(int i=0; i<100;i++){
Square squ = new Square(x,y);
squares.add(squ);
}
timer.start();
}
public void paintComponent( Graphics g ){
super.paintComponent( g );
for( Square square : squares ) {
square.boom();
g.setColor( square.getColor() );
// update the square's location
g.fillRect( square.getXCoord(), square.getYCoord(),(int)square.getSize(), (int)square.getSize());
}
}
#Override
public void actionPerformed(ActionEvent ae) {
repaint();
}
}
the Square class
import java.awt.Color;
import java.util.Random;
class Square {
private int width;
private int height;
//direction of x cordinate
private Random random= new Random();
private int randomNumber=(random.nextInt(25)-12);
//direction of y cordinate
private Random rand= new Random();
private int rando=(rand.nextInt(25)-12);
private int x;
private int y;
private double size=50;
Color c=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
Square(){
}
Square(int width, int height){
this.width=width;
this.height=height;
}
Square(int width, int height,Color c){
this.width=width;
this.height=height;
this.c=c;
}
public void setWidth(int width){
this.width=width;
}
public void setHeight(int height){
this.height=height;
}
public void setSize(double size){
this.size=size;
}
public void setColor(Color c){
this.c=c;
}
public int getXCoord() {
return x+width;
}
public int getYCoord(){
return y+height;
}
public double getSize(){
return size;
}
public double getHeight(){
return size;
}
public Color getColor(){
return c;
}
public void boom(){
this.x+=rando;
this.y+=randomNumber;
this.size-=1;
}
}
Swing uses a single thread for painting and event handling. Anything that blocks this thread from executing, will prevent the event dispatch thread form processing repaint requests.
The while loop in the mouseClicked event will stop your program from running.
I suggest you use a javax.swing.Timer instead. See Concurrency in Swing for more details
You should use .getY() if you need the y position.
#Override
public void mouseClicked(MouseEvent me) {
while(x>0){
this.x=me.getX();
this.y=me.getY();
}
}

Component size problem! Resizes to parent's size for reasons I'm unaware of!

I can't seem to figure out why this is happening...
My serverPaddle (it's based on a java.awt.Component) isn't coming out the right size.
I've placed System.out.println(serverPaddle.getSize()); in the thread loop and it shows that the component is the right size for 1 loop and then the next and thereafter, it's the same size as the parent (Container).
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.Color;
import java.awt.Graphics;
private final int FRAMERATE = 60, THIS_MUCH = 1000/FRAMERATE,
BALL_DIAMETER = 30, BALL_SPEED = 15,
PADDLE_WIDTH = 15, PADDLE_HEIGHT = 60, PADDLE_SPEED = 30;
private final Color BALL_COLOR = Color.WHITE, PADDLE_COLOR = Color.WHITE;
private Container c;
private Ball puck;
private Paddle serverPaddle, clientPaddle;
...
private Container c;
...
c = getContentPane();
...
public void run() {
//Center puck
puck = new Ball(c.getWidth()/2 - BALL_DIAMETER/2,
c.getHeight()/2 - BALL_DIAMETER/2, BALL_SPEED);
puck.setSize(BALL_DIAMETER, BALL_DIAMETER);
puck.setForeground(BALL_COLOR);
puck.createHitbox();
puck.setMoving(true);
//West paddle
serverPaddle = new Paddle(PADDLE_WIDTH,
c.getHeight()/2 - PADDLE_HEIGHT/2,
PADDLE_SPEED);
serverPaddle.setSize(PADDLE_WIDTH, PADDLE_HEIGHT);
serverPaddle.setForeground(PADDLE_COLOR);
serverPaddle.createHitbox();
c.add(puck);
c.add(serverPaddle);
//Draw at FRAMERATE frames per second
while (true) {
System.out.println(serverPaddle.getSize());
puck.move(determineSituation());
puck.repaint();
wait(THIS_MUCH);
}
}
This is the Paddle class
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Paddle extends GameObject implements KeyListener {
private int x = 0, y = 0;
public Paddle(int x, int y, int speed) {
super(speed);
this.x = x;
this.y = y;
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void paint(Graphics g) {
setLocation(x, y);
//These two are the culprits, the size is correct when
//I use constants instead.
g.fillRect(0, 0, **getWidth()**, **getHeight()**);
updateHitbox();
}
}
My GameObject class...
import java.awt.Component;
import java.awt.Rectangle;
public class GameObject extends Component {
/* Members */
private int speed = 0;
private boolean isMoving = false;
private Rectangle hitbox;
//Dead south = 0 radians, anti-clockwise
private double direction = 0;
public GameObject(int speed) {
this.speed = speed;
}
/* Accessors */
public boolean isMoving() { return isMoving; }
public void setMoving(boolean isMoving) { this.isMoving = isMoving; }
public int getSpeed() { return speed; }
public void setSpeed(int speed) { this.speed = speed; }
public double getDirection() { return direction; }
public void setDirection(double direction) { this.direction = direction; }
public Rectangle getHitbox() { return hitbox; }
public void createHitbox() {
hitbox = new Rectangle(getX(), getY(), getWidth(), getHeight());
}
public void createHitbox(int x, int y, int width, int height) {
hitbox = new Rectangle(x, y, width, height);
}
public void updateHitbox() {
hitbox.setLocation(getX(), getY());
}
}
even when I comment all the code out for the Ball/puck, it doesn't work. Commenting out the "wait" method doesn't work as well. It just somehow changes for some reason that I don't know but I really wanna know SO I CAN FIX THIS THING!!11
Help! Thanks.
By the way, wait is just calling Thread.sleep
public void wait(int durationInMilliseconds) {
try {
Thread.sleep(durationInMilliseconds);
} catch (InterruptedException e) {
System.out.println(e);
}
}
Firstly, that is an awful lot of code. You want to be able to cut the problem down so that it is small enough that it becomes trivial. And if you want other people to help, it's generally a good idea to be able to produce a complete, compilable program that shows the problem and nothing else.
As it is, it looks as if you are using the content pane of a JFrame with the default LayoutManager. This will default to BorderLayout. When you add components without specifying constraints, they will be added to the "center". Without any side components, the center component will spread out to fill all available area.
So set an appropriate layout manager. I prefer to create a JPanel and use setContentPane, rather than have to bend my code structure around getContentPane.
Any reason you are not using Swing?
Eh...
For some very strange reason unbeknownst to me, when I add this line right below the run() method...
try {
Clip clip = AudioSystem.getClip();
//More to be written
} catch (Exception e) {}
The paddle renders (correct size) correctly.
When I remove it, the problem comes back... so... what the heck?
try {
//Clip clip = AudioSystem.getClip();
//More to be written
} catch (Exception e) {}
You might like this Java 2D games tutorial, featuring a Breakout style game.

Categories

Resources