Draw Rectangle If Key Pressed - Move Elements

Apr 7, 2014

I have a class that implements JComponent and draw a rectangle and if a key pressed(for example UP) I want to draw it up, and then when down key pressed to draw it down... But I want not only to disappear, but somehow to move, so the user can see the rectangle moving...

View Replies


ADVERTISEMENT

Draw 3 Rectangles In Different Spots / Move Elements In Order So They Do Not Cross

Apr 9, 2014

I made a small aplication which draws 3 rectangles in different spots(0,0 , 50,50 , 100,100)

Am I used an KeyListener + ActionListener, to make them move in the let's call it box.

How can I do in order that those 3 elements do not cross, so you can always see them, they don't collapse into each other?

Java Code:

package matrixmoveelements;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

[Code] ......

View Replies View Related

Draw Rectangle Through ArrayList

Oct 30, 2014

So I also am working on this problem. My frame does print each new rectangle; however, it is printed each time from the top left corner, and the bottom right is where I click.

Here is my RectangleComponent:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.ArrayList;
import javax.swing.JComponent;
public class RectangleComponent extends JComponent

[Code] ....

View Replies View Related

Using Jslider To Draw Rectangle In JTextField In Different Colors?

May 15, 2014

[color]
public class MyColorChooser extends JPanel implements ChangeListener{
private JSlider redSlider;
private JSlider greenSlider;
private JSlider blueSlider;
private JTextField txt1;
private JTextField txt2;
private JTextField txt3;
private final Color[] colorValues =

[code]....

Do i have to make a new no argument constructor for my sliders?

View Replies View Related

Declare Array Of 50 Elements Of Type Double - Print Output 10 Elements Per Line

Feb 5, 2015

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class array
{
public static void main(String[] args)

[Code] ...

Is there a way to write this, where, alpha is one array.

Write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed.

If I have an array of 50 integers, can I break that to read in lines of 10?

View Replies View Related

Add Elements To 2D Array Without Losing Elements

Jan 12, 2015

I am trying to make a 2d array that keeps track of comparison counts. what I have so far works ok but writes over the previous elements with 0. can't seem to find where I am re-initializing the previous elements.

//this is one of my fill sort arrays

public void fillSelectionArray(int index, long countSum) {
//rand = new Random( );
//for ( int i = 0; i < listsize; i++) {
selectionList[ index -1] = countSum;
// }

[Code] ....

I know I am missing something just not sure what.

View Replies View Related

How To Change Image When A Key Is Pressed

Aug 21, 2014

I am very much new to Java. I have just started learning event handling. Currently I am learning about Key Events. And am learning by doing. Well that is how one could learn anything in fact. I want to write a program in which the currently loaded image in the panel of a frame changes upon a key press say "Enter" or any other key.

View Replies View Related

How To Set Color When Space Is Pressed

Apr 11, 2014

The title says it all and i'm a fairly new java programmer but here is the code:

package TestVersion;
import java.awt.Color;
import java.awt.Graphics;

[Code]......

I think i need a variable or something oh and here is the 2 other classes, the keylistener one:

package TestVersion;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

[Code].....

View Replies View Related

JCombobox Can't Change Value Once Pressed

Apr 17, 2015

I have this swing code here:

//dark elixir
Panel panel_4 = new Panel();
config.add(panel_4);
JLabel del = new JLabel("Starting DE: ");
panel_4.add(del);

[code]...

It all displays well etc but if I change the value of one of the fields then I'm not allowed to change the JCombox field anymore.

View Replies View Related

How To Get JList To Update When Refresh Button Pressed

Mar 15, 2015

I am able to update the array holding the items but I don't know how to refresh the JList to include all the new items in the array.

Code for GUI:

import java.awt.ScrollPane;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ListView extends JFrame{
HobbyList stuff = new HobbyList();

[Code] .....

View Replies View Related

Change Text In JLabel When Button Pressed

Dec 4, 2014

Ok, so I am trying to learn about JFrames and JPanels. I have figured out buttons, layouts, etc. What im having trouble with is accessing the label object from the ButtonHandler class since it is static and I need my JLabel to be non-static so that it can change. Basically, I want JLabel to change to a pre-determined text string once a button is pressed. Here's what I have so far

package makewindow;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

[code]....

View Replies View Related

JavaFX 2.0 :: Keep Last Button Pressed With Custom Style

Dec 27, 2014

I have a VBox with 20 buttons and I have this style applied (menu.css) (only for VBox wrapper):
 
.button:focused {
    -fx-background-color: #0768A9;
    -fx-text-fill: #FFFFFF;
}

But when I pressed other button outside the VBox the style disappears, because the other button get the focus.

How I can do to keep the last button pressed with a custom style? (Only for buttons containing the VBox).

View Replies View Related

Java Scrolling Game - Moving Image When Key Is Pressed

Mar 19, 2015

Okay, gotta be able to move the image when a key is pressed. When I test it, the image moves, but leaves the old image behind.

package Game;
import java.awt.Component;
import javax.swing.JOptionPane;
 public class Game
{
private Grid grid; //holds the grid used to store and display images
private int userRow; //keep track of which row the user-controlled image appears in
private int msElapsed; //keep track of the total amount of milliseconds that have elapsed since the start of the game

[Code] ....

Here is what I was given on instructions:

Complete the handleKeyPress method. Write the following line of code in the handleKeyPress method to check for a key press:

int key = grid.checkLastKeyPressed();

If the user pressed the up arrow (key == 38), move the user's image up one row (unless the user is already in the top row). Likewise, if the user pressed the down arrow (key == 40), move the user's image down one row (unless the user is already in the bottom row). Either way, make sure to update userRow.

Movement of the image can be accomplished by changing the value of userRow. Study how the initial image is displayed in the game constructor, then

Set the current image location to null

Update userRow to the new location and,

Set the image in the new value of userRow.

You should be able to move the user image up and down, and you should be prevented from moving the image off the screen.

View Replies View Related

Game Logic - Run Something In Main Method When JButton Is Pressed

Apr 11, 2014

I want to run a few things in my main when a JButton is pressed but can't work out the best way to do it. Here's the code in the main.

public static void main(String[] args) throws FileNotFoundException, ParseException, InterruptedException{
Map m = new Map("defaultMap");
GameLogic g = new GameLogic("defaultMap");
GodsEyeView gev = new GodsEyeView(g, m);
gev.initialRun();

//Want to run this when a button is pressed.
HumanUser h = new HumanUser(g);
playerGUI gui = new playerGUI(h);
gui.run();
gev.run();
}

View Replies View Related

Center Is Determined By Location Of Point Where Mouse Is Pressed

May 6, 2014

The goal is to desplays circles (max of 30) of fixed radius = 20, with the rightmost circle drawn in blue and left most circle in red and the rest are in white. The center is determined by the location of the point where the mouse is pressed. the number of circles is equivalent to number of mouse has been pressed_ the circles will be drawn only when you push the "draw" button.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JPanel;
public class CirclePanel extends JPanel {
private final int WIDTH = 500, HEIGHT = 300;
private final int RDIUS = 20;
private final int MAX = 30;

[code]...

View Replies View Related

Code That Add 1 To A Variable And Store It Anytime Key P Is Pressed On The Keyboard

May 7, 2015

a code that will add 1 to a variable and store it anytime key "p" is pressed on the keyboardKeep in mind

Here's what i have....

import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner kin = new Scanner (System.in);
int pa, ma;

[Code]...

View Replies View Related

Switch Loop - Displaying Characters Entered Until Q Is Pressed

Jul 26, 2014

I cant apply my knowledge of the switch to modify this program.

What i need is that the program runs the same but i can press q to stop the program and it will continue in a loop otherwise. repeating the same process i.e displaying the characters entered until q is pressed.

How do i do it? I think i need to make use of do and while but how to apply those in this case.

View Replies View Related

Classing White Space As Invalid Name When Close Button Is Pressed

Oct 29, 2014

I have a filechooser that works how it should and if anyone enters anything with a dot that isnt .xml it shows an invalid file name message. However when I dont choose a file and press cancel it still says that because when my boolean hits false it's the first thing it hits in that section of code.

if(!writeSuccess)
{
//display output messages in JOptionPane
JOptionPane.showMessageDialog(null,"Error, file name invalid", "Error", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(null,"Export successful", "Success", JOptionPane.INFORMATION_MESSAGE);
//close the form
me.dispose();
}

If I want it to just close down without it saying anything is there sort of if statement I could do that would prevent this? But if it is an invalid file name it will still show that message?

View Replies View Related

Applets :: Accessing External Resources - Play Particular Sound Whenever Button Is Pressed

May 4, 2014

I am quite new to developing Applets in java. I recently made an applet that will play a particular sound whenever a button is pressed. The sound files are placed in the same directory as the applet class file and i access them by using

AudioClip clip=getAudioClip(getCodeBase(),"A.wav").

The Applet runs fine in the appletviewer but whenever I try to open it using any browser like Chrome,IE,Firefox the applet loads but sound isn't played. I checked the java console and it showed

"java.security.AccessControlException: access denied ".

My questions are:

1. How do I allow the applet to get access to those files?

2. When the Applet is running from the local file system why CAN NOT I access those resources via getCodeBase() ???

3. I googled and found that I can sign a JAR and that can have full access to the computer. But how do I create a JAR from an applet (which has no Main method) while it asks me to point to the main class in the manifest?

View Replies View Related

JSF :: Move Datatable Rows Up And Down?

May 22, 2014

I have a requirement like in a Rich datatable, and i have placed two links up and down in one of the column of each row, here i wanted to swap rows (move up/ down ) the rows when i click the link on a perticular row. is it possible with jsf/ java script?

View Replies View Related

How To Move JLabel In JPanel

Jan 9, 2014

I am trying to move a JLabel in a JPanel, but can't seem to make it work. I can't provide a compiling code, but a few fragments. I have a JLabel [] array. i want to move with the mouse elements of this array. Let's say i want to move JLabel[i]. I implemented this:

Java Code:

static int labelY;
static Point labelX;
static Boolean flag = false;
jLabel[i].addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent evt){
labelY=evt.getY();

[Code]...

I used the bool flag because i can't make changes to the label when it is inside an anonymous class. I also tried using setLocation but that did not work also. I don't get any errors when compiling. But when i run it and try to drag the JLabel[i] i get a long list of errors:

Java Code:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at puzzle.hw$1$2.mouseDragged(hw.java:276)
at java.awt.Component.processMouseMotionEvent(Unknown Source)
at javax.swing.JComponent.processMouseMotionEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)

[Code]...

View Replies View Related

Trying To Make The Image Move

Oct 27, 2014

I am having trouble making my image move to a certain position in a certain amount of time which is set inside a text file. I was wondering what I'm doing wrong and what I'm missing.

import java.io.*;
import java.util.*;
import java.io.FileReader;
public class Animator {
EZImage HP;

[code]....

View Replies View Related

How To Keep Character In Place With Up And Down To Move

Jan 30, 2014

I noticed it is a huge success to have the backgrounds of games move and the character just move up and down and dodge obstacles.How Do I make a background that scrolls horizontally to the left? I know how to keep the character in place with up and down to move, but not sure how to go about making the background move with obstacles.

View Replies View Related

Character Won't Move When Key Is Held

May 29, 2014

I was just trying to figure out why when I hold down a movement key, that my character doesn't move. It only moves if I continuously press the movement key. Here is my code:

package game;
import java.awt.*;
import TileMap.TileMap;
import Handlers.Keys;
public class Player {
private double x;
private double y;

[Code] .....

View Replies View Related

How To Move Multiple Objects At Once

Oct 8, 2014

I am new to Java programming and i am using BLUEJ to create some basic objects etc.

I have 3 circle objects (individual) and i am wanting to use "slowMoveHorizontal" method to move these 3 objects about 50px to the right.

So far, everything works BUT the issue is that they do NOT move together simultaneously. They move on a ONE BY ONE basis.

View Replies View Related

Servlets :: How To Move To Another JSP Page

Mar 27, 2014

I have created a authentication servlet as follows :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub
/* Connexion la base de donnees */
try {
Class.forName("org.postgresql.Driver");
System.out.println("Driver O.K.");

[code]...

I just want to know how can i move to another jsp page from my servlet.

View Replies View Related







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