Drawing Rectangle Onto Screen - Window Pops Up But No Graphics

Jan 7, 2014

I run this code and a window will pop up, but no graphics :(

I tried to draw a rectangle onto the screen, but it will not show up. I was told to extend JComponent, and I did, but it still does not work.

Java Code:

import javax.swing.*;
import java.awt.Graphics;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;

[Code] .....

View Replies


ADVERTISEMENT

JPanel Rectangle Drawing Relative To The Full Window

Jun 16, 2014

I'm currently making a Breakout clone to learn about different ways of organizing my code and whatnot but I've run into a problem with the positioning of some pieces versus the position of the mouse.

I've created a blue rectangle on the screen that turns white when the mouse hovers over it, BUT it actually only ends up turning white about halfway down and then continues on off the block. I moved the rectangle to 0,0 to see where it draws and it looks like it is drawing to the very top left corner of the window itself but for the mouse coordinates, the top left is just where the black area is and excludes the borders of the window.

Here is the class rendering the rectangle, it extends from a class that in turn extends from the JPanel class:

public class MainMenuState extends MenuState {
JFrame gameFrame;
Graphics2D g;
// PlayButton variables
int playPosX = 0;
int playPosY = 0;
int playWidth = 100;
int playHeight = 50;

[Code] ....

A few other things I tried that I can think off the top of my head: Play around with different layouts (at times this caused the screen to shrink to a small size as well as using pack()), using setSize() setLocation() or setBorder() in the MainMenuState class which had no effect.

Someone on another forum also suggested trying to utilize component.getLocationOnScreen(): I tried two things with the getXOnScreen method, first subtracting with the location of the JPanel which of course gave me the same problem. After that I tried

MOUSE.x = event.getXOnScreen() - gameFrame.getLocationOnScreen().x;
MOUSE.y = event.getYOnScreen() - gameFrame.getLocationOnScreen().y;

but that resulted in some errors (null pointers) and the mouse was just printing out a position of 0,0

View Replies View Related

Specify How To Delete Only Certain Amount Of Objects On Screen In Graphics Window

Oct 10, 2014

I have a question about how to delete only a certain amount of objects on screen in a graphics window. I have a method that will enable me to get rid of every object of the same type off screen, for example this method:

Java Code:

public void deleteTrees() {
//clears all tree trunks and leaves
for (Iterator<GRect> it = historyT.iterator(); it.hasNext();) {
GObject gobj = it.next();
this.remove(gobj);

[Code] ....

That Will delete all trees.

Now In my main method I populate the screen with trees by doing this

Java Code:

for (int xpos = 10; xpos < getWidth() - 40; xpos += getWidth() / 8) {
drawTree(150, 200, xpos, getHeight() / 2.0 - 50);
historyT.add(trunk);
historyL.add(leave);

} mh_sh_highlight_all('java');

My question is how to specify only deleting x amount of trees instead of the whole thing. I pretty sure I need to use something like history.remove(trunk) and to iterate through the amount of trees specified, but not sure how to implement that.

View Replies View Related

Sound Won't Play When Player Dies And A Game Over Screen Pops Up

Jan 14, 2014

I am making a game with java in a program called greenfoot.My problem is that a sound won't play when the player dies and a Game Over screen pops up..

- I have saved the audio file in my sound map of the scenario
- Applied the good name of the file
- tested the sound effect and it's working

this is my code:

public GameOver()
{
setImage(new GreenfootImage("GAME OVER", 100, Color.BLACK, null));
Greenfoot.playSound("fail-trombone-03.wav");
Greenfoot.stop();
}

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

Simple Graphics - Drawing Smiley

Feb 6, 2014

Ok. So I have decided that taking a step back is better than trying to go forward blind. I am trying to come to an understanding of how java draws simple shapes, but continue to run into problems. I have now gone ahead and directly copied code from a book just to see if it will work. It does not. The code is just supposed to make a smiley face, but when I run it I get a bunch of errors. I have attached the code that I am trying to run and the error report at the bottom.

Java Code:

import javax.swing.JFrame;
public class DrawSmileyTest
{
public static void main( String[] args );
DrawSmiley panel = new DrawSmiley();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add( panel );

[Code] ....

9 errors found:

File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 10]

Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:10: <identifier> expected

[Code] .....

View Replies View Related

Drawing Simple Yellow Rectangle In White Background?

Feb 1, 2014

Here is the code, I just wanted to draw a simple yellow rectangle in white background...

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class NewEmpty extends Jframe {
Rectangle test = new Rectangle(100,100,100,100);
public NewEmpty()

[Code] .....

Errors in the compiler ( i think there's something wrong with that Jframe)

init:
deps-jar:
Compiling 1 source file to C:Users****Gametestbuildclasses
C:UsersMarcoGametestsrcgametestNewEmpty.java :6: error: cannot find symbol
public class NewEmpty extends Jframe {

[Code] .....

5 errors

BUILD FAILED (total time: 0 seconds)

View Replies View Related

Drawing Image On To Screen - Nothing Appears?

Feb 7, 2014

I am trying to draw an image unto the screen, however, nothing appears...

Java Code:

private ImageIcon i = new ImageIcon("image.png");
private Image image;
...
image = i.getImage();
...
g.drawImage(image, (int)(x - r), (int)(y - r), null); mh_sh_highlight_all('java');

Am I doing it right? The program gives me no error, it's just that when I try to draw it, I see nothing.

View Replies View Related

Window Is Popping Up But Rectangle Is Not Appearing

Jan 6, 2014

Here is the code:

the window opens, no errors, but the rectangle does not appear

Java Code:

import javax.swing.*;
import java.awt.Graphics;
public class myGame {
public int x = 20;
public int y = 20;
myGame(){

[Code] ....

I am using a text editor

View Replies View Related

Drawing Polygon On Screen - Collections As Arguments

Aug 29, 2014

I have a method that draws a polygon on the screen:

public void poly(List<Point> points) {
//code
}

For the usage of the method, it makes no difference whether points is an array or a List. In fact, switching doesn't even change any of the code in the method since I use a for-each loop. So, my question is, is it a better practice to use a List as an argument, or an array when the method itself doesn't care about which to use?

View Replies View Related

ACM Graphics Program - How To Set Location Of Entire Window

Jun 15, 2014

I have one quick question. If you are extending the acm graphics program, how can you set the location of the entire window? All the set location methods within the graphics program only seem to set the location of graphical objects. I know with JFrames I can use the setLocationRelativeTo(null) to position the JFrame in the center. How to set the location of a graphics window.

View Replies View Related

How To Put Console On JFrame Or Graphics Program Window

Sep 8, 2014

how to put a console on a JFrame or on graphics window. I thought about simply using a JtextField however I don't have access to the readline command since it appears to me that it is only found in a Console Program. So basically I was wondering how to put up a console on JFrame so I can issue commands on it and watch graphical stuff happen on the same screen(minus where the console box is of course).

View Replies View Related

Swing/AWT/SWT :: Dragging Window Downwards - Screen Alignment?

Oct 29, 2014

I am trying to drag the window down wards and screen has alignment issues. I tried to use WindowListeners, Component Listeners, MouseListeners ect…

But still this problem persists as only I release the mouse after drag down it appears fine that we have handled ComponentResized() method. So how to restrict component size while keep dragging down?

View Replies View Related

Family Tree - Cannot Enter Anything When Text Box Pops Up And Ask To Start With Root Name

Apr 13, 2014

The problem I'm having is when the text box pops up and ask to start with a root name, I cannot enter anything and I am not sure what I have done wrong.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class FamilyTreeDemo extends JFrame

[Code] ....

View Replies View Related

Cardlayout Format - Change Main Menu Screen Into Game Screen On Button Click

Mar 16, 2015

I'm making a game of checkers for my A2 Computing coursework which is due in within a week. I have completely finished the game, and only thing I have left to do is connect the two JPanels together via a CardLayout that I have made. However I am unsure how to do so

Here is the code from my CardLayout:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;

[Code] ....

I have kept the code I am displaying to a minimal, hence I have removed all the action listeners for my buttons, anyway the problem I have is that, I would like it so that when the user clicks on the 'Multiplayer' button which is the array button ourButtons[1], it will then transition into my main game screen so that the user can then play a game of checkers.

Here is the main important GUI from my CheckerBoard class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  public class CheckerBoard extends JPanel implements ActionListener, MouseListener {
// Main routine that opens an Applet that shows a CheckerBoard
public static void main(String[] args) {
new CLayout();
 
[Code] ....

Once again kept to a minimal.

View Replies View Related

JavaFX 2.0 :: Manipulating Several Windows (Window Inside Window)

Apr 6, 2015

I am new to javafx I start using it instead of swing i used the scene builder for my forms the problem i faced i don't know how to have main screen with menu bar at top and depending the select from the menu it will open other windows and those window must be inside my window just like for example netbeans.

I don't need to open new window in separate i need all windows inside my main window and controlling over them minimize maximize perhaps.

View Replies View Related

Rectangle Keeps Getting Faster?

Apr 12, 2014

obstacleX = (int) (obstacleX - 0.1);
if(obstacleX <= 0){
obstacleX = 600;
}
repaint();

Thats my code, and the rectangle (ObstacleX is the X cordinate for the rectangle) goes fine on the first few times across the screen, then starts to go hyperspeed....

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

Bounding Of A Rectangle

Sep 17, 2014

I have to do the following: A bounding rectangle is the minimum rectangle that encloses a set of points in a two-dimensional plane. Write a method that returns a bounding rectangle for a set of points in a two-dimensional plane, as follows:

public static MyRectangle2D getRectangle(double[][] points)

The Rectangle2D class is defined in Programming Exercise 10.13. Write a test program that prompts the user to enter five points and displays the bounding rectangle's center, width, and height. Here is a sample run:

Enter five points: 1.0 2.5 3 4 5 6 7 8 9 10.The bounding rectangle's center (5.0, 6.25), width 8.0, height 7.5

This is my code so far, taking in account that Rectangle2D is already done in a previous problem.the thing is that i don't know if i have to erase public static void or do i leave it or how do i start it?

package theboundingrectangle;
import java.util.Scanner;
public static MyRectangle2D getRectangle(double[][] points)
public class TheBoundingRectangle {
public static void main(String[] args) {
// TODO code application logic here
}
}

View Replies View Related

How To Get Rectangle From JScrollPan

Apr 21, 2015

in order to speed up my application I want 'paintComponent(Graphics g)' to only write the visible area in my viewport. I can not find how to get that Rectangle from my JScrollPane.

View Replies View Related

Rectangle Won't Display In JFrame

Jan 30, 2014

So I've been getting back into Java and downloaded eclipse back onto my laptop. However when I go to make a simple rectangle into a JFrame, the frame will pop up but no rectangle will be shown. Here is my main class, which sets up the JFrame...

mport javax.swing.JFrame;
public class Frame{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("I hope this fucking works");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

[code]....

View Replies View Related

Swing/AWT/SWT :: Add A Rectangle To JFrame?

Apr 13, 2014

Is there a way to simply slap a rectangle into a JPanel (make it appear) with out creating an inner Class or helper Method, all within "Main"? And if not, why?

Making a JFrame is easy.

Adding a JPanel is a snap.

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TheJFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub

[code]....

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

Rectangle Isn't Showing Up In JFrame

Nov 14, 2014

MyGraphics worked before I added a background but, even now when I take the background away it isn't showing up.

package com.snow.game;
import javax.swing.*;
import java.awt.*;
class MyGraphics extends JComponent { //creating a class for graphics
public void draw(Graphics g){
//calling Graphics making a new graphics (g) now you can use it to make objects
g.drawRect(10, 10, 50, 50); //Draws a rectangle

[Code] ......

View Replies View Related







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