(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.
Related
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...
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.
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 ));
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;
}
I created a new JApplet form in NetBeans:
public class UI extends javax.swing.JApplet {
//generated code...
}
And a JPanel in design mode named panou:
// Variables declaration - do not modify
private javax.swing.JPanel panou;
How do I get to draw a line on panou? I've been searching for this for 5 hours now so a code snippet and where to place it would be great. Using Graphics2D preferably.
Go to design mode
Right Click on the panel "panou"
Click "Costumize code"
In the dialog select in the first combobox "costum creation"
add after = new javax.swing.JPanel() this, so you see this:
panou = new javax.swing.JPanel(){
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Do the original draw
g.drawLine(10, 10, 60, 60); // Write here your coordinates
}
};
Make sure you import java.awt.Graphics.
The line that you will see is always one pixel thick. You can make it more "line" by doing the following:
Create this method:
public static final void setAntiAliasing(Graphics g, boolean yesno)
{
Object obj = yesno ? RenderingHints.VALUE_ANTIALIAS_ON
: RenderingHints.VALUE_ANTIALIAS_OFF;
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, obj);
}
And add after super.paintComponent(g); (in your costum creation) this:
setAntiAlias(g, true);
Edit:
What you are doing wrong is: you paint the line once (by creating the frame).
When you paint the line the frame is also invisible. The first draw is happening when the frame becomes visible. The frame will be Repainted, so everything from the previous paint will disappear.
Always you resize the frame, everything will be repainted. So you have to make sure each time the panel is painted, the line also is painted.
To do custom painting in a JPanel, one would need to make a subclass of a JPanel, and then overload the paintComponent method:
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
// Perform custom painting here.
}
}
In the example above, the MyPanel class is a subclass of JPanel, which will perform whatever custom painting is written in the paintComponent method.
For more information on how to do custom painting in Swing components, Lesson: Performing Custom Painting from The Java Tutorials have some examples.
If one wants to do painting with Java2D (i.e. using Graphics2D) then one could do some painting on a BufferedImage first, then draw the contents of the BufferedImage onto the JPanel:
class MyPanel extends JPanel {
BufferedImage image;
public MyPanel() {
Graphics2D g = image.createGraphics();
// Do Java2D painting onto the BufferedImage.
}
public void paintComponent(Graphics g) {
// Draw the contents of the BufferedImage onto the panel.
g.drawImage(image, 0, 0, null);
}
}
Further reading:
Painting in AWT and Swing
Trail: 2D Graphics