Getting Compile time error [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm getting the following error : cannot find symbol color b=new color(Color.BLACK);
EDIT: I'm trying to create an instance of the class color.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class eventqueue{
public static void main(String args[]){
final JFrame frame=new JFrame("PROGRAM");
final JPanel panel=new JPanel();
EventQueue.invokeLater(new Runnable(){
public void run(){
final JButton black=new JButton("BLACK");
panel.add(black);
frame.add(panel);
color b=new color(Color.BLACK);
black.addActionListener(b);
frame.setVisible(true);
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
class color implements ActionListener{
public Color c;
public color(Color bc){
c=bc;
}
public void actionPerformed(ActionEvent event){
panel.setBackground(c);
}
}
}
});
}
}

Since color is class that is locally declared in a run() method. Now that class must be declared before it's use.
Sample code:
EventQueue.invokeLater(new Runnable() {
public void run() {
...
// MOVE it HERE
class color implements ActionListener {
public Color c;
public color(Color bc) {
c = bc;
}
public void actionPerformed(ActionEvent event) {
panel.setBackground(c);
}
}
// Now use it HERE
color b = new color(Color.BLACK);
black.addActionListener(b);
...
}
});
Note: follow Java Naming convention.

Declare the variable as,
Color b = new Color(Color.BLACK);

Change your code
Color b = new Color(Color.BLACK);
to represent the Color class. Which is the thing you want in your code.
You can even see that there is a color (I really mean to say color) difference of both Color and color. Lowercase is the variable name and Uppercase is the Class name.

Related

How to use the JComponent [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm very new to programming. I just started today and am following a book called learning java by Patrick Neimeyer and Daniel Leuck.
It started by asking me to create the Hello World ! programme, and did this by placing it in a JFrame which was all done in one class, it is now asking me two create another class in which will use a JComponent. I've tried creating another class and have just ended up going around in circles with errors.
This is my previous attempt. Can someone explain please what I'm meant to do?! I know it is fairly basic, but I am struggling.
It's telling me that the new code for the class is:
import java.awt.*;
class HelloComponent extends JComponent {
public void paintComponent( Graphics g ) {
g.drawString( "Hello, Java!", 125, 95 );
}
}
Where do I add this?
This is a rather perplexing way to start programming in Java. Even so, in this instance why would you not use a JLabel rather than creating your own JComponent?
In any case, as some of the comments already said, you need to add your JComponent to a JFrame.
An example below:
Example JFrame class
This class extends JFrame rather than creating and using a JFrame object. It also includes the main method as well, just to avoid another class. The main method runs the GUI on the event dispatch thread. This part is important, but not so much for a beginner.
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class ExampleFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ExampleFrame frame = new ExampleFrame();
frame.createAndShow();
}
});
}
public void createAndShow() {
getContentPane().add(new HelloComponent());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Hello World Example");
pack();
setVisible(true);
}
}
HelloComponent
As before with your version, but sets the preferred size so that when the JFrame is 'packed' it sizes to the component. Additionally, I've used the Graphic font height metric to position the y-coordinate, since the drawString method places the y coordinate at the bottom left.
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
public class HelloComponent extends JComponent {
private final int COMPONENT_WIDTH = 100;
private final int COMPONENT_HEIGHT = 30;
public HelloComponent() {
setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello World", 0, g.getFontMetrics().getHeight());
}
}

Failed to make my own drawing widget on java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I have stucked to 4 lines of code.
They supposed to let me create my own widget.
But it seems that the main method is missing.
I have tried to write a main class on my-own and create an object in paintComponent() method with no luck.
I know that i am not supposed to call the method myself, but when i run the program i get the error Main method not found in class....
Can someone please explain me how this works or give me a link to read?
Here is the simple code that I tried:
import javax.swing.*;
import java.awt.*;
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(20, 50, 100, 100);
}
}
Try this simple code, it should be helpful to you:
import javax.swing.*;
import java.awt.*;
public class MyDrawPanel extends JPanel {
private static final int WIDE = 300;
private static final int HIGH = 200;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.orange);
g.fillRect(20, 50, 100, 100);
}
public MyDrawPanel() {
setBackground(Color.white);
// Set a initial size for the program window
this.setPreferredSize(new Dimension(WIDE, HIGH));
}
private void display() {
// Some statements to let the JFrame appear in a good way
JFrame f = new JFrame("MyDrawPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
// Main method is called when the program is runned
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new MyDrawPanel().display();
}
});
}
}
Output:
If you are new to java, I would suggest to just give 4-5 days on Tutorials | Javatpoint.
Java rule is that the java file name should match the class name containing main method.
So a simple code of main method:
class Simple {
public static void main(String args[]) {
System.out.println("Hello Java");
}
}
In this case this code should be in Simple.java file.

