weird JFrame problems - jframe

I am trying to get a box to move in a JFrame by repainting it, but for some reason i cannot figure out it will not work. I know its probably something really stupid but here are my classes: (also sorry for the formatting its a pain in the ass...) It gives me an error on the frame.add(square) line.
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class Runner extends JFrame{
final static int FRAME_WIDTH = 1000;
final static int FRAME_HEIGHT = 600;
final static int BOX_WIDTH = 50;
final static int BOX_HEIGHT = 50;
public static void main (String[] args){
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Animation");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setVisible(true);
Component square = new Component();
Dimensions.setBoxDimensions(BOX_WIDTH, BOX_HEIGHT);
frame.add(square);
frame.setVisible(true);
for (int i = 0; i < 100; i++){
Dimensions.setPosition(i,i);
square.repaint();
frame.setVisible(true);
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class Component extends JFrame{
public void paintComponent (Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.fillRect(Dimensions.xPos, Dimensions.yPos, Dimensions.boxWidth, Dimensions.boxHeight);
}
}
public class Dimensions {
public static int boxHeight = 50;
public static int boxWidth = 50;
public static int xPos = 0;
public static int yPos = 0;
public static void setBoxDimensions(int width, int height){
boxHeight = height;
boxWidth = width;
}
public static void setPosition(int x, int y){
xPos = x;
yPos = y;
}
}

Since your component class is a JFrame you cannot add teh JFrame to a JFrame. You may want to add a JPanel to the JFrame and paint on the JPanel instead.
Or you may be intending to extend java.awt.Component instead?
import javax.swing.JPanel;
public class MyComponent extends JPanel{
public void paintComponent (Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.fillRect(Dimensions.xPos, Dimensions.yPos, Dimensions.boxWidth, Dimensions.boxHeight);
}
}
And when you want a Component you can do this:
JPanel square = new MyComponent();

It's better to override the "paint" method in your JFrame. It's much easier to do.
#override
public void paint(Graphics g){
super();
/*
put the code to draw your shape of shapes here
*/
}
You can call repaint method of your JFrame whenever you want to update your JFrame.

Related

JPanel subclass won't appear in JFrame

I have this class PokerPanel which extends JPanel, whatever I put in the JPanel subclass will not appear in the JFrame when it is set as the Content Pane. Below is the code
public class PokerPanel extends JPanel {
private int x = 1000;
private int y = 1000;
public PokerPanel(){
this.setPreferredSize(new Dimension(x, y));
this.setBackground(Color.BLACK);
}
Then in my main class
public static void main(String[] args) {
PokerPanel pp = new PokerPanel();
pp.setOpaque(true);
JFrame frame = new JFrame();
frame.setSize(pp.getX(), pp.getY());
frame.setContentPane(pp);
frame.setVisible(true);
}
This does not show the black background as intended (or anything else i put in there for that matter JLabels etc), however if I do it as shown below then it does work.
public static void main(String[] args) {
PokerPanel pp = new PokerPanel(rg);
pp.setOpaque(true);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
JFrame frame = new JFrame();
frame.setSize(pp.getX(), pp.getY());
frame.setContentPane(panel);
frame.setVisible(true);
}
I can't seem to figure out why it would not show the JPanel when it is a subclass of JPanel any help would be appreciated

can't find method java

So I run the following program and my cmd prompt gives me an error saying that the getDescriptions() method is not found in the DataElements class. I'm sure there's a simple solution but I'm just stuck. Here's the DataElements class:
import java.io.*;
public class DataElements
{
File file;
private int columns;
private int row;
private int length;
private String name;
private String type;
private int position;
private String[] descriptions;
public File getFile(){
return file;
}
public void setFile(File f){
file = f;
}
public int getColumns(){
return columns;
}
public void setColumns(int c){
columns = c;
}
public int getRow(){
return row;
}
public void setRow(int r){
row = r;
}
public int getLength(){
return length;
}
public void setLength(int l){
length = l;
}
public String getName(){
return name;
}
public void setName(String n){
name = n;
}
public String getType(){
return type;
}
public void setType(String t){
type = t;
}
public int getPosition(){
return position;
}
public void setPosition(int p){
position = p;
}
public String[] getDescriptions(){
return description;
}
public void setDescriptions(String[] d){
description = d;
}
}
And here's the main method. If you need the CMSReader class let me know, but the problem seems to be stuck in these two classes
import java.util.Scanner;
import java.io.*;
public class Project2{
public static void main(String[] args) throws FileNotFoundException{
Scanner keyboard = new Scanner(System.in);
boolean fileParsed = false;
String inFile;
String outFile;
if(args.length != 1){
System.out.println("Error. Enter one argument: the file that needs to be parsed.");
System.exit(0);
}
Scanner scan = new Scanner(new File(args[0]));
DataElements storage = new DataElements();
CMSReader reader = new CMSReader(scan,storage);
reader.scanTopData();
System.out.println("Input File - " + storage.getName());
System.out.println("Output File - ");//*************Look at this*********************
System.out.println("Number of Variables - " + storage.getColumns());
System.out.println("Number of Records - " + storage.getRow());
System.out.println("Record Length - " + storage.getLength());
System.out.println("Variable information:");
reader.scanVariableData();
String[] variableData = storage.getDescriptions();
for(int i = 0; i < variableData.length ; i++){
System.out.println(variableData[i]);
}
}
}
I appreciate any help. Like I said, I'm sure it's something dumb but I've been looking at this for too long.
The variable description is not declared in your DataElements class, which is why DataElements file cannot compile, and my guess is that you have an older compiled version (.class file) of DataElements which does not contain that method.
Recommendation:
Start working with a good IDE (IntelliJ is my personal favorite, but Eclipse and Netbeans are also good options). A good IDE, on top of all other goodies it provides, will highlight such issues in a way you won't miss.

Accessing public method of another class

Basically, I'm trying to call a method in another class, but I'm running into errors.
Here is the code:
public class Ocean
{
private static int gridWidth, gridHeight;
private int safeCount;
private boolean shipSafe;
private static Ship[] ships;
private static int[][] grids;
public Ocean(int a, int b, int c)
{
grids = new int[a][b];
ships = new Ship[c];
for(int i = 0; i < c; i++)
{
ships[i] = Ship();
}
gridWidth = a;
gridHeight = b;
}
I cannot access the public Ship class, and the error appears in ships[i] = Ship();
I've tried redefining it, adding a constructor.
change the line ships[i] = new Ship();

Bluej keeps rounding my double... How do I get it to stop?

I am doing a project, I have a test file and interface and I am told to make a "line" file that successfully implements the interface and checks out successful on all the tests.
It implements and is successful on 3 of the 4 tests but fails on the last because it rounds the slope to 1.0...
Here is the code for the tester:
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* The test class LineTest.
*
* #author (your name)
* #version (a version number or a date)
*/
public class LineTest
{
/**
* Default constructor for test class LineTest
*/
public LineTest()
{
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
#Before
public void setUp()
{
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
#After
public void tearDown()
{
}
#Test
public void testConstructor()
{
Line line1 = new Line(10, 10, 25, 25);
assertEquals(10, line1.getXOne());
assertEquals(25, line1.getXTwo());
assertEquals(10, line1.getYOne());
assertEquals(25, line1.getYTwo());
}
#Test
public void testGetSlope()
{
Line line1 = new Line(10, 10, -25, -25);
assertEquals(0.0, line1.getSlope(), 0.1);
line1.print();
}
#Test
public void testCalcSlope()
{
Line line1 = new Line(10, 10, -25, -25);
line1.calculateSlope();
assertEquals(1.0, line1.getSlope(), 0.1);
line1.print();
}
#Test
public void testSetCoords()
{
Line line1 = new Line(10, 10, -25, -35);
line1.calculateSlope();
assertEquals(1.285, line1.getSlope(), 0.003);
line1.print();
line1.setCoordinates(10, 10, 25, 35);
line1.calculateSlope();
assertEquals(1.667, line1.getSlope(), 0.001);
line1.print();
}
}
Here is the line class:
public class Line
{
private int xOne,yOne, xTwo, yTwo;
private double slope;
public Line(int x1, int y1, int x2, int y2)
{
xOne=x1;
yOne=y1;
xTwo=x2;
yTwo=y2;
}
public void setCoordinates(int x1, int y1, int x2, int y2)
{
x1=xOne;
y1=yOne;
x2=xTwo;
y2=yTwo;
}
public void calculateSlope( )
{
slope = (((yTwo)-(yOne))/((xTwo)-(xOne)));
}
public void print( )
{
System.out.println("The slope of the line created by the points ("+xOne+","+yOne+"), and ("+xTwo+","+yTwo+") is "+slope+".");
}
public int getXOne(){
return xOne;
}
public int getXTwo(){
return xTwo;
}
public int getYOne(){
return yOne;
}
public int getYTwo(){
return yTwo;
}
public double getSlope(){
return slope;
}
}
Here is the interface:
public interface TestableLine
{
public void setCoordinates(int x1, int y1, int x2, int y2);
public void calculateSlope( );
public void print( );
public int getXOne();
public int getYOne();
public int getXTwo();
public int getYTwo();
public double getSlope();
}
What is wrong here? I have tried specifying the number of decimals to round to, it just makes test 3 fail as well.
You are calculating the slope only with int values. In the method which calculates the slope you can create double variables out of the int values and use those to make the calculation.

Selection does not contain an applet from eclipse

When I try to run/debug this code:
package ca.kianyarand.game;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH / 12*9;
public static final int SCALE =3;
public static final String NAME = "Game";
private JFrame frame;
public boolean running = false;
public Game(){
setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public synchronized void start() {
running = true;
new Thread(this).start();
}
public synchronized void stop() {
running = false;
}
public void run(){
while(running){
}
}
public static void name(String []args){
new Game().start();
}
}
It comes up with 'Selection does not contain an applet' when trying to run/debug it on a Java Applet and it also comes up with 'Selection does not contain a main type' when running with a Java Application.