No main class found? Netbeans - java

I was working on my class project. I tried to draw a face emoji using the graphics thing, but I typed in the class and it says FaceComponent is not found in the FaceComponent project. I was confused about how to name the class and main method.Also, if I want to add a frame, I would have to introduce another class and main method. How do I do that in one program?
package facecomponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
/**
*
* #author HelenPeng
*/
public class FaceComponent extends JComponent {
public static void main(Graphics g) {
Graphics2D g2= (Graphics2D)g;
Ellipse2D.Double interestingEmoji = new Ellipse2D.Double(0,0,100,100);
g2.draw(interestingEmoji);
Ellipse2D .Double eye1 = new Ellipse2D.Double(25,25,10,10);
Ellipse2D .Double eye2 = new Ellipse2D.Double(50,25,10,10);
g2.draw(eye2);
g2.draw(eye1);
Line2D.Double mouth = new Line2D.Double(35,75,75,75) ;
g2.draw(mouth);
}
}

In Java args contains the supplied command-line arguments as an array of String objects.
change
public static void main(Graphics g)
In the Java programming language, every application must contain a
main method whose signature is:
public static void main(String[] args) {

When your java program run as application JVM will first try to find main method will one of the below syntax, so try to change your syntax to one of the belo version.
public static void main(String[] args) { }//Prefered style
public static void main(String args[]) { }
public static void main(String... args) { }//which additionally allows callers to use varargs syntax

Here your definition of main method is wrong. One solution is to have a main class and a drawing class. Change your FaceComponent class to below:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
import javax.swing.JPanel;
/**
*
* #author HelenPeng
*/
public class FaceComponent extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2= (Graphics2D)g;
Ellipse2D.Double interestingEmoji = new Ellipse2D.Double(0,0,100,100);
g2.draw(interestingEmoji);
Ellipse2D .Double eye1 = new Ellipse2D.Double(25,25,10,10);
Ellipse2D .Double eye2 = new Ellipse2D.Double(50,25,10,10);
g2.draw(eye2);
g2.draw(eye1);
Line2D.Double mouth = new Line2D.Double(35,75,75,75) ;
g2.draw(mouth);
}
}
Then create a new class called FaceComponentTest and paste the code below in it.
import javax.swing.JFrame;
public class FaceComponentTest
{
public static void main( String[] args )
{
FaceComponent faceComponent = new FaceComponent();
JFrame faceComponentFrame = new JFrame( "My Face" );
faceComponentFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
faceComponentFrame.add( faceComponent );
faceComponentFrame.setSize( 400, 400 );
faceComponentFrame.setVisible( true );
}
This will display the face. Good luck.

public class FirstClass
{
public static void main(String[] args)
{
System.out.println("Java");
}
}
In above coding portion everything is good. But you may still get,, "no main class found"!! (It is because, I have deleted the built in class and created a new class named "FirstClass".)
For not getting this error at first 'debug' the class and then 'run'

Related

JFrame repaint() and revalidate() only updating when window is resized on Mac os

I use this class for my school app projects. It is how I set the application up and it extends JFrame and implements Runnable. Now whenever I use this in school on a Windows computer and everything works and the screen updates, but at home on a Mac it doesn't. I use Eclipse neon with JDK 1.8.0_101
Please help me out, I can't test any projects at home cause of this.
import java.awt.Graphics;
import javax.swing.JFrame;
public abstract class GUIApplication extends JFrame implements Runnable{
private Screen currentScreen;
//no main, cant instentiate an abstract class
public GUIApplication(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int x=40;
int y=40;
int width=1000;
int height=640;
setBounds(x,y,width,height);
initScreen();
setVisible(true);
}
//this is a method for creating the starting screen
protected abstract void initScreen();
public void setScreen(Screen screen){
//stop controls from previous screen
removeListeners();
setCurrentScreen(screen);
//add new controls
addListeners();
}
private void removeListeners(){
if(getCurrentScreen() != null){
if(getCurrentScreen().getMouseListener() != null) removeMouseListener(getCurrentScreen().getMouseListener());
if(getCurrentScreen().getMouseMotionListener() != null) removeMouseMotionListener(getCurrentScreen().getMouseMotionListener());
if(getCurrentScreen().getKeyListener() != null) removeKeyListener(getCurrentScreen().getKeyListener());
// if(currentScreen.getMouseWheelListener() != null) removeMouseWheelListener(currentScreen.getMouseWheelListener());
}
}
private void addListeners(){
if(getCurrentScreen() != null){
if(getCurrentScreen().getMouseListener() != null)addMouseListener(getCurrentScreen().getMouseListener());
if(getCurrentScreen().getMouseMotionListener() != null) addMouseMotionListener(getCurrentScreen().getMouseMotionListener());
if(getCurrentScreen().getKeyListener() != null){
addKeyListener(getCurrentScreen().getKeyListener());
}
// if(currentScreen.getMouseWheelListener() != null) addMouseWheelListener(currentScreen.getMouseWheelListener());
}
}
public void paint(Graphics g){
g.drawImage(getCurrentScreen().getImage(), 0, 0, null);
}
public void run(){
while(true){
getCurrentScreen().update();
repaint();
try {
Thread.sleep(30);
repaint();
revalidate();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Screen getCurrentScreen() {
return currentScreen;
}
public void setCurrentScreen(Screen currentScreen) {
this.currentScreen = currentScreen;
}
}
This is how a game would start:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.JFrame;
import game.mainScreenTeam.Dragon;
import game.mainScreenTeam.HomeScreen;
import game.miniGameTeam.GameInstructions;
import game.miniGameTeam.GameScreen;
import game.miniGameTeam.HighScoreScreen;
import game.shopScreen.BuyScreenWendy;
import game.shopScreen.HomeShopScreen;
import game.shopScreen.SellShopZheng;
import guiPractice.GUIApplication;
import guiPractice.Screen;
import guiPractice.components.AnimatedComponent;
/**
* #author Kat
*
*/
public class DragonLand extends GUIApplication {
public static DragonLand game;
public static int coins = 1500;
public static HomeScreen homeScreen;
public static Screen shopMain; // shop 1
public static Screen sellScreen; // shop 2
public static Screen buyScreen; // shop 3
public static Screen highscoreScreen; // high score
public static GameScreen miniGameScreen; // minigame
public static Screen gameInstructionsScreen;
public static Screen HelpScreen;
public static Color NAVY;
public static Color BRIGHT_PINK;
public static Color LIGHT_PINK;
public static Color LIGHT_NUDE;
public static Color DARKER_NUDE;
/**
*
*/
// public static void addDragon(AnimatedComponent a){
// dragonList.add(a);
// }
public DragonLand() {
}
/* (non-Javadoc)
* #see guiPractice.GUIApplication#initScreen()
*/
#Override
protected void initScreen() {
initColors();
miniGameScreen = new GameScreen(getWidth(),getHeight());
shopMain = new HomeShopScreen(getWidth(),getHeight());
sellScreen = new SellShopZheng(getWidth(),getHeight());
homeScreen = new HomeScreen(getWidth(),getHeight());
buyScreen = new BuyScreenWendy(getWidth(),getHeight());
highscoreScreen = new HighScoreScreen(getWidth(),getHeight());
HomeScreen.jenCode = new game.mainScreenTeam.HomeJenniber();
gameInstructionsScreen = new GameInstructions(getWidth(), getHeight());
setScreen(homeScreen);
}
private void initColors() {
NAVY = new Color(62,74,99);
BRIGHT_PINK = new Color(224,102,102);
LIGHT_PINK = new Color(248,186,182);
LIGHT_NUDE = new Color(244,215,183);
DARKER_NUDE = new Color(230,195,147);
}
/**
* #param args
*/
public static void main(String[] args) {
game = new DragonLand();
Thread go = new Thread(game);
go.start();
}
//public coin getter + setter
public void setCoins(int x){
coins = x;
}
public int getCoins(){
return coins;
}
}
This is the home screen
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import game.DragonLand;
import guiPractice.ClickableScreen;
import guiPractice.components.Action;
import guiPractice.components.AnimatedComponent;
import guiPractice.components.Button;
import guiPractice.components.Graphic;
import guiPractice.components.TextLabel;
import guiPractice.components.Visible;
import guiPractice.sampleGames.MouseFollower;
/**
* #author Kat
* #author Jenniber
*
*/
public class HomeScreen extends ClickableScreen implements Runnable{
private Graphic background;
public static HomeJenniber jenCode;
public HomeScreen(int width, int height) {
super(width, height);
Thread play = new Thread(this);
play.start();
}
#Override
public void initAllObjects(ArrayList<Visible> viewObjects) {
background=new Graphic(0,0,getWidth(),getHeight(),"img/Grassland.png");
viewObjects.add(background);
HomeKat katCode=new HomeKat(viewObjects, getWidth(), getHeight());
}
#Override
public void run() {
}
}
katCode adds buttons to the screen and image annimations
public void paint(Graphics g){
g.drawImage(getCurrentScreen().getImage(), 0, 0, null);
}
Don't override paint() on a JFrame.
The proper way to do custom painting is to override paintComponent(...) on a JPanel (or JComponent) and then you can set the content pane of the frame to this panel. And don't forget to invoke super.paintComponent(...) as the first statement in the method. Read the section from the Swing tutorial on Custom Painting for more information and working examples.
However if you do get lazy, then at minimum you need to invoke super.paint(...) as the first statement in the paint(...) method.
Also, I doubt you need the revalidate(), since you don't appear to be adding/removing components from the frame.
But in general the order should be:
revalidate(); // to invoke the layout manager
repaint(); // paint components in new location.
I also don't know why you are invoking the update() method. That seems like old AWT code which you don't use in Swing. I suggest you take a look at the tutorial link I gave you and look at the table of contents for other Swing basics.

Update JFrame paintings

I want to update my rectangle when my key is pressed currently I have almost achieved that
package com.raggaer.frame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Elements extends JPanel {
private static int y = 250;
public Elements() {
this.getInputMap().put(KeyStroke.getKeyStroke("UP"), "up");
this.getActionMap().put("up", new Listener("up"));
this.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "down");
this.getActionMap().put("down", new Listener("down"));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(250, this.y, 10, 10);
}
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
public static void setY(int cord) {
y += cord;
// Cant access repaint()
}
}
And this is my listener class
package com.raggaer.frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.AbstractAction;
public class Listener extends AbstractAction {
private String code;
//final Elements game = new Elements();
public Listener(String order) {
this.code = order;
}
public void actionPerformed(ActionEvent e) {
System.out.println(this.code);
Elements.setY(1);
}
}
Currently everything works except that I dont know how to update the y position of the rectangle, I tried using an static variable but then I cant call the repaint() method.
No,.... don't use static anything, neither methods nor fields because if you do this, you'll break object-oriented programming rules, leaving your code difficult to update and maintain. The exception of course would be your main method, and carefully chosen fields and methods who truly belong to the class. Instead give the class that needs to call the Element object's method a valid reference to the object whose state you wish to change, the drawing JPanel or Element object. This can be done by passing in a valid reference via a constructor parameter.
public class Listener extends AbstractAction {
private Elements elements;
public Listener(Elements elements) {
this.elements = elements;
}

Need help finding out why this error occurs

My code is this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.Timer;
public class chromeNPlayerScreen extends JFrame implements ActionListener{
DrawScreen dPnl = new DrawScreen();
public void actionPerformed(ActionEvent e){
}
public void main(String[ ] args){
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.add(dPnl);
this.setSize(600,600);;
this.setVisible(true);
this.setResizable(false);
this.setLocation(200, 200);
}
}
But when i run it.....
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Could someone explain to me why this isnt working?
the DrawScreen code is
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
public class DrawScreen extends JPanel {
String picPath = "pictures/";
ClassLoader cl = pokemonChromeNewPlayerScreen.class.getClassLoader();
URL imgURL = cl.getResource(picPath+"welcomeBG.png"),imgURL2 = cl.getResource(picPath+"dialogBox.png"),
imgURL3 = cl.getResource(picPath+"Professor.png");
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG, imgDialog, imgProfessor;
public void imgImport(){
imgBG = tk.createImage(imgURL);
imgDialog = tk.createImage(imgURL2);
imgProfessor = tk.createImage(imgURL3);
}
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
Graphics2D g2 = (Graphics2D)g;
for(int x=0;x<=600;x+=25){
g2.drawLine(x,0,x,600);
g2.drawString(""+x,x+5,20);
}
for(int y=0;y<=600;y+=25){
g2.drawLine(0,y,600,y);
g2.drawString(" "+y,0,y+20);
}
}
}
This is the code for the DrawScreen, atm all it does it drag a grid but thats because i just started it and wanted the x,y values for different positions
I'm assuming this is something related to your IDE. Specifically, it's looking for a non-static version of main().
This:
public void main(String[ ] args){
}
Should actually be:
public static void main(String[ ] args){
}
... of course, this means the this reference no longer works - you'll need to actually create a chromeNPlayerScreen first:
public static void main(String[ ] args){
chromeNPlayerScreen screen = new chromeNPlayerScreen();
screen.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
screen.add(dPnl);
screen.setSize(600,600);;
screen.setVisible(true);
screen.setResizable(false);
screen.setLocation(200, 200);
}
Your main method is currently not behaving as the entry method. It should be defined as static i.e.
public static void main(String[ ] args){
public void main(String[ ] args){
should be
public static void main(String[ ] args){
Without the appropriate main-method declaration there is no entry point for the JVM.
Having said this, it looks like the code currently in your 'main' should really be in a constructor of your class - and it looks like you probably meant to create an instance of your class in the main-method.
Your main is not static , replace this:
public void main(String[] args)
by this :
public static void main(String[] args)

Troubles with a piechart applet

Alright the deal is I am trying to use the drawPie method to create my pie chart in an applet. After attempting google searches I find multiple tutorials that explain part of the process but not all of it. While trying to knit together partial information I am not getting the results I want.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JApplet;
import javax.swing.JComponent;
public class JLW_PieApplet extends JApplet {
class PieData extends JComponent {
PieValue[] slices = new PieValue[4];
PieData() {
slices[0] = new PieValue(35, Color.red);
slices[1] = new PieValue(33, Color.green);
slices[2] = new PieValue(20, Color.pink);
slices[3] = new PieValue(12, Color.blue);
}
public void paint(Graphics g) {
drawPie((Graphics2D)g, getBounds(), slices);
}
}
}
Ther isn't such method in Swing called drawPie. Without the contents of this method, we have no idea of how to help you
Try having a read through 2D Graphics and have a look at Ellipse2D in particular
The other problem I can see is you don't call super.paint(g) in your paint method. This is VERY, VERY, VERY important
You have a PieData component within your applet but you never add it, so you need to add it in init and bring in drawPie from your link above:
public class JLW_PieApplet extends JApplet {
public void init() {
add(new PieData());
}
class PieData extends JComponent {
PieValue[] slices = new PieValue[4];
PieData() {
slices[0] = ...
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawPie((Graphics2D) g, getBounds(), slices);
}
public void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) {
...

store images and display in different classes

I'm trying to get mutiple images in 'GetImage' class, and disply them in the main class.
Can anybody show me an example how to do it?? I tried bunch of other samples but they didn't work since I have two classes.
Here is one that I tried.
main clss:
import java.awt.*;
import hsa.*;
public class Test
{
static Console c;
public void Display()
{
GetImage c = new GetImage();
c.paint(g);
}
public Test()
{
c = new Console ();
}
public static void main (String[] args) throws Exception
{
Test = new Test();
a.Display();
}
}
seperate class:
import java.awt.*;
import hsa.Console;
import java.awt.event.*;
public class GetImage extends Frame
{
Image image;
String imageName = "ImageFileName.jpg";
public void paint (Graphics g)
{
Toolkit tool = Toolkit.getDefaultToolkit ();
image = tool.getImage (imageName);
g.drawImage (image, 30, 30, this); // location of the image
g.drawString (imageName, 100, 50); // location of the name
}
}
I'm not very familiar with the hsa package, but some quick googling says it's an educational package from some company that has since gone out of business, correct me if I'm wrong. So personally I'd try to avoid using any of their stuff if you can.
If you have to use this for school or something then you probably want to stick entirely with their package instead of mix and matching hsa with awt. Something like this might accomplish what you want, but again I'm not familiar with the hsa package.
import java.awt.*;
import hsa.*;
public class Test
{
static Console c;
public void Display()
{
GetImage gI = new GetImage(c,25,80,12);
}
public Test()
{
c = new Console ();
}
public static void main (String[] args) throws Exception
{
Test = new Test();
a.Display();
}
}
import java.awt.*;
import hsa.ConsoleCanvasGraphics;
import java.awt.event.*;
public class GetImage extends ConsoleCanvasGraphics
{
Image image, image2;
String imageName = "ImageFileName.jpg", image2Name = "Image2FileName.jpg";
public GetImage(ConsoleParent parent, int rows, int columns, int fontSize)
{
Toolkit tool = Toolkit.getDefaultToolkit ();
image = tool.getImage (imageName);
image2 = tool.getImage (image2Name);
super(parent,rows,columns,fontSize);
drawImage(image,30,30,this);
drawImage(image2,30,60,this);
drawString(imageName,100,50,new Font("TimesRoman", Font.PLAIN, 20),Color.BLACK);
drawString(image2Name,100,80,new Font("TimesRoman", Font.PLAIN, 20),Color.BLACK);
}
}
Again, I'd try to avoid hsa myself, but if you're set on using it and need to have two separate classes in your program then the above should be a rough outline of something that might work.

Categories