Problems with java graphics [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can anyone give me some information what did I do wrong? Program runs but drowing a rectangle or anything else doesnt work. There is just empty space on the middle. I tried open the program from terminal and eclipse on kubuntu 15 and windows. Always with the same result.
I m just starting my adventure with java so please be patient.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyGUI {
JFrame frame;
JLabel label;
public static void main(String[] args)
{
MyGUI gui = new MyGUI();
gui.go();
}
public void go()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton labelButton = new JButton("Change label");
labelButton.addActionListener(new LabelListener());
JButton colorButton = new JButton("Change color");
colorButton.addActionListener(new ColorListener());
label = new JLabel("LABEL");
DrawSmth2 drawPanel = new DrawSmth2();
frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.EAST, labelButton);
frame.getContentPane().add(BorderLayout.WEST, label);
frame.setSize(680,480);
frame.setVisible(true);
}
class LabelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
label.setText("DONE");
}
}
class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
frame.repaint();
}
}
public class DrawSmth2 extends JPanel{
public void PaintComponent(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
Java is case-sensitive which means that PaintComponent is not the same as paintComponent. To avoid such problems always add #Override annotation to methods you want to override (if you are not overriding existing method compiler will inform you about it - more info: When do you use Java's #Override annotation and why?).
Also don't forget to call super.paintComponent(g); at start of your own implementation of paintComponent to let Swing perform standard operations needed to properly paint this component.
Just add gloabal variable count;
int count=0;
class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
count++;
frame.repaint();
}
}
public class DrawSmth2 extends JPanel{
public void paintComponent(Graphics g)
{
if(count%2==1)
g.setColor(Color.blue);
g.fillRect(0, 0, getWidth(), getHeight());
}
}

JFrame appears empty and minimised [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to write a small GUI program that consist of one JComboBox that has to paint the ContentPane with a color chosen from the combobox.
I do not know why it doesn't work as it is supposed to. When I start my main method it produces an empty JFrame even without the comboBox, which I know that I had added to the JFrame.
This is my main method:
import javax.swing.JFrame;
public class TestRGBComboBox {
public static void main(String[] args) {
JFrame frame = new RgbComboBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
this is my JFrame class
public class RgbComboBoxFrame extends JFrame{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 300;
private JComboBox colorComboBox;
private ActionListener listener;
public void RgbComboBoxFrame() {
colorComboBox = new JComboBox();
colorComboBox.addItem("RED");
colorComboBox.addItem("GREEN");
colorComboBox.addItem("BLUE");
colorComboBox.setEditable(true);
listener = new AddListener();
paintContentPane();
createPanel();
setSize(FRAME_WIDTH,FRAME_HEIGHT);
}
class AddListener implements ActionListener{
public void actionPerformed(ActionEvent event){
paintContentPane();
}
}
private void paintContentPane(){
Color c = (Color)colorComboBox.getSelectedItem();
getContentPane().setBackground(c);
}
private void createPanel(){
JPanel controlPanel = new JPanel();
colorComboBox.addActionListener(listener);
controlPanel.add(colorComboBox);
add(controlPanel,BorderLayout.SOUTH);
}
}
Although all the code is there to create the panel and add things to it you never call them. public void RgbComboBoxFrame(){} should be a constructor and therefore written as public RgbComboBoxFrame(){}

paintComponent not Running [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Basically, I'm trying to do a test on my GUI to make sure it will paint.
Here are my classes:
Game
public class Game {
private static GUI gui = new GUI();
private static int[][] pixels = new int[10][10];
public static void main(String[] args) {
}
public void startGame() {
System.out.print("start");
gui.setGameFrame();
}
public static GUI getGUI() {
return gui;
}
public static int[][] getGraphics() {
return pixels;
}
}
GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUI extends JPanel {
private static Game game = new Game();
private static JPanel panel = new JPanel();
private static JFrame frame = new JFrame();
final private static int FRAME_HEIGHT = 500;
final private static int FRAME_WIDTH = 500;
//Board size 25x25px
final private static int PIXEL_SIZE = 20;
public GUI () {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setAttributes();
makeMenu();
}
});
}
public static void setAttributes() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("");
frame.setBackground(Color.black);
frame.setVisible(true);
}
private static void makeMenu() {
JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
game.startGame();
}
});
panel.add(start);
frame.add(panel);
frame.pack();
}
public void setGameFrame() {
panel.removeAll();
frame.getContentPane().add(Game.getGUI());
frame.setTitle("Snake v0.1");
frame.setSize(getPreferredSize());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(5, 5, 10, 10);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(FRAME_WIDTH, FRAME_HEIGHT);
}
public void paintGraphics() {
int[][] pixels = Game.getGraphics();
}
}
I've attempted to debug it, but cannot trace why it isn't functioning.
I believe it's something to do with:
frame.getContentPane().add(Game.getGUI());
But I'm not certain.
Me: "What's the purpose of the Game class? Why not just run everything from the Gui class? "
You: "because further code will involve logic and event handling, and sending these events to the GUI class for updating."
Think of the Game as your data model. Use the Game class for only data and data manipulation and keep all the GUI procedures in your GUI class. Just create an instance of the Game class in your GUI class. Run the program from your GUI class, i.e. have the main method in your GUI class. and run the invokeLater from the main method.
I copied and pasted your code directly into Netbeans, and it seems to be working.
On mine there seems to be some graphical object covering the top left of the white rectangle, which is why it didn't show up initially.
Try changing your fillRect() function to fill a larger area.
This worked for me:
g.fillRect(5, 5, 100, 100);

Categories

Resources