Swing/AWT/SWT :: BorderLayout - Adding Multiple Video Game Oriented Objects Such As Sprites To JFrame?
May 24, 2014
I decided to write a small Java program to experiment around with BorderLayout, because I'm developing a Java game and I need to have 2 objects placed in a single JFrame at the same time, and everyone I asked said I need BorderLayout to do that.Before you answer: Also, is using BorderLayout the best option for adding multiple video game oriented objects such a sprites to a JFrame?
So the Java program I wrote is supposed to place a JButton on the JFrame and ALSO place a graphic component (a rectangle in this case). The problem is, only the button shows up, as can be seen in the image link below: URL....
Here is the code:
**main.java** --> The main method class + JFrame/JPanel/JButton constructor
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class main {
The program loads, but nothing shows. I did some research and saw that I needed to set the JFrame to visible at the bottom of the frame section. I made the change, but it still does not show anything. Do I need to add everything that I added to the content pane to the frame? From my understanding you add elements to the panel, the panel to the contentPane, then the pane to the frame...
I have to do an assigment using Swing, where I have to make a Board(extends JPanel) and add multiple instances of moving elements to it, with following requirements:
By adding a new object to the Board, the others must not slow down. Also the user interface must not be blocked by the moving objects. So, the movement of the objects must be handled in a separate thread.
and
The application must support adding lots of objects, so it is not okay if every object has its own thread, as this would take too many resources. Design the application in such a way that it uses a constant number of threads, meaning that the number of threads does not grow when adding more objects.
The question is:
How to do this thing with constant number of threads?
I use eclipse as my IDE. I have decided to make my own video game and somebody had sent me a class that could be used for collision detection. The problem with the collision detection class was that it made a box around an object and if something else had touched the object, the collision detection would work. My problem with this class is what happens when i want to use circles? I cant have a box drawn around it. Then it wouldn't work as i would want. Is there a pixel perfect collision detection class out there I can use? It'll be useful in my journey to become successful in computer science! By the way this is in the java language.
So I'm doing a project for my CS class where we have to create a snowman doing something. My picture is of Vince Young in the 2005 Rose Bowl. I'm trying to make the actual video clip from Youtube (do I need to download it? If so I can do that) appear on the scoreboard. As of right now, this is my background file:
import java.awt.*; import java.awt.event.*; import java.awt.Font.*; class snowBackground { public static void drawField(Graphics g, Color field) { Color sky = new Color(95,166,243);
[Code]...
and this is the Driver file (not sure if this is necessary):
import java.awt.*; import java.awt.event.*; import java.awt.Font.*; public class SnowDriverWSB { public static void main(String args[]) { GfxApp gfx = new GfxApp(); gfx.setSize(1000,650);
[Code]...
There are 3 other files that make the actual Snowmen. To do this, I need VERY basic steps, as I'm extremely confused and new to Java. Also, would I need to download the mp3 sound file seperately and play that, or will inserting the video do both?
I am currently in the process of making a game in java. it's going fairly well, however, one problem that i am having is that i cant seem to get my enemy sprites not stack up onto one another. What i want to happen is if two enemy sprites come in contact with one another i want them to touch but not to overlap. However, i cant seem to get this to work.
This is the enemy class:
import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.util.ArrayList; import javax.swing.ImageIcon; public class Enemy extends Base { int x; int y;
Normally, the height of the CENTER component (or position) is greater than that of the NORTH and SOUTH components in a BorderLayout.
Is it possible to increase the size of the SOUTH component and decrease the size of the CENTER position because the jcomponents I want to add in the SOUTH component are bigger? or shall I have to change the layout and use some other layout?
Below are three classes. The first and second create car objects and the third is the main method. My goal is to move two cars across the window. When I compile and run the program both cars are generated however they will not animate. I am moving them with threads but I cannot seem to find why the cars will not move.
import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; public class CarComponent extends JComponent { private Car car1; private Car car2;
import javax.swing.*; import java.awt.event.*; public class menu { public static void main (String[] args){ JFrame frame = new JFrame("Menu"); frame.setVisible (true);
I need adding a textbox for users to input text onto my JFRame. Also how come the
frame.add(o);
is getting an error under the "add".
Main method class:
import javax.swing.JFrame; public class main { public static void main(String[] args) { JFrame frame = new JFrame(); bot o = new bot();
[Code] ......
Other class (this is where I want the textbox to be)
import javax.swing.JTextArea; import javax.swing.JTextField; public class bot { int x; int y; JTextField f = new JTextField(); JTextArea a = new JTextArea(30, 50); }
I've two classes: Student and ArrayListExamples (which has my main method in it).
The student class acts pretty simply at the moment with a constructor that takes a name, surname and ID number. I've tested it and it seems to be working. My issue is with adding an object to an array list, here is my effort:
public class ArrayListExamples { private static int MAX_SIZE = 50; static Scanner sc = new Scanner(System.in); public static void main(String[] args) { ArrayList<Student> students = new ArrayList();
[code]....
I know the last loop to add the student objects is wrong.I'm calling the student ArrayList add method with a student object whose constructor requires two strings and an integer. Why isn't this allowed?
I am trying to add scrollbars to my frame containing many different components and appearing when user resizes the window with the mouse adds pictures and admin of this online application adds labels or other components.
I want scrollbars to appear when the frame is resized and has components you don't see under so you can scroll down.
If I have for example this code, how i add the scrollbars when i make frame smaller ?
public class Test extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() {
Having trouble adding Class (Dollar) objects to a HashSet (money), i have done this before with arraylists and i understand that HashSets are different in that they cannot contain duplicates. Currently when this code is compiled i am getting "null" printed when I run the "howFullDatWallet" method.
import java.util.*; public class Wallet { private HashSet<Dollar> money; private int walletSize = 0; private int walletFiller = 0; /** * Constructor for objects of class Pocket */ public Pocket(int walletCap)
I am trying to build a method that takes an array of object and adds a new object of that type to the end of it . ONLY ALLOWED TO USE ARRAY , NO ARRAYLISTS VECTORS ECT . i realize that if the array is full you can use the java copy array method to make a new array and double its size until a certain point . The MAX AMOUNT OF OBJECTS is a constant that is 100 but for some reason even when executing my code i keep getting null pointer exceptions or index out of bounds errors , i have written this method many times now with out any success.
My question is how do I write a method that adds an object to the end of an array and if there are no spots left copies the current array into a new array and extend the size
private Animal [] objects; final int MAX_ANIMALS = 100; public AnimalObject() { objects = new AnimalObject[MAX_ANIMALS]; } public AnimalObject(Animal[]a)
I made my items class and I am storing my item as a String array to list them all and what I want to be able to do is type pickup, have the game read my location and display the items that are avaible to pickup at current location then type the item and store it in my playerInventory. Then, have it check the slot if it is doesn't equal null go to next.
Use a boolean value of 0 = false and 1 = true. then, have it check if the inv array = 1 or 0. if 1 go to next inv. If all are full then, ask player if they would like to replace an item. Use a 2d array for storage for slots and true or false value.
Here is my code
Player.java package com.PenguinGaming; import java.util.Random; import java.util.Scanner; public class Player { public void User(){ Commands commands = new Commands(); Map map = new Map();
I managed to retrieve data, and set data in my own ways in which I like. But my problem is, if the file does not contain anything (fully empty), when I try to use my
set("", ""); method, it only sets the last one that is called.
I have to read in a file that that has Student, Employee, Faculty....such as name, last name, salary...etc
Example of a file input Java Code: Student, John, Doe, [email]johnDoe[At]yahoo.com[/email], 123,Wonderland Ave, 21323,TX PhoneNumber,Cell,111,222,3333 Student, Jesus,Satan,[email]jesus[At]satan.com[/email,666, HeavenHell Dr., 666666,CA PhoneNumber,Work,111,333,5555 mh_sh_highlight_all('java'); Java Code: while(input.hasNext()){
[code]...
There is more code, but it's repetitive. Now, I keep getting an error. Array of bounds. I believe i get the error because the phone number. The phone number is on the next line..I created a different arraylist for phonenumber, but I dont know how to match it with the correct person, student, employee...etc.
I have been trying to add a draw system to my code so far once all the boxes have been taken it does not tell the player its a draw game over. I have tried and deleted some code but I have commented some stuff that did'nt work at the bottom and also the X's and O's can be replaced in the same spaces?
import java.util.*; //import for scanner public class NoughtsAndCrosses { public static int row,col; //integer for rows and columns public static Scanner scan = new Scanner(System.in); //Scans the program public static char[][] board = new char [4][4]; //Characters for the board set out 4/4
When I add the first car only, it puts it in the correct spot and I can move it correctly. However when I attempt to add another car, the image of the second car repaint itself onto the first one, and the second car is not functioning at all (not moving)...