Draw Image 2D Graphics












0















I am doing a game in java, but I didnt put image, I already tried to put it by "drawimage", but it is not working right = (
I do not know where would be the best place to save the image and how would be the best method to call the image.`



    public void paint(Graphics2D g2){
g2.setColor(getRandomColor());
g2.fillOval((int)x, (int)y, (int)diametro, (int)diametro);
}


`


This code is from my class asteroides they are a circle (because I'm going to test the collision with circles).
Thanks










share|improve this question




















  • 1





    English please.

    – shmosel
    Nov 14 '18 at 1:11











  • Bem-vindo ao StackOverflow! Este é um site somente em inglês. Eu recomendo tentar pt.stackoverflow.com

    – Marcus Campbell
    Nov 14 '18 at 1:20
















0















I am doing a game in java, but I didnt put image, I already tried to put it by "drawimage", but it is not working right = (
I do not know where would be the best place to save the image and how would be the best method to call the image.`



    public void paint(Graphics2D g2){
g2.setColor(getRandomColor());
g2.fillOval((int)x, (int)y, (int)diametro, (int)diametro);
}


`


This code is from my class asteroides they are a circle (because I'm going to test the collision with circles).
Thanks










share|improve this question




















  • 1





    English please.

    – shmosel
    Nov 14 '18 at 1:11











  • Bem-vindo ao StackOverflow! Este é um site somente em inglês. Eu recomendo tentar pt.stackoverflow.com

    – Marcus Campbell
    Nov 14 '18 at 1:20














0












0








0








I am doing a game in java, but I didnt put image, I already tried to put it by "drawimage", but it is not working right = (
I do not know where would be the best place to save the image and how would be the best method to call the image.`



    public void paint(Graphics2D g2){
g2.setColor(getRandomColor());
g2.fillOval((int)x, (int)y, (int)diametro, (int)diametro);
}


`


This code is from my class asteroides they are a circle (because I'm going to test the collision with circles).
Thanks










share|improve this question
















I am doing a game in java, but I didnt put image, I already tried to put it by "drawimage", but it is not working right = (
I do not know where would be the best place to save the image and how would be the best method to call the image.`



    public void paint(Graphics2D g2){
g2.setColor(getRandomColor());
g2.fillOval((int)x, (int)y, (int)diametro, (int)diametro);
}


`


This code is from my class asteroides they are a circle (because I'm going to test the collision with circles).
Thanks







java java-2d drawimage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 1:27







Sansone

















asked Nov 14 '18 at 1:10









Sansone Sansone

11




11








  • 1





    English please.

    – shmosel
    Nov 14 '18 at 1:11











  • Bem-vindo ao StackOverflow! Este é um site somente em inglês. Eu recomendo tentar pt.stackoverflow.com

    – Marcus Campbell
    Nov 14 '18 at 1:20














  • 1





    English please.

    – shmosel
    Nov 14 '18 at 1:11











  • Bem-vindo ao StackOverflow! Este é um site somente em inglês. Eu recomendo tentar pt.stackoverflow.com

    – Marcus Campbell
    Nov 14 '18 at 1:20








1




1





English please.

– shmosel
Nov 14 '18 at 1:11





English please.

– shmosel
Nov 14 '18 at 1:11













Bem-vindo ao StackOverflow! Este é um site somente em inglês. Eu recomendo tentar pt.stackoverflow.com

– Marcus Campbell
Nov 14 '18 at 1:20





Bem-vindo ao StackOverflow! Este é um site somente em inglês. Eu recomendo tentar pt.stackoverflow.com

– Marcus Campbell
Nov 14 '18 at 1:20












2 Answers
2






active

oldest

votes


















1














I'm pretty sure you mean you want do draw an image on the Panel. First of all, I'd recommend you create a resources folder inside the projects src folder, and add all images there. Once you're done, you have to load the image with imageIO and draw it with drawImage. Here's a short example:



package asteroid;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Nave {

BufferedImage iconeNave;

public Nave( ... ) {
try{
iconeNave = ImageIO.read(getClass().getResource("/resources/nave.png"));
}catch(IOException e){e.printStackTrace();}
catch(Exception e){e.printStackTrace();}
}

@Override
public void paint(Graphics2D g2){
AffineTransform at = new AffineTransform();
at.translate((int)x + radius/2.5,(int)y + radius/2.5);
at.rotate(Math.PI/2 + angle);
at.translate(-iconeNave.getWidth()/2, -iconeNave.getHeight()/2);
g2.drawImage(iconeNave, at, null);
}
}





share|improve this answer
























  • Caio Issa Thanks you. Now it's working.

    – Sansone
    Nov 16 '18 at 19:28





















0














In order to create an image in Java, you will first need to create a JPanel (essentially a window on your screen). It will look something like this (taken from javacodex.com):



import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;

public class JPanelExample {

public static void main(String arguments) throws IOException {

JPanel panel = new JPanel();

BufferedImage image = ImageIO.read(new File("./java.jpg"));
JLabel label = new JLabel(new ImageIcon(image));
panel.add(label);

// main window
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JPanel Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// add the Jpanel to the main window
frame.add(panel);

frame.pack();
frame.setVisible(true);

}
}


However, I am assuming that by "image," you mean a shape drawn out onto the screen instead (since you are using a paint method that draws a circle in your example). Then, you have your code in the correct method but need to implement a JPanel (since without a JPanel or JFrame you can't really display anything onto the screen graphics-wise).



Here is an example taken from Board.java of my Atari Breakout mini-game:



public class Board extends JPanel implements KeyListener {

// Other Game Code

public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);

// Shows lives left and score on screen
g2.drawString("Lives left: " + lives, 400, 600);
g2.drawString("Score: " + score, 400, 500);

// Paints Walls (separate paint method created in wall class)
topWall.paint(g2);
bottomWall.paint(g2);
leftWall.paint(g2);
rightWall.paint(g2);

// Paints 2 Balls (separate paint method created in ball class)
b.paint(g2);
b2.paint(g2);

// Paints Paddle (separate paint method created in paddle class)
paddle.paint(g2);

// Paints Bricks based on current level (separate paint method created in brick class)
// Bricks were created/stored in 2D Array, so drawn on screen through 2D Array as well.
if (level == 1) {
for (int x = 0; x < bricks.length; x++) {
for (int i = 0; i < bricks[0].length; i++) {
bricks[x][i].paint(g2);
}
}
}
if (level == 2) {
for (int x = 0; x < bricks.length; x++) {
for (int i = 0; i < bricks[0].length; i++) {
bricks[x][i].paint(g2);
}
}
}


}


Here is an example of a Paint Method in the Paddle Class:



// Constructor
public Paddle(int x, int y, int w, int h){

xpos = x;
ypos = y;
width = w;
height = h;

r = new Rectangle(xpos, ypos, width, height);
}

public void paint(Graphics2D g2){

g2.fill(r);
}


Output (What is shown on Screen):



enter image description here



Hope this helps you in drawing your circle!






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291767%2fdraw-image-2d-graphics%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I'm pretty sure you mean you want do draw an image on the Panel. First of all, I'd recommend you create a resources folder inside the projects src folder, and add all images there. Once you're done, you have to load the image with imageIO and draw it with drawImage. Here's a short example:



    package asteroid;

    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;

    public class Nave {

    BufferedImage iconeNave;

    public Nave( ... ) {
    try{
    iconeNave = ImageIO.read(getClass().getResource("/resources/nave.png"));
    }catch(IOException e){e.printStackTrace();}
    catch(Exception e){e.printStackTrace();}
    }

    @Override
    public void paint(Graphics2D g2){
    AffineTransform at = new AffineTransform();
    at.translate((int)x + radius/2.5,(int)y + radius/2.5);
    at.rotate(Math.PI/2 + angle);
    at.translate(-iconeNave.getWidth()/2, -iconeNave.getHeight()/2);
    g2.drawImage(iconeNave, at, null);
    }
    }





    share|improve this answer
























    • Caio Issa Thanks you. Now it's working.

      – Sansone
      Nov 16 '18 at 19:28


















    1














    I'm pretty sure you mean you want do draw an image on the Panel. First of all, I'd recommend you create a resources folder inside the projects src folder, and add all images there. Once you're done, you have to load the image with imageIO and draw it with drawImage. Here's a short example:



    package asteroid;

    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;

    public class Nave {

    BufferedImage iconeNave;

    public Nave( ... ) {
    try{
    iconeNave = ImageIO.read(getClass().getResource("/resources/nave.png"));
    }catch(IOException e){e.printStackTrace();}
    catch(Exception e){e.printStackTrace();}
    }

    @Override
    public void paint(Graphics2D g2){
    AffineTransform at = new AffineTransform();
    at.translate((int)x + radius/2.5,(int)y + radius/2.5);
    at.rotate(Math.PI/2 + angle);
    at.translate(-iconeNave.getWidth()/2, -iconeNave.getHeight()/2);
    g2.drawImage(iconeNave, at, null);
    }
    }





    share|improve this answer
























    • Caio Issa Thanks you. Now it's working.

      – Sansone
      Nov 16 '18 at 19:28
















    1












    1








    1







    I'm pretty sure you mean you want do draw an image on the Panel. First of all, I'd recommend you create a resources folder inside the projects src folder, and add all images there. Once you're done, you have to load the image with imageIO and draw it with drawImage. Here's a short example:



    package asteroid;

    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;

    public class Nave {

    BufferedImage iconeNave;

    public Nave( ... ) {
    try{
    iconeNave = ImageIO.read(getClass().getResource("/resources/nave.png"));
    }catch(IOException e){e.printStackTrace();}
    catch(Exception e){e.printStackTrace();}
    }

    @Override
    public void paint(Graphics2D g2){
    AffineTransform at = new AffineTransform();
    at.translate((int)x + radius/2.5,(int)y + radius/2.5);
    at.rotate(Math.PI/2 + angle);
    at.translate(-iconeNave.getWidth()/2, -iconeNave.getHeight()/2);
    g2.drawImage(iconeNave, at, null);
    }
    }





    share|improve this answer













    I'm pretty sure you mean you want do draw an image on the Panel. First of all, I'd recommend you create a resources folder inside the projects src folder, and add all images there. Once you're done, you have to load the image with imageIO and draw it with drawImage. Here's a short example:



    package asteroid;

    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;

    public class Nave {

    BufferedImage iconeNave;

    public Nave( ... ) {
    try{
    iconeNave = ImageIO.read(getClass().getResource("/resources/nave.png"));
    }catch(IOException e){e.printStackTrace();}
    catch(Exception e){e.printStackTrace();}
    }

    @Override
    public void paint(Graphics2D g2){
    AffineTransform at = new AffineTransform();
    at.translate((int)x + radius/2.5,(int)y + radius/2.5);
    at.rotate(Math.PI/2 + angle);
    at.translate(-iconeNave.getWidth()/2, -iconeNave.getHeight()/2);
    g2.drawImage(iconeNave, at, null);
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 17:21









    caio issacaio issa

    111




    111













    • Caio Issa Thanks you. Now it's working.

      – Sansone
      Nov 16 '18 at 19:28





















    • Caio Issa Thanks you. Now it's working.

      – Sansone
      Nov 16 '18 at 19:28



















    Caio Issa Thanks you. Now it's working.

    – Sansone
    Nov 16 '18 at 19:28







    Caio Issa Thanks you. Now it's working.

    – Sansone
    Nov 16 '18 at 19:28















    0














    In order to create an image in Java, you will first need to create a JPanel (essentially a window on your screen). It will look something like this (taken from javacodex.com):



    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.swing.*;
    import javax.imageio.ImageIO;

    public class JPanelExample {

    public static void main(String arguments) throws IOException {

    JPanel panel = new JPanel();

    BufferedImage image = ImageIO.read(new File("./java.jpg"));
    JLabel label = new JLabel(new ImageIcon(image));
    panel.add(label);

    // main window
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JPanel Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // add the Jpanel to the main window
    frame.add(panel);

    frame.pack();
    frame.setVisible(true);

    }
    }


    However, I am assuming that by "image," you mean a shape drawn out onto the screen instead (since you are using a paint method that draws a circle in your example). Then, you have your code in the correct method but need to implement a JPanel (since without a JPanel or JFrame you can't really display anything onto the screen graphics-wise).



    Here is an example taken from Board.java of my Atari Breakout mini-game:



    public class Board extends JPanel implements KeyListener {

    // Other Game Code

    public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.BLACK);

    // Shows lives left and score on screen
    g2.drawString("Lives left: " + lives, 400, 600);
    g2.drawString("Score: " + score, 400, 500);

    // Paints Walls (separate paint method created in wall class)
    topWall.paint(g2);
    bottomWall.paint(g2);
    leftWall.paint(g2);
    rightWall.paint(g2);

    // Paints 2 Balls (separate paint method created in ball class)
    b.paint(g2);
    b2.paint(g2);

    // Paints Paddle (separate paint method created in paddle class)
    paddle.paint(g2);

    // Paints Bricks based on current level (separate paint method created in brick class)
    // Bricks were created/stored in 2D Array, so drawn on screen through 2D Array as well.
    if (level == 1) {
    for (int x = 0; x < bricks.length; x++) {
    for (int i = 0; i < bricks[0].length; i++) {
    bricks[x][i].paint(g2);
    }
    }
    }
    if (level == 2) {
    for (int x = 0; x < bricks.length; x++) {
    for (int i = 0; i < bricks[0].length; i++) {
    bricks[x][i].paint(g2);
    }
    }
    }


    }


    Here is an example of a Paint Method in the Paddle Class:



    // Constructor
    public Paddle(int x, int y, int w, int h){

    xpos = x;
    ypos = y;
    width = w;
    height = h;

    r = new Rectangle(xpos, ypos, width, height);
    }

    public void paint(Graphics2D g2){

    g2.fill(r);
    }


    Output (What is shown on Screen):



    enter image description here



    Hope this helps you in drawing your circle!






    share|improve this answer






























      0














      In order to create an image in Java, you will first need to create a JPanel (essentially a window on your screen). It will look something like this (taken from javacodex.com):



      import java.awt.*;
      import java.awt.image.*;
      import java.io.*;
      import javax.swing.*;
      import javax.imageio.ImageIO;

      public class JPanelExample {

      public static void main(String arguments) throws IOException {

      JPanel panel = new JPanel();

      BufferedImage image = ImageIO.read(new File("./java.jpg"));
      JLabel label = new JLabel(new ImageIcon(image));
      panel.add(label);

      // main window
      JFrame.setDefaultLookAndFeelDecorated(true);
      JFrame frame = new JFrame("JPanel Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // add the Jpanel to the main window
      frame.add(panel);

      frame.pack();
      frame.setVisible(true);

      }
      }


      However, I am assuming that by "image," you mean a shape drawn out onto the screen instead (since you are using a paint method that draws a circle in your example). Then, you have your code in the correct method but need to implement a JPanel (since without a JPanel or JFrame you can't really display anything onto the screen graphics-wise).



      Here is an example taken from Board.java of my Atari Breakout mini-game:



      public class Board extends JPanel implements KeyListener {

      // Other Game Code

      public void paint(Graphics g) {
      super.paint(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setColor(Color.BLACK);

      // Shows lives left and score on screen
      g2.drawString("Lives left: " + lives, 400, 600);
      g2.drawString("Score: " + score, 400, 500);

      // Paints Walls (separate paint method created in wall class)
      topWall.paint(g2);
      bottomWall.paint(g2);
      leftWall.paint(g2);
      rightWall.paint(g2);

      // Paints 2 Balls (separate paint method created in ball class)
      b.paint(g2);
      b2.paint(g2);

      // Paints Paddle (separate paint method created in paddle class)
      paddle.paint(g2);

      // Paints Bricks based on current level (separate paint method created in brick class)
      // Bricks were created/stored in 2D Array, so drawn on screen through 2D Array as well.
      if (level == 1) {
      for (int x = 0; x < bricks.length; x++) {
      for (int i = 0; i < bricks[0].length; i++) {
      bricks[x][i].paint(g2);
      }
      }
      }
      if (level == 2) {
      for (int x = 0; x < bricks.length; x++) {
      for (int i = 0; i < bricks[0].length; i++) {
      bricks[x][i].paint(g2);
      }
      }
      }


      }


      Here is an example of a Paint Method in the Paddle Class:



      // Constructor
      public Paddle(int x, int y, int w, int h){

      xpos = x;
      ypos = y;
      width = w;
      height = h;

      r = new Rectangle(xpos, ypos, width, height);
      }

      public void paint(Graphics2D g2){

      g2.fill(r);
      }


      Output (What is shown on Screen):



      enter image description here



      Hope this helps you in drawing your circle!






      share|improve this answer




























        0












        0








        0







        In order to create an image in Java, you will first need to create a JPanel (essentially a window on your screen). It will look something like this (taken from javacodex.com):



        import java.awt.*;
        import java.awt.image.*;
        import java.io.*;
        import javax.swing.*;
        import javax.imageio.ImageIO;

        public class JPanelExample {

        public static void main(String arguments) throws IOException {

        JPanel panel = new JPanel();

        BufferedImage image = ImageIO.read(new File("./java.jpg"));
        JLabel label = new JLabel(new ImageIcon(image));
        panel.add(label);

        // main window
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("JPanel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // add the Jpanel to the main window
        frame.add(panel);

        frame.pack();
        frame.setVisible(true);

        }
        }


        However, I am assuming that by "image," you mean a shape drawn out onto the screen instead (since you are using a paint method that draws a circle in your example). Then, you have your code in the correct method but need to implement a JPanel (since without a JPanel or JFrame you can't really display anything onto the screen graphics-wise).



        Here is an example taken from Board.java of my Atari Breakout mini-game:



        public class Board extends JPanel implements KeyListener {

        // Other Game Code

        public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);

        // Shows lives left and score on screen
        g2.drawString("Lives left: " + lives, 400, 600);
        g2.drawString("Score: " + score, 400, 500);

        // Paints Walls (separate paint method created in wall class)
        topWall.paint(g2);
        bottomWall.paint(g2);
        leftWall.paint(g2);
        rightWall.paint(g2);

        // Paints 2 Balls (separate paint method created in ball class)
        b.paint(g2);
        b2.paint(g2);

        // Paints Paddle (separate paint method created in paddle class)
        paddle.paint(g2);

        // Paints Bricks based on current level (separate paint method created in brick class)
        // Bricks were created/stored in 2D Array, so drawn on screen through 2D Array as well.
        if (level == 1) {
        for (int x = 0; x < bricks.length; x++) {
        for (int i = 0; i < bricks[0].length; i++) {
        bricks[x][i].paint(g2);
        }
        }
        }
        if (level == 2) {
        for (int x = 0; x < bricks.length; x++) {
        for (int i = 0; i < bricks[0].length; i++) {
        bricks[x][i].paint(g2);
        }
        }
        }


        }


        Here is an example of a Paint Method in the Paddle Class:



        // Constructor
        public Paddle(int x, int y, int w, int h){

        xpos = x;
        ypos = y;
        width = w;
        height = h;

        r = new Rectangle(xpos, ypos, width, height);
        }

        public void paint(Graphics2D g2){

        g2.fill(r);
        }


        Output (What is shown on Screen):



        enter image description here



        Hope this helps you in drawing your circle!






        share|improve this answer















        In order to create an image in Java, you will first need to create a JPanel (essentially a window on your screen). It will look something like this (taken from javacodex.com):



        import java.awt.*;
        import java.awt.image.*;
        import java.io.*;
        import javax.swing.*;
        import javax.imageio.ImageIO;

        public class JPanelExample {

        public static void main(String arguments) throws IOException {

        JPanel panel = new JPanel();

        BufferedImage image = ImageIO.read(new File("./java.jpg"));
        JLabel label = new JLabel(new ImageIcon(image));
        panel.add(label);

        // main window
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("JPanel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // add the Jpanel to the main window
        frame.add(panel);

        frame.pack();
        frame.setVisible(true);

        }
        }


        However, I am assuming that by "image," you mean a shape drawn out onto the screen instead (since you are using a paint method that draws a circle in your example). Then, you have your code in the correct method but need to implement a JPanel (since without a JPanel or JFrame you can't really display anything onto the screen graphics-wise).



        Here is an example taken from Board.java of my Atari Breakout mini-game:



        public class Board extends JPanel implements KeyListener {

        // Other Game Code

        public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);

        // Shows lives left and score on screen
        g2.drawString("Lives left: " + lives, 400, 600);
        g2.drawString("Score: " + score, 400, 500);

        // Paints Walls (separate paint method created in wall class)
        topWall.paint(g2);
        bottomWall.paint(g2);
        leftWall.paint(g2);
        rightWall.paint(g2);

        // Paints 2 Balls (separate paint method created in ball class)
        b.paint(g2);
        b2.paint(g2);

        // Paints Paddle (separate paint method created in paddle class)
        paddle.paint(g2);

        // Paints Bricks based on current level (separate paint method created in brick class)
        // Bricks were created/stored in 2D Array, so drawn on screen through 2D Array as well.
        if (level == 1) {
        for (int x = 0; x < bricks.length; x++) {
        for (int i = 0; i < bricks[0].length; i++) {
        bricks[x][i].paint(g2);
        }
        }
        }
        if (level == 2) {
        for (int x = 0; x < bricks.length; x++) {
        for (int i = 0; i < bricks[0].length; i++) {
        bricks[x][i].paint(g2);
        }
        }
        }


        }


        Here is an example of a Paint Method in the Paddle Class:



        // Constructor
        public Paddle(int x, int y, int w, int h){

        xpos = x;
        ypos = y;
        width = w;
        height = h;

        r = new Rectangle(xpos, ypos, width, height);
        }

        public void paint(Graphics2D g2){

        g2.fill(r);
        }


        Output (What is shown on Screen):



        enter image description here



        Hope this helps you in drawing your circle!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 3:07

























        answered Nov 14 '18 at 3:00









        Henry WangHenry Wang

        358




        358






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291767%2fdraw-image-2d-graphics%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues