I am trying to move from a static main() to a non static method called paintComponent(), but the problem I am having is I can not move from Static to Non static in the way which I have. The class is a follows, where hunter and hunted are external classes:
import javax.swing.JFrame;
import java.awt.Graphics;
public class Main extends JFrame{ //Public class: Available for all other classes to refer to
private static final long serialVersionUID = -4511248732627763442L;
public static void main(String[] args){
frame();
repaint();
move(); //Passes to the method move() in the class Main()
}
public static JFrame frame(){
JFrame frame = new JFrame("Hunter VS Hunted"); //Sets the window title
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //Sets the size of the window
frame.setVisible(true); //Says to display the window
frame.setResizable(false); //Sets it so the screen cannot be adjusted
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes the window when the x is pushed
System.out.println("Frame works.");
return frame;
}
public void paintComponent(Graphics g){
super.paintComponents(g); //Assigns a graphics value to g, so that it can be passed to other methods
Hunted.paint(g);
Hunter.paint(g);
System.out.println("Main.paintComponent works.");
}
public static void move(){
Hunter.move(); //Passes to move() in the Hunter class
Hunted.move(); //Passes to move() in the Hunter class
}
}
Bear in mind I am a beginner, so please try to keep it simple!
You need to call repaint and paintComponents using an object.
JFrame frame = frame();
frame.repaint();
move();
Every non-static method needs to be called from an object. Static methods belongs to the class, so you can call them without an object.
repaint and paintComponents belongs to the JFrame object. They are not static and so they need to be called using an object (repaint will call paintComponents).
Your 'frame()' method will return a JFrame object. So you can call the repaint() method using the JFrame object that is returned from the 'frame()' method.
Having said that, I'm not sure what you are trying to accomplish in your code, and even if my explanation will resolve a Compilation-Error, without further elaboration, it might not accomplish what you are trying to achieve.
you have got some messed up code! but let me clean it up a bit, make it more presentable. However it is up to you to get it to work now. I have created another class for your jframe and adjusted your extra / repetitive code in the constractor.
btw i know nothing about haunted and haunter.
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
public static void main(String[] args){
myFrame x = new myFrame("Hunter VS Hunted");
x.ui(x.getContentPane());
}
}
class myFrame extends JFrame {
public myFrame(String name){
// constractor
super(name); //Sets the window title
setExtendedState(JFrame.MAXIMIZED_BOTH); //Sets the size of the window
setVisible(true); //Says to display the window
setResizable(false); //Sets it so the screen cannot be adjusted
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes the window when the x is pushed
}
public void ui(final Container pane){
JLabel test = new JLabel("test frame");
pane.add(test);
System.out.println("Frame works.");
}
}
now if you include graphics in this class, everytime you call up repaint(), it will call the function paintComponent(Graphics g). good luck :)
Related
I was working on this lab in class and when I tried changing the background color it would stay at its default of white can someone please explain where I my programming went wrong.
import javax.swing.*;
import java.awt.*;
public class DemoPoly extends JFrame {
// constructor
public DemoPoly() {
// defines Frame characteristics
int size = 300;
setSize(size,size);
setTitle("a random window");
getContentPane().setBackground(Color.red);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main (String[] args){
// instantiates a JFrame
// exits on close (opional)
JFrame object = new DemoPoly();
}
public void paint (Graphics g){
// This provides the Graphics object g, where
// you are going to use you graphics primitive
// to paint on the content pane of the frame.
int[] arr = {0,100,100,0};
int[] yarr = {0,0,100,100};
Square object = new Square(arr,yarr,Color.red);
AbstractPolygon randSquare = new Square(arr, yarr, Color.red);
}
I see a couple of problems in your code:
Extending JFrame is like saying your class is a JFrame, JFrame is a rigid container, instead create your GUI based on JPanels. See Java Swing extends JFrame vs calling it inside of class for more information.
You're breaking the paint chain by removing the super.paint(g) call on the paint(...) method. When changing your GUI to extend JPanel instead of JFrame you should use the paintComponent(...) method instead. Take the Lesson: Performing Custom Painting in Swing.
You forgot to add #Override notation on the paint(...) method.
You're not placing your program on the Event Dispatch Thread (EDT) which could cause threading issues.
You can solve this by changing your main() method like this:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Your constructor here
}
});
}
Instead of setting the JFrame size, override the getPreferredSize() method and call pack(). See Should I setPreferred|Maximum|MiniumSize in Java Swing?. The general consensus says yes.
Your problem gets solved by adding
super.paint(g);
on the paint(...) method:
#Override
public void paint(Graphics g) {
super.paint(g); //Never remove this
//Your code goes here
}
With all the above recommendations taken into account, your code should look like this now:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class DemoPoly {
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new DemoPoly().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
CustomPanel cp = new CustomPanel();
cp.setBackground(Color.RED);
frame.add(cp);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class CustomPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
Which produces this output (and is the same output that you'll get with your current code but better because it gives you more control over your components)
I'm don't understand your question. But here is code for change your background to RED;
public class DemoPoly extends JFrame {
public DemoPoly() {
// defines Frame characteristics
int size = 300;
setSize(size, size);
setTitle("a random window");
//change background here
getContentPane().setBackground(Color.red);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// instantiates a JFrame
// exits on close (opional)
JFrame object = new DemoPoly();
}
}
Your code is well. Maybe use #override in your paint method.
I'm trying to make a test window with some text on it, when I run my code, it doesn't draw the string. I specified the color for it. Can anybody help me with this?
import javax.swing.*;
import java.awt.*;
class Main
{
public static void main(String[] args) {
DrawFrame f = new DrawFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class DrawFrame extends JFrame
{
public DrawFrame(){
setTitle("For Aylin");
setSize(1280,720);
DrawPanel panel = new DrawPanel();
Container cp = getContentPane();
cp.add(panel);
}
}
class DrawPanel extends JPanel
{
public void paintComponents(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.darkGray);
g.drawString("Hi", 100, 10);
}
}
You should override the JPanel's paintComponent method not its paintComponents method as they are for two very different purposes. The first paints the component itself (what you want) while the second gets the child components held by this parent to paint themselves.
Also remember to change the super call so that it matches, and to use the #Override annotation above the method.
My program uses a Jframe, whenever I minimize the window and bring it back up, the program runs itself again. How can I cause it to not do this when minimized?
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.util.Random;
public class AB extends Component {
public void paint(Graphics g) {
Random r = new Random();
g.setColor(Color.black);
g.fillRect(r.nextInt(100), r.nextInt(100), 100, 100);
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.getContentPane().setBackground(Color.white);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(dim.width, dim.height);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new AB());
f.setVisible(true);
}
}
My guess: you have program logic present in your paint(...) or paintComponent(...) method override. If so, get the logic out of these methods, because as you're finding out, you do not have full control over when or even if they are called. The logic belongs elsewhere, perhaps in a Swing Timer, hard to say without code or a more detailed question.
If this doesn't help, then you'll need to show us the bugs in your code by creating and posting a minimal, runnable, example program as well as tell us more of the details of your program and its misbehavior.
After looking at your code, I see that my assumption/guess was correct: you are selecting your random numbers inside of the paint method, and so they will change with each repaint. You will want to
Create a class that extends JPanel.
Create your random numbers in this class's constructor, and use the random numbers to set class instance fields.
Override this class's paintComponent method
Don't forget to call the super's paintComponent method inside this method.
Use the numbers generated in the constructor in the paintComponent method override to draw your rectangle.
Place your JPanel into a JFrame.
e.g.,
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.*;
#SuppressWarnings("serial")
public class RandomRect extends JPanel {
private static final int MAX_INT = 100;
private static final Color RECT_COLOR = Color.black;
private Random random = new Random();
private int randomX;
private int randomY;
public RandomRect() {
randomX = random.nextInt(MAX_INT);
randomY = random.nextInt(MAX_INT);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(RECT_COLOR);
g.fillRect(randomX, randomY, MAX_INT, MAX_INT);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("RandomRect");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new RandomRect());
//frame.pack();
// frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Getting an error at frame.add(game);:
Multiple markers at this line
- Debug Current Instruction Pointer
- The method add(Component) in the type Container is not applicable for the arguments
(Display)
My code:
import java.awt.Canvas;
import java.awt.Component;
import javax.swing.JFrame;
public class Display {
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static void main(String[] args){
Display game = new Display();
JFrame frame = new JFrame();
frame.add(game);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
}
}
Your class Display should extend a Component (Container, Button, Canvas, Label ...). I think you would like to extend JPanel which is the most common, but it really depends on what Display class is intended to do:
public class Display extends JPanel {
}
Your Display should extend JPanel or some other Component as mentioned by the other answer.
For your purpose you should also override the paintComponent(Graphics g) method when you are ready to draw something on the Displayand have a constructor if your going to use it as a Component.
I am trying to learn java and i am practicing with a simple program with 2 simple buttons. Here is my code :
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Askhsh 3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ColorJPanel application = new ColorJPanel();
frame.add(application);
frame.setSize(500,500);
frame.setVisible(true);
}
}
And the class ColorJPanel:
import java.awt.*;
import javax.swing.*;
public class ColorJPanel extends JPanel{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
JButton arxikopoihsh = new JButton("Αρχικοποίκηση");
JButton klhrwsh = new JButton("Κλήρωση");
add(arxikopoihsh);
add(klhrwsh);
this.revalidate();
this.repaint();
}
}
As you can see the only thing i want to do for now is to place 2 simple buttons that do nothing! Here is my output:
http://imageshack.us/photo/my-images/847/efarmogh.jpg/
When i am running the application i am seeing the buttons filling the window!
Note that if i remove the "this.revalidate();" command i have to resize the window to see the buttons !
Thanks very much for your time :)
Don't add components in paintComponent. This method is for painting only, not for program logic or to build GUI's. Know that this method gets called many times, often by the JVM and most of the time this is out of your control, and also know that when you ask for it to be called via the repaint() method, this is only a suggestion and the paint manager may sometimes choose to ignore your request. The paintComponent method must be lean and fast as anything that slows it down will slow down the perceived responsiveness of your application.
In your current code, I don't even see a need to have a paintComponent method override, so unless you need it (if doing for instance custom painting of the component), I suggest that you get rid of this method (and the calls to repaint and revalidate). Instead, add your components in the class's constructor and make sure to pack your top level container after adding components and before calling setVisible(true). Most important -- read the Swing tutorials as this is all covered there.
e.g.,
Main.java
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Askhsh 3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ColorJPanel application = new ColorJPanel();
frame.add(application);
frame.pack();
frame.setVisible(true);
}
}
ColorJPanel.Java
import java.awt.*;
import javax.swing.*;
public class ColorJPanel extends JPanel{
public static final int CJP_WIDTH = 500;
public static final int CJP_HEIGHT = 500;
public ColorJPanel() {
this.setBackground(Color.WHITE);
JButton arxikopoihsh = new JButton("Αρχικοποίκηση");
JButton klhrwsh = new JButton("Κλήρωση");
add(arxikopoihsh);
add(klhrwsh);
}
// let the component size itself
public Dimension getPreferredSize() {
return new Dimension(CJP_WIDTH, CJP_HEIGHT);
}
}