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);
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
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
I have been trying to make a game lately, but I can't seem to work out how to get an image to display on my JFrame. I have tried everything, copied different codes of the internet, but nothing works. Here is my code of the GUI:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUI extends JFrame implements Runnable, ActionListener { JButton start = new JButton("Start nieuw spel"); JButton instellingen = new JButton("Instellingen"); public GUI() {
I have a program in which I am prompting users for integer values to display in a JFrame. I call the method below to load an array with their input:
Java Code:
public String inputAssembly(){ if (!jtfInput.getText().matches("d")){ JOptionPane.showMessageDialog(null, "Input must be of integer value."); } if (jrbFar.isSelected()){ return jtfInput.getText() + jrbFar.getText();
[Code] ....
Regardless of the input, both messages display (invalid input / got it). I've tried debugging so I know that the values are getting entered and read correct, at least to my knowledge. It is a very simple regular expression, only checking to be sure an integer was entered.
I had the opengl display attach to the JFrame before i did the runnable. But after adding the runnable the display now shows up the same size as my screen size. I have tried rearranging the
canvas.setSize(); and the frame.setSize();
but nothing changes the opengl display is still the same size and when i try to close the JFrame first rather then close the display first i get this error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: From thread Thread[AWT-EventQueue-0,6,main]: Thread[main,5,main] already has the context current
which points me to my
Display.destroy();
which im guessing is telling me that i am not properly disposing the display? How to attach the opengl display to the JFrame and fix the error above?
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....
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 } }
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.
I was tasked with building a program that, when is given a string by the user, takes it and prints it out as a rectangle. For example, if the user types in "COMPUTER", the output would be:
So, it works once, but then it doesn't work again. Here is my code:
i was tasked with building a program that, when is given a string by the user, takes it and prints it out as a rectangle. For example, if the user types in "COMPUTER", the output would be:
So, it works once, but then it doesn't work again. Here is my code:
import java.util.Scanner; public class WordRectangle { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner userInput = new Scanner (System.in);
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
I need to do a rectangle using boolean array where true elements are the borders of the rectangle, and false elements are the inner space. I imagine that the first and last rows and columns must give the true element,
public class Functionality { public static boolean[][] rectangleArray(int n, int m){ boolean[ ][ ] matrix = new boolean[n][m]; for(int i = 0; i <matrix.length; i++){ for(int j = 0; j < matrix[i].length; j++){
Java Code: public class PrintHoledRectangle { public static void main(String[] args) { int width = Integer.parseInt(args[0]) / 2 * 2 + 1; int height = Integer.parseInt(args[1]) / 2 * 2 + 1; int centre = width * height / 2 + 1; int index = 0;
[Code] ....
The width of the rectangle to be printed is given by the first argument, the height by the second
The centre tile ("[_]") of the rectangle (to be printed as " ") is given by the formula: ((width*height)/2)+1, assuming you count each tile left-to-right, top-to-bottom
My thinking is that the programme keeps a running count (seen as 'index') so that, when the centre tile is to be printed, it outputs " ", otherwise "[_]", hence the if-else statement
I am just trying to move a rectangle in a diagonal line. When the applet pops up, the rectangle is there but doesn't move. If I drag the sides of the applet to make it bigger or smaller the rectangle will redraw itself and move, but I want it to move without touching the window.
I have created a method Rectangle and added a loop in my class Resizer (MouseAdapter) but impossible to resize the rectangles of the arraylist independantly !
class Rectangle extends Rectangle2D.Float{ private String name; public Rectangle(float x, float y, float width, float height, String name) { setRect(x, y, width, height); this.name = name;
I was told to design a class named Rectangle to represent a rectangle.The class contains:
■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the area of this rectangle. ■ A method named getPerimeter() that returns the perimeter.
Draw the UML diagram for the class and then implement the class. Write a test program that creates two Rectangle objects one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area,and perimeter of each rectangle in this order.Here is my code for the Rectangle Class:
class Rectangle { double width; double height; public Rectangle() { width = 1; height = 1;
[code]....
The error that I am given when I compile the driver is as follows:constructor Rectangle in class Rectangle cannot be applied to given types; required: no arguments; found:int,int; reason: actual and formal argument lists differ in length.
I have run into a bit of a head scratcher, at least to me. I am building multiple rectangles using double precision. I had to switch from int to double due to another issue that requires decimal places. Now, my fillRect (last line in the code section I posted) is causing an error as it only wants to work with int.
public void draw(Graphics2D g2) { // check that sizes are above 0 if ((rectWidth <= 0) || (rectHeight <= 0))
I'm trying to build a program which sets a fill color for a rectangle. Each time the mouse is clicked the fill color changes according to the position of the mouse's X and Y. I succeeded to create a JFrame with a changing background according to the Mouse's position, but I can't seem to change the color of the rectangle (defined in MyPanel class).
I want to write a method which would give us a rectangle with numbers. The input is an integer which represents the length of a line in rectangle and the result type is void. The example for n=4 is: