How to make a graphic line into JFrame? - java

I use Eclipse, and I want make a graphic line into JFrame by following code:
public void Makeline () {
Graphics g=new Graphics(); // has error
Graphics2D g2 = (Graphics2D) g;
g2.draw(new Line2D.Double(0, 0, 20, 20));
}
but give followin error:
Cannot instantiate the type Graphics

Graphics is an abstract class, defining the requirements of the overall API.
Painting in Swing is done within the context of the paint chain. This is typically performed within the paintComponent method of components that extend from JComponent
Take a look at Perfoming Custom Painting for more details
You can also use a BufferdImage to generate a Graphics context, but you still need somewhere to draw the image, so it comes down what your are trying to achieve.

The solution is to overwrite the paintComponent method, but the JFrame isn't a JComponent, so instead of JFrame, use JPanel and then add the JPanel to the JFrame.
paintComponent(Graphics g) {
super.paintComponent(g)
//here goes your code
Graphics2D g2 = (Graphics2D) g;
...
}

Graphics is an abstract class. You can't instantiate following way.
Graphics g=new Graphics();
To access Graphics2D, first you need to override paint(Graphics) method.
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
}

Related

AWT-EventQueue-0 NullPointerException because of Graphics g initilisation

(I'm new here, sorry if I forget to write any important information).
I'm trying to visualise weather from the AccuWeather API and I am currently stuck with the initilisation of Graphics g, I think.
So this is my inilisation:
public class Draw extends JFrame {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
Graphics g;
I tried Graphics g = new Graphics(); but that doesn't work because Graphics g is static.
this is a part of my paint method:
public void paint (Double[] weather, String sender, Graphics g) {
super.paint(g);
Toolkit tk = Toolkit.getDefaultToolkit();
if (sender.equals("index5_Button")) {
Graphics2D stargazing = (Graphics2D) g;
((Graphics2D) g).setBackground(Color.BLACK);
this is what the call for paint looks like:
paint(weatherValue, sender, g);
So obviously it isn't initilising g correctly, but I don't know how to fix it.
Thanks a lot for possible help in advance!
I tried Graphics g = new Graphics();
You should not be trying to initialize the Graphics object of a component. Swing will create the Graphics object and pass it to the component when the component is repainted.
Custom painting is done by overriding the paintComponent(...) method of a JPanel. Then you add the panel to the JFrame`.
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// add custom painting logic here
}
Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started. The tutorials will show you how to better structure your classes.

Should I extend JPanel for custom class rendering in Swing?

I have a custom class representing spaceships that will rotate and fly around on the screen. However, I realized that if I want to use Swing, I have to render the ships via paintComponent().
This is the method I was going to use for rendering:
public void render(Graphics g){
AffineTransform original = ((Graphics2D) g).getTransform();
Graphics2D g2d = (Graphics2D) g;
g2d.rotate(orientation, getGamePosition().x, getGamePosition().y);
g2d.drawImage(spriteDefault, position.x, position.y, null);
g2d.setTransform(original);
}
So in order to use Swing, should I extend JPanel so that I can override paintComponent and put this code inside that method, or should it be JComponent, or some other way?
I just want to make sure I'm on the right track here...

About positions a Rectangle on the screen

I have a JFrame and a class extending JPanel and overriding paintComponent() (named DrawingPanel). An instance of DrawingPanel is added to the JFrame.
This is the paintComponent() method of DrawingPanel:
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Rectangle rect = new Rectangle(80,180,50,50);
g2d.fill(rect);
}
I ran the program, and it seems that the coordinats 80, 180 specified when creating rect, refer to the JFrame that contains the instance of DrawingPanel. I would expect that these coordinates would refer to the JPanel where the rectangle is actually painted.
1) Is it supposed to be like that?
2) Is it possible to change this?
Thanks
I would expect that these coordinates would refer to the JPanel where the rectangle is actually painted.
They do refer to the JPanel, not the frame.
Why do you think they refer to the frame? Post your SSCCE that demonstrates the problem.

draw graphics outside of paint method

private void draw_shape() {
Graphics g = getGraphics();
g.drawLine(0, 0, 100, 100);
repaint();
}
In paint method only those graphics are drawn which is a part of paint method because of which
I wanted to draw shapes outside of paint method.
This code draws the line but it immediately disappeares, I don't understand why this is happening. please help
This doens't work because you are getting the current Graphics outside of the Swing repaint thread. Basically:
you get the current Graphics
you draw something on it
then you call repaint() that will call the paint() of the component thus discarding all you did
To make it work you should override the paint (paintComponent for Swing) method of your object:
#Override
public void paint(Graphics g) {
super.paint(g); // if you have children to the component
g.drawLine(..)
}
and then just call repaint() when something has been modified.
The line disappears because Swing (or AWT) will call paint(Graphics) or paintComponent(Graphics g) in order to pain the component.
What you need to do is to put your drawing logic on the paint(Graphics) or paintComponent(Graphics g) method. The latter is more advisable.
If you really need to draw things using another method, store an image as a class field and draw this image on the paint or paintComponent methods.
Because the paint method also paints stuff. You should not draw graphics outside the paint method. You should instead override the paint method, like this:
#Override public void paint (Graphics g) {
super.paint(g);
g.drawLine(0, 0, 100, 100);
}
Thanks for the help found the answer
BufferedImage image = (BufferedImage) createImage(300, 300);
image.getGraphics().drawLine(0, 0, 300, 300);
jLabel1.setIcon( new ImageIcon(image ));

Java: Repaint artifacts with alpha painting

I just had a problem with artifacts at a JPanel which contains transparent parts.
My JPanel overrides the paintComponent() method:
protected void paintComponent(Graphics g) {
g2d = (Graphics2D) g;
drawMyAplhaImage(g2d);
}
As you can see, the Image drawn on the JPanel is a bit smaller than the JPanel itself.
The solution is to redraw the Parent component.
To save ressources I just repaint the area of the JPanel.
The new paintComponent() method:
protected void paintComponent(Graphics g) {
g2d = (Graphics2D) g;
getParent().repaint(getX(), getY(), getWidth(), getHeight());
drawMyAplhaImage(g2d);
}

Categories

Resources