Passing A Graphics Object To 3 Other Classes?

Oct 5, 2014

Basically I am making a paddleball game, like i'm sure everyone does in learning Java. I'm supposed to use different classes for each component, i.e. one for the ball, one for the paddle, and one for the display, then finally one as a 'controller' to implement mouselistener and stuff.

However, I can't quite grasp how to implement the paintComponent method. I know I can only have it in one class extended from JPanel, and I have the syntax for creating an object which I understand is something like this:

public void paintComponent(Graphics g){ //this is the rectangle my game will be played on,
super.paintComponent(g); //a gray background to define boundaries for the ball
g.setColor(Color.GRAY);
g.drawRect(0, 0, Frame.getHeight(), Frame.getWidth());

However what I don't understand is, how do I then pass this graphics object to the ball and paddle to let them draw themselves? I found something that described it like this here

class GamePanel extends JPanel {
Entity e=new Entity;
@Override
protected paintComponent(Graphics g) {
super.paintComponent(g);

[Code] .....

What I don't get is, if I use this, where would I put the drawRect and stuff to make the other shapes I need? in their class, under the entity.Draw(g) method? or in the display class where it calls the graphic object in the first place?

Last, how can I have my controller class refresh the displays of each of these with the timer I have implemented? Is there a simple way to call one refresh command and have it refresh the drawing of both the paddle and ball simultaneously, or would I need to call a separate refresh command for each object?

View Replies


ADVERTISEMENT

Thread And Passing Exception With Two Classes

Jan 23, 2014

I have two classes, where main class is simple with only main function in it. Another class extends Thread and there's couple of functions I want to execute from main class.

Problem: I try to get other.shut() to be run in main class catch() block, after I have stopped the other class's running thread (e.g. by ctrl+c).

I think I need to somehow "pass" the exception from other class to main class so it goes to the catch block?

Code for main class:

Java Code:

public class MainClass {
public static void main(String[] arguments) {
OtherClass other = new OtherClass();
try {
other.exec();
} catch (Exception e) {
other.shut();

[Code] .....

View Replies View Related

Passing Values To Two Classes And Retrieving Values From Those Classes

Feb 14, 2015

I'm doing an aggregation exercise that's suppose to find the volume and surface area of a cylinder. What I'm trying to do is pass values from one class, to a second class, and that second class passes values to a third class.

This may be a clearer explanation: The first class is the main program which sends values to the second and third class. The second class is used do calculations for a circle (a pre-existing class from another assignment). The third class grabs the values that the second class calculated and calculates those values with the one that was passed from the first class to the third class. The first class then prints the outcome.

Problem is when the program gets to the third class, it just calculates the value from the first class with the default constructor from the second class. It's like the second class never received the values from the first class. I think I'm missing a step, but I don't what it is.

First Class:

package circle;
import java.util.Scanner;
public class CylinderInput
{
static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
//user defined variable

[Code]...

View Replies View Related

What Does A Graphics Object Actually Represents

Aug 13, 2014

I used to think that it is simplly a tool to use to change colors and draw to specific container(ie JFrame, JPanel). However, I've been studying buffering (triple, double, flipping...etc) and how it works for 3 days now, and my confusion has only increased. for instance, why when we need to draw to the buffer(ie BufferStrategy, BufferedImage) we get its own graphics object to draw to it and then we project it to the screen? does the Graphics Object represent the drawing surface (ie the JPanel it self if we're using one to draw custom painting via JPanel#paintComponent(Graphics g)) ? so generally speaking if we are using a JPanel and we say bufferedImage#createGraphics and use that graphics object to draw to, we would not be drawing to the JPanel but to the BufferedImage correct?

View Replies View Related

What Does A Graphics Object Actually Represents

Aug 13, 2014

what is A Graphics object for a while now, I used to think that it is simply a tool to use to change colors and draw to specific container(ie JFrame, JPanel). However, I've been studying buffering(triple, double, flipping...etc) and how it works for 3 days now, and my confusion has only increased. for instance, why when we need to draw to the buffer(ie BufferStrategy, BufferedImage) we get its own graphics object to draw to it and then we project it to the screen? does the Graphics Object represent the drawing surface (ie the JPanel it self if we're using one to draw custom painting via JPanel# paint Component(Graphics g) and so when we're getting the graphics object of a buffer, do we actually get its drawing surface to paint on? so generally speaking if we if we are using a JPanel and we say BufferedImage#createGraphics and use that graphics object to draw to, we would not be drawing to the JPanel but to the BufferedImage correct?

View Replies View Related

How To Add Image On To Graphics Object / JFrame

Apr 8, 2014

I use Canvas, Graphics & JFrame, but not JPanel, how do I add an Image? To see my code: [Java] package pack.script.game; import java.awt. BorderLayout; import java.awt.Canv - Pastebin.com

What do I need to add, or where can I find information on how to add an image? Can I add an image using Graphics? I will import any utils, swings or awts needed. But I don't want to change my code too much.

View Replies View Related

Swing/AWT/SWT :: What Does Graphics Object Actually Represents

Aug 13, 2014

I'm not even sure if that is the right question to ask. I've been confused by what is A Graphics object for a while now, I used to think that it is simply a tool to use to change colors and draw to specific container(ie JFrame, JPanel). However, I've been studying buffering(triple, double, flipping...etc) and how it works for 3 days now, and my confusion has only increased. for instance, why when we need to draw to the buffer(ie BufferStrategy, BufferedImage) we get its own graphics object to draw to it and then we project it to the screen?

Does the Graphics Object represent the drawing surface (ie the JPanel it self if we're using one to draw custom painting via JPanel#paintComponent(Graphics g)) ? and so when we're getting the graphics object of a buffer, do we actually get its drawing surface to paint on?

So generally speaking if we are using a JPanel and we say bufferedImage#createGraphics and use that graphics object to draw to, we would not be drawing to the JPanel but to the BufferedImage correct?

View Replies View Related

Graphics In Game - How To Draw Object Or Get It Into The Frame

Apr 23, 2014

I am trying to create this snake and input it into the next window that is opened when the play button is pushed but how to draw this snake or get him into the frame and i have looked all over google.

import javax.swing.*;
import javax.swing.Box.Filler;
import java.awt.*;
import java.awt.event.*;
public class SnakeObject extends JFrame
{
public SnakeObject()

[Code] ....

View Replies View Related

How To Draw Multiple Graphics Inside One JPanel Using Multiple Classes

May 5, 2015

I'm very new to Java, and I am creating a program that takes multiple user input to create one face. I have a class for the eyes, nose, lips, and headshape. For some reason, my program is not drawing the graphics. ***for question purposes, I have only included my head shape class and my test class****

my "test" class:

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FaceTest
{
public static void main(String[] args)
{
String head = JOptionPane.showInputDialog("Would you like a circle, square, rectangle shaped head?: ");

[Code] ....

View Replies View Related

Passing Object As Parameter?

Aug 8, 2014

i want to pass an object of type Software to assign it to a computer from Computer class...

i should note that computer and software are arrays of objects thats the variable and method to set software in Computer class

private Software[] software;
public void setSoftware(Software software,int index){
this.software[index]=software;}

note: the user choses the a computer from a list and a software as will

for example the program will show the user 2 computers

0 for computer: apple, Model: mac mini, Speed: 2.8

1 for computer: sony, Model: vaio, Speed: 2.2

the user enters the index he wants then the program will show a list of software to add to the computer selected

the error I'm having is run time error Exception in thread "main" java.lang.NullPointerException and it points to these 2 lines

1.comp[Cch].setSoftware(software,Sch);

2. the method setSoftware

every thing is running correctly but this step above

Cch= the chosen computer index

Sch= the chosen Software index

why am i getting an error and how to fix it?

View Replies View Related

Passing Object Created In One Method To Another?

Oct 29, 2014

I am working on an independent project it is a simple little text based rpg that will run in a counsel window. I have an object for Character that is creating during a CreateCharacter method. I want the play to be able to enter a character that will open up a menu that displays things like the name and health and stuff of the character from the object created in CreateCharacter, but because I have it in a different class I don't know how to reference the object made in CreateCharacter.

I have it in 6 files

Character --- Object with getters/setters for things like name, age, race, class, ect
MainMenu --- Displays title and promts for new game and quit
CreateCharacter --- Walks through and sets all values in Character
Stats --- Keeps the players stats (health, attack, ect) in an array
Intro --- Beginning demo thing (not really important for this question)
Menu --- Displays all current user stats (Having issues with this one)

Example I have this in Menu

System.out.println("Name: " + ????.getName());

View Replies View Related

Passing Object As Parameter - Combination Values Are All Zero

Mar 8, 2015

I have an object that has an instance of another object class as its parameter :

CombinationLock oneHundred = new CombinationLock(28,17,39);
Locker Mickey = new Locker(100, "Mickey", 3, oneHundred);

This is for a locker, which has a combination assigned to the student. Within the locker class I have the following constructor:

public Locker(int locker, String student, int numberOfBooks, CombinationLock combo) {
this.locker = locker;
this.combo = combo;
this.student = student;
this.numberOfBooks = numberOfBooks;
}

combo is the private CombinationLocker object I created within the Locker class. Do I need to pass the combo object on to the CombinationLock class? For reason, I do not comprehend, the combination password from the main class is not passing through to the CombinationLock class, and the combination values are all zero.

View Replies View Related

JSF :: Passing Parameters From Normal List Object Without Using DataModel

Oct 9, 2014

How to use the id parameter in my documents entity to download documents from a list of documents. Normally I use ListDataModel and the getRowData method. I would like to know how to achieve the same thing using an ordinary List object.

My list of documents is called List<CountryDocs> selectedDocs;

<h:form>
<p:dataTable value="#{countryDocBean.selectedDocs}" var="docs">
<p:commandLink id="download" value="Download" ajax="false">
<p:fileDownload value="#{countryDocBean.downloadedFile}"
contentDisposition="attachment"/>

[Code] ....

Clicking on the download link calls the following method in my managed bean:

@ManagedBean(name = "countryDocBean")
@SessionScoped
public class CountryDocBean {
private List<CountryDocs> selectedDocs;
public StreamedContent getDownloadedFile() {

[Code] ....

Debugging shows the value for the id is 0 and this results in a NullPointerException. I've tried several methods for grabbing the document id in my backing bean, but no luck yet. I also read about the the ViewParams and ViewAction method but they caused validation errors to do with the <f:metadata> tags. I don't know how to obtain this value using a normal List object.

View Replies View Related

Passing Object From JSP Page Through JSTL Into Controller Method

May 12, 2015

I need to pass advanced object into controller from JSP page, but I always get null result.It's a controller method:

Java Code:

@RequestMapping(value="admin-user-edit", method=RequestMethod.POST)
public ModelAndView editUser(@ModelAttribute(value="user") UsersEntity user)
{
if (null == user)System.out.println("User is null");
else
System.out.println("User name = " + user.getName() + " | Users id = " + user.getId());
ModelAndView view = new ModelAndView();
return view;
} mh_sh_highlight_all('java');

And this is a JSP page snippet. I need to choose some user from user list and pass it to controller.

XML Code:

<c:forEach var="user" items="${user_list}">
<tr>
<td><c:out value="${user.id}" /></td>
<td><c:out value="${user.name}" /></td>
<td><c:out value="${user.login}" /></td>
<td><c:out value="${user.status}" /></td>

[code]...

I tried to pass it through HttpServletRequest argument but all the same.

View Replies View Related

Classes Of Object - Implement Two Out Of Three Interfaces

May 8, 2014

I have three classes of object, most of which must implement two out of three interfaces. The interfaces look like this:

public interface Source {
public void startSending();
} public interface Sender {
public void setReceiver();

[Code] .....

That works fine, but I am wondering if pairing the interfaces into subinterfaces is a defensible methodology. For example, all classes that act like Producer must implement both the Source and Sender interfaces. And all classes that act like Relayer must implement the Sender and BlackHole interfaces. I could define two subinterfaces like this:

public interface Factory extends Source, Sender {
}
public interface Modifier extends BlackHole, Sender {
}

I could then define my classes like this:

public class Producer implements Factory {
}
public class Relayer implements Modifier {
}
public class Consumer implements BlackHole {
}

Within the class definitions, it makes no difference, as I will have to implement the same methods either way. But it seems more self-documentary to create the subinterfaces from their parent interfaces and name them in ways that reflect what the classes that implement them must actually do.

View Replies View Related

Passing Parameter From Object Of Class B To Object Of Class C By Use Of Class A?

Dec 13, 2014

Assuming that we have two classes B and C which inherit from class A. What is the best way to pass a parameter from an object of class B to an object of class C by the use of class A without using static variable and without defining a get function in B?

View Replies View Related

Passing Object Argument To A Method - Unable To Call Methods On Argument

Feb 7, 2015

I am trying to pass an object of type Product p to my editProduct method, however trying to call p.getName(); doesn't work and throws a NullPointerException. The same kind of thing works for my displayRecord method (in a different class) and I can call .getName() on Product p, also passed as an argument to that method. Below is my editProduct class. The NullPointerExcepion is being thrown at line 61 (i.e., nameField.setText(p.getName());).

I don't know if I explained right, so here's a line thing of how the classes relate:

Search >>>(field.getText())>>> displayRecord(Product p) >>>editProduct(p)>>> EditProduct

And as a side note: adding the line p = new Product(); fixes it and successfully runs the class (including the Save and Quit parts) but obviously I want it to specifically refer to the Product I pass to the method.

Java Code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;

[Code] ....

I'm asking a question because I don't understand how Product p could possibly be null, because the argument is passed through my DisplayRecord class, which also takes a Product p argument and works. In that class, I have declared Product prod = p; and prod is what I am passing to editProduct.

View Replies View Related

Which Method Is Used While Passing Or Returning A Object From The Native Method

Mar 5, 2014

Which method is used while passing or returning a java object from the native method?

View Replies View Related

How To Call Classes Within Other Classes

Apr 18, 2014

How do you call classes within other classes? Or can you only call classes through the main?

View Replies View Related

How To Build Graphics

Jan 5, 2014

He said that if you need something that Swing can't provide, like a bar graph, you build it. Now, being used to just writing a method that will open up a JFrame, how you would actually build graphics on your own. How in the world would that work? How would one write their own methods to make a window or to build a graph?

View Replies View Related

Graphics Are Flickering

Feb 2, 2014

I attempted to make my square move in the screen and i set up collision with another object, however the graphics are flickering, really flickering, here's the code:

Java Code:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class NewEmpty extends JFrame {
double p1speed =5, p2speed =5;

[code]....

View Replies View Related

How To Easily Use Graphics

Dec 31, 2014

I am attempting to create a n-body simulation using orbital dynamics and Java in school.

I am only in high-school, but I have some programming experience. The logic of this program comes quite easily. My problem is the graphical part, so I was just looking for quick methods to implement graphics.

What I am looking to do, is display objects onto the screen and manipulate their respective x and y coordinates as time pass. I have tried to use BufferedImages and an array of every pixel on the screen, but it quickly becomes extremely hard to draw circles etc. I have also tried the Graphics class and using g.fillOval() etc, but this can also get tedious.

Are there any more efficient methods, libraries etc? And also, how should I use the time variable, a variable that updates everytime the program updates?

View Replies View Related

Adding Listener To Graphics

Nov 24, 2014

I wanted to create a interface with buttons ofshapes and type of transformation where user first select a shape, the shape will appear and user will have to click on the buttons on resize, reflect, rotate or skew to transform to shape. How can i do the coding? such as adding listeners to the shapes?

View Replies View Related

Saving Graphics 2D As Image

Jul 30, 2014

What I am trying to do is save the the content drawn to my screen as an image. The following code is my render method and although I know how to use it, I don't fully understand the classes and how they work with is making this difficult.

public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;

[code]....

View Replies View Related

Graphics - Drawing A Building

May 28, 2014

I have to write a program for class that basically uses Paint Component to draw a bunch of rectangles to look like a building then have them change color randomly. I am stuck I can't figure out how to make it draw the rectangles in rows and columns to look like the building i can make it display multiple squares randomly however but thats not the assignment.. here is my code

package labBuilding;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Building extends JPanel {

[code]....

View Replies View Related

Graphics Rotation In Swing

Feb 3, 2014

How to rotate a shape in Swing? The only thing I found was something involving this, but I have not seen something like this before:

Java Code : Graphics2D g2d = (Graphics2D)g; mh_sh_highlight_all('java');

I see this is an object reference, but what is on the right side? Does not seem like a method, as it would not equal a method nor would the parentheses be on the left. Why are parentheses there? Disregarding the above code, I would like to know how to rotate without Graphics 2d

Also, with G2D you it will not allow for setting x coords

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved