Calling a Java Method to Draw Graphics

calling a java method to draw graphics

Remove test_string from FrameTest class. Set test_string directly using set method. See example:

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

public static void main(String[] args) {
FrameTest1 test_frame = new FrameTest1();
test_frame.setContentString("I WANT TO DRAW THIS STRING");
}

}

class FrameTest1 extends JFrame {
private static final long serialVersionUID = 1L;

Painting painting = new Painting();

public FrameTest1() {
JFrame gui = new JFrame();
gui.setTitle("Test Title");
gui.setSize(400, 400);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container pane = gui.getContentPane();
pane.setLayout(new GridLayout(1, 1));

pane.add(painting);
gui.setVisible(true);

}

public void setContentString(String value) {
painting.test_string = value;
}
}

class Painting extends JPanel {
private static final long serialVersionUID = 1L;
String test_string;

public Painting() {
setBackground(Color.WHITE);
this.test_string = "TEMP STRING FROM PANEL";
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

g.setColor(Color.RED);
g.drawString(test_string, 20, 20);
}
}

How do I call a method which has Graphics g as its parameters?

Is there any other way to display a message on a JFrame screen?

Don't try to do custom painting.

I would use a JOptionPane.

Read the section from the Swing tutorial on How to Make Dialogs for more information.

You would use the simple JOptionPane.showMessageDialog(...) method.

Calling a method from a different class containing Graphics g [JAVA]

To fix of that compilation error you can pass a graphics object.

For example you can use windows graphics (But this may not be the requirement of your task/project. With JDK 10 Window.TITLE is not present, I doubt if it was there in earlier versions as well.)

Optionally: By conventions method names in Java should start with small cases characters so the method name should be draw.

public static void main(String[] args) {
GameObjects gameOBJ = new GameObjects();

//Pass the graphics object to the Draw method
Window window = new Window(Window.WIDTH, Window.HEIGHT, Window.TITLE);
Graphics graphics =window.getGraphics() ;
gameOBJ.Draw(graphics);
}

Call a method which has Graphics g as its parameters

Move the draw call to the paint method:

@Overrride // since you're overriding.
public void paint(Graphics g) {
super.paint(g);

if(shapetype != null) {
shapetype.draw(g);
}
}

Drawing the shape only once will not be enough, it will get overwritten each time paint is called, so it needs to be redrawn.

Calling methods to draw on JFrame

The Graphics object isn't declared anywhere. If you want to draw on your JPanel you should rather create a class extending JPanel and there add draw() method which will get an "automated" Graphics object.

Eventually you can create your own Graphics object but you didn't do that anywhere in that code. Your BoxMen class is very messy. You have to decide if you use Graphics object argument under paint() method or declare it yourself. I'm assuming you try the second one, if so, you should change the g to a (there isn't a g variable anywhere in BoxMen class). You can also get rid of the field g2 and use a local variable instead.

The error pops up because Java doesn't know what you mean by g (it isn't declared anywhere). It depends on you if you want to use the JPanel's Graphics or your own.

How can i draw something only once in Java using Graphics

The Swing painting system calls paintComponent() whenever there's a need to update something about the appearance of your component. The reason can be that the window was hidden or partially obscured and now becomes visible again, or that the contents of the component changed.

So, whenever Swing calls paintComponent(), it's important to draw everything that falls into the paint-requested part of the component, otherwise you'll get nasty paint artifacts like missing elements or leftovers from previous window states.

From your description, I guess it's mostly your software requesting a repaint of your component, by calling the repaint() method somewhere in your code. My recommendations:

  • Make sure you supply a rectangle to the repaint() call specifying the region that has changed (the snake head, more or less). Swing only repaints the component parts that are known to need it, by setting a fitted clipping region before calling paintComponent().
  • Optimize your paintComponent() implementation to check whether the clipping region of the Graphics object intersects with your walls. If not, you can skip painting the walls.


Related Topics



Leave a reply



Submit