Java 2D Game - Use Canvas Without Extending It
Apr 10, 2014
Java Code:
public class Game extends Canvas implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public Game() {
setMinimumSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
}
[code]...
I want to remove the extends Canvas from the top line and when I use JFrame to add canvas, I don't do add.(this, but instead I do add a canvas variable I make. So instead of extending I want to make a variable. But then how would I do the start, stop and run?I want to use Graphics, Canvas (Not Jpanel because I want to use BufferedImage) & JFrame. I don't want to extend Canvas, how could I use Canvas identically to shown above but instead of extending using it as a variable? or, how could I do this?
View Replies
ADVERTISEMENT
May 5, 2015
I have to build a simulated OS for my college class. I'm making it pretty simple, nothing too fancy and am using HTML5 Canvas. So far I have the icons on a black background and have made them draggable. I want to know how I can make it so when you click on an icon a box appears (just like a real OS) and then closes by clicking on the 'x'. I also want to be able to drag any icon into the trash bin and it disappears. I understand there are click/mouse events, but I don't know how to implement them into the code I have setup. Here's the code:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="canvas"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/kineticjs/5.2.0/kinetic.js"></script>
<script defer="defer">
var stage = new Kinetic.Stage({
container: 'canvas',
width: 1910,
height: 995,
[Code] ....
View Replies
View Related
Jun 21, 2014
I'm currently having trouble with a Breakout clone that I'm working on when I want to change the scenes of the game. When I start out with the main menu and I hit play, the main menu calls for the StateManager to remove the MainMenu and then to add the GameState to start playing through the levels. But, whenever this is done it just gives a NullPointerException and freezes the game. I know the code for initially starting a State is fine because that is the same way the MainMenu is loaded but there seems to be an issue with changing from one to another...
This is the StateManager class that adds and removes the States/Canvases to and from the JFrame/Main class:
public class StateManager
{
private ArrayList<State> states;
private int currentState;
private JFrame gameFrame;
public StateManager(JFrame gameFrame)
{
this.gameFrame = gameFrame;
states = new ArrayList<State>();
[Code] ....
I've tried messing around with the order of which things are added/loading from the StateManager but am just failing to see what I did wrong.
View Replies
View Related
Jun 22, 2014
I'm currently having trouble with a Breakout clone that I'm working on when I want to change the scenes of the game. When I start out with the main menu and I hit play, the main menu calls for the StateManager to remove the MainMenu and then to add the GameState to start playing through the levels. But, whenever this is done it just gives a NullPointerException and freezes the game. I know the code for initially starting a State is fine because that is the same way the MainMenu is loaded but there seems to be an issue with changing from one to another...
This is the StateManager class that adds and removes the States/Canvases to and from the JFrame/Main class:
package com.pathtocreating.Breakout_V2.state;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JFrame;
public class StateManager {
private ArrayList<State> states;
[Code] ....
The code that calls the change of state in MainMenuState is called in the update method:
if(inputController.isMouseClicked("LeftClick")) {
if(playHover) {
soundManager.playSound("ButtonClick");
BreakoutMain.sm.changeStates("GameState");
[Code] ....
Here is the full stack trace when clicking the area to change states:
Exception in thread "Thread-2" java.lang.NullPointerException
at sun.java2d.SunGraphics2D.validateColor(Unknown Source)
at sun.java2d.SunGraphics2D.<init>(Unknown Source)
at sun.awt.image.SunVolatileImage.createGraphics(Unknown Source)
[Code] ...
It gives that same value both before and after clicking on the region.
Printing out b after the State is changed to GameState gives the following:
java.awt.Component$BltSubRegionBufferStrategy@66116674
I've tried messing around with the order of which things are added/loading from the StateManager but am just failing to see what I did wrong.
View Replies
View Related
Feb 25, 2015
I want to create off-screen images by Canvases in multi-thread environment.
I know that I use methods of GraphicsContext2D to draw on Canvas. Can I do this on Canvas which is not added to Scene ?
How can I get an image from a canvas after I invoke GraphicsContext2D methods? Can I get images in multi-thread environment ?
View Replies
View Related
Jul 25, 2014
Really need some simple and complete example on how to extends components (graphically and functionally) for primefaces 5.
For example I can't figure how to add custom attributes to existing component or define default values for existing attributes.
Is there any tutorial or a basic common way to achieve this goal or each component have to be extended in its way?
View Replies
View Related
Jan 30, 2015
My teacher told me : It looks like everything is working except that in the Box, the method returns true if it is not a box.
The only other thing missing is the if statements in the main method using the .equals() method to do the comparisons. But, now I am lost on how to do my if statement.
HTML Code:
private int height;
/**
* Constructor for objects of class box
*/
public Box3(int l, int w, int h)
{
// call superclass
super(l, w);
// initialise instance variables
height = h;
[Code] ....
View Replies
View Related
Oct 21, 2014
I am writing a program that should take a url and scan the page for any links. It is in the beginning stages, but I ran into an error when I tried to extend a class. There's a lot going on in this code, but the error is caused by the constructor.
Error message at compile time:
"constructor Page in class Page cannot be applied to given types;
{//Constructor
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length".
Here's the code(first my class, then the class I extended)
public class SearchEngine extends Page {
public static Color customGreen = new Color(69, 194, 33);
public static Color customYellow = new Color(232, 166, 12);
public static Color customBlue = new Color(25,97,255);
public static Color customYellowComp = new Color(178,125,0);
[code]...
View Replies
View Related
Sep 20, 2014
Basically I want to make a class called library, but I don't want to make an interface because I actually want to define the methods. I think I can only use abstract classes but not really sure how to use those. But I still have a problem, I want to create a Map that classes implementing Library class have to have in their code, and the Map will be a HashMap with <String, ParentClassHere>, so basically let's say I make a class called Car, implemeing Library to the Car class would create a Map library = new HashMap<String, Car>. Can I do something like this? And also include methods to get values and set values to the library Map?
View Replies
View Related
Jan 4, 2015
I have a working program, except that it does not calculate the credit hours and the financial aid. When I enter an input, it works, until it should show the student name, credit hours and financial aid. the error i get from the command is "Hours invalid for false student". Here is the program i think i might have the problem.
import java.text.DecimalFormat;
public abstract class Student
{
//initialise variables
String name;
int creditHrs;
[Code] .....
View Replies
View Related
May 26, 2014
I'm having a problem with SplitPane (horizontal Flow). What I'm looking for is to find a way to maintain the left pane in the same position when the main window maximizes. I do not want the left pane to grow on its width. If I go into the AnchorPain Constraint in the Scene Builder and remove the right and left constraints, when I run the application, the left pane keeps its position, but it appears a gap between the two panes (the left pane and the right one).
I need to find the way to expand the right pane to the position where the left pane is.
The link shows an image that illustrates the problem. [URL]
View Replies
View Related
Oct 18, 2014
What is the difference between extending JFrame in one class and simply constructing a new JFrame object in that same class? What benefits do I have with each solution, providing I want to use that class to create the GUI. Is it the same or are there differences rather than not having to reference to a new JFrame to be able to use its functions?
View Replies
View Related
Mar 12, 2015
1. Tried to use setLayout() but it wouldn't let me. Have imported libs
import javax.swing.*;
import java.awt.*;
but it doesn't work. It only worked when I extended a class with JFrame. Why do I have to do it? I already have a JFrame instance:
public test(){
JFrame frame = new JFrame();
frame.setTitle("Workshop");
frame.setBounds(500,300,400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
....
}
This code runs smooth, so it's confusing for me that it lets me create a JFrame object, and put buttons onto it, but it wants me to extend the class onto JFrame class in order to use a method.
2. I've read somewhere that AWT is old and should not be used anymore. Is there a diagram or a list with objects and properties that are manipulated only by Swing so that I could concentrate only on those? For example (made up list):
JPaper -
- width, setWidth(x)
- height, setHeight(x)
- position, setPosition(x,y)
- color, setBackground(RGBa)
- acquire, add(subordinate component)
- deprive, remove( subordinate comopnent)
- etc...
JSticker -
- width ...
- height ...
- write, setText(String)
- etc...
View Replies
View Related
Feb 14, 2014
I am new to java and during my classes we built a basic calculator. I want to extend my calculator and added function to square numbers.Here is my program:
Java Code:
package calculator02;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
[code]...
View Replies
View Related
Apr 26, 2014
I've been trying to pull data from another class file "Calculations.java" and have it be displayed in a TextField in "DataAnalyzerGUI.java". Here is how the hierarchy is broken down for my assignment:
DataAnalyzerGUI.java extends JFrame
ReadFiles.java extends DataAnalyzerGUI.java
Calculations.java extends ReadFiles.java
Everything displays and functions correctly in a command prompt if I use a line like this:
System.out.println ("Lowest opening " + dateArray[lowestOpenIndex] + ": " + dataArray[lowestOpenIndex][2]);
But trying to get it to display in a GUI has been quite troubling.
I know the code is supposed to look something like this:
dataOutput.setText(DESIRED CODE HERE);
But I am unable to find anything of value to work out.
I have attached my complete project....
Attached File(s)
Assignment 4.pdf (247.05K)
Assignment 4.zip (235.81K)
View Replies
View Related
May 11, 2014
I am new to java, i can develop basic java programs but am not really good at it. How should i pursue and how should i start. I know the game rules.Do i need to learn swings for the same?
View Replies
View Related
May 11, 2014
i can develop basic java programs but am not really good at it.I have been given this assignment of developing UNO game in java and have been given 10 days. How should i pursue and how should i start.I know the game rules.Do i need to learn swings for the same?
View Replies
View Related
Jan 26, 2015
I am creating a simple tiled minecraft like game in java, but i dont know how to make the game loop.Here is the source:
package net.pltformgame.main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class main extends JFrame implements KeyListener {
[code]...
I really dont know how i make the jump or the game loop.
View Replies
View Related
Jan 20, 2015
I'm building this custom project in stages, and I'm having difficulty getting the health of the player and the enemy to change. Upon compiling, I'll select weapon 1, then choose action 1 (basic attack), and the health of both the player and the enemy will stay at 15 and 10.There are no errors when compiling.
import java.util.Scanner;
public class CustomProject
{
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
it hp = 0;
int potionsleft = 0;
int choice1 = 0;
int weapon = 0;
int enemy1hp = 10;
//int newenemy1hp = 0;
[code]....
View Replies
View Related
Apr 14, 2015
I'm working on a Java matching game and I'm having a hard time wrapping my mind around it. What I'm having trouble with is how exactly to create an array so that my squares have random values. Then how do I get it to reveal and compare those values by clicking on two of them? Here is what I have so far:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
[code]....
View Replies
View Related
Apr 10, 2015
The game has two players: x and o. The players take alternate turns, with player x moving first at the beginning of each game.
Player x starts at position (1,1) while o starts at (8,8).
Each turn, a player can move like a queen in chess (in any of the eight directions) as long as her path does not cross a square already filled in or occupied. After moving, the space vacated by the player is designated as filled and cannot be moved to again. Notice that only the space that was occupied is filled, not the entire path.
The game ends when one player can no longer move, leaving the other player as the winner.
The coordinate (1 1) indicates the top left hand side of the board.
The board is specified as a list of rows. Each row is a list of entries:
- is an empty square * is a filled in square x is the current position of the x player o is the current position of the o player
The board will always be 8 by 8.
Your player function should take in the parameters as described above and return a move as a list in the format (row column) within 1 minute. If you cannot move, return (nil nil). The Tournament Referees will check for this. If you return an illegal move, the other player (and the Tournament Referees) will detect it and you will lose.
Additionally if your time expires you will lose.
View Replies
View Related
Jul 3, 2014
I am working on a project involving a class that has the attributes of one of its inner classes. Now, if possible, I would like to make it so that the inner class is not visible outside of the class. Also, some of the functional mechanics require that the class be an instance of the nested inner class (it extends the inner class). The following code snippet demonstrates the situation.
public class A extends A.B {
public static class B { //ideally I would like this to be private/protected.
}
}
When I try to compile this program, I get the error message "Cyclical inheritance involving A." This error does not make much sense because, since the inner class "B" is static, it requires no instance of "A" (it does not inherit from "A" or uses it). My question is "Is it possible to do something similar to this structure?" I have searched many forums in search of the answer but have not found anything that attempts to explain it. The closest problem that I have found is one relating to the inheritance of a nested inner class from another class. I would like to express that the problem that I am having involves a class defined within the inheriting class.
View Replies
View Related
May 10, 2014
I'm trying to create a tile based map JPanel but all I get is a white screen. I'm fairly new to the Java Swing and AWT package so I've been watching tutorials on YouTube so learn as much as I can.
I've got three classes: Window.java which includes the Main method, Panel.java which is the JPanel and Tile.java to draw all the images into an array.
Window.java:
import javax.swing.JFrame;
public class Window extends JFrame {
public Window() {
setTitle("Project");
setSize(500, 400);
[Code] .....
I've checked through everything and still cannot find what I'm doing wrong. I did try different codes but I just got errors instead.
View Replies
View Related
Dec 3, 2014
I have an battleship program that I would like to improve in performance. I know that using certain algorithms it can be improve however I am beginner in Java. I would like any recommendations and or hint in how to improve this game. The are 5 ships per game and the computers plays 10000. I just want to sink all ships in as many shots as possible.
Main class
package solution;
import battleship.BattleShip;
import java.awt.Point;
import java.util.List;
[Code] .....
View Replies
View Related
Nov 17, 2014
I have to make the connect 4 game be connect 3. Ive edited a code but I the math is over my head. These loop methods check the ways someone can win.
for (int j=0;j<7;j+=2)//need to change
{
if ((f[i][j+1] != " ")
&& (f[i][j+3] != " ")
&& (f[i][j+5] != " ")
&& (f[i][j+7] != " ")
&& ((f[i][j+1] == f[i][j+3])
&& (f[i][j+3] == f[i][j+5])
&& (f[i][j+5] == f[i][j+7])))
//end of loop
[code]....
View Replies
View Related
May 8, 2014
So I am creating a game in Java, the game works fine when I run it in Eclipse but when I export it it does not draw anything on the screen.
I added a try and catch for nullpointerexceptions but the game does not throw any nullpointerexceptions.
What is the problem?
The game seems to be finding all the images & resources since it does not throw any NullPointerExceptions...
View Replies
View Related