Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9053747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:30:17+00:00 2026-06-16T13:30:17+00:00

I was trying to do my computer science homework but I am stuck as

  • 0

I was trying to do my computer science homework but I am stuck as I was trying to use the following methods.

  1. public Graphics create(int x,int y,int width,int height)

    That Creates a new Graphics object based on this Graphics object, but with a new translation and clip area.

    Parameters:

    • x – the x coordinate.
    • y – the y coordinate.
    • width – the width of the clipping rectangle.
    • height – the height of the clipping rectangle.
       
  2. public abstract void translate(int x,int y)

    That Translates the origin of the graphics context to the point (x, y) in the current coordinate system.

Can anyone explain and give examples of how to use them?

i was trying to do this..

public Graphics drawPlayer1()
{
    myPencil.up();
    myPencil.move(-620,300);
    myPencil.down();
    myPencil.fillCircle(20);
    myPencil.up();
    myPencil.move(-590,300);
    myPencil.drawString("Player1: " + player1);
    p1.create(-620,300,40,40);
    return p1;
}//end drawPlayer1

and it threw me a nullPointerException when it comes to p1.create(-620,300,40,40);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:30:18+00:00Added an answer on June 16, 2026 at 1:30 pm

    I’m with Andrew on this one, I’ve never used Graphics#create(int, int, int, int). I do use Graphics#create though.

    Basically, the create method will create a new graphics context which is a copy of the original. This allows you to manipulate the copy with out effecting the original. This is important if you are performing operations on the graphics that can’t be (easily) undone.

    Translate simple “zeros” the graphics context to the new location. The Swing painting process does this for each component it paints. Basically, before paint is called, the graphics context is translated to the components position, meaning that all painting within the component is done from 0x0

    enter image description here

    public class TestGraphics01 {
    
        public static void main(String[] args) {
            new TestGraphics01();
        }
    
        public TestGraphics01() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestGraphicsPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestGraphicsPane extends JPanel {
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                FontMetrics fm = g.getFontMetrics();
    
                // This creates a "copy" the graphics context, it's translated
                // to the x, y position within the current graphics context
                // and has a width and height.  If the width or height is outside
                // the current graphics context, then it is truncated...
                // It's kind of like clip, except what ever you do to this copy
                // does not effect the graphics context it came from...
                // This would be simular to setting the clipping region, just it 
                // won't effect the parent Graphics context it was copied from...
                Graphics create = g.create(100, 100, 200, 200);
                create.setColor(Color.GREEN);
                create.fillRect(0, 0, 200, 200);
                create.setColor(Color.YELLOW);
                create.drawString("I'm inside...", 0, fm.getAscent());
                create.dispose();
    
                // But I remain uneffected...
                g.drawString("I'm outside...", 0, fm.getAscent());
    
                // I will effect every thing draw afterwards...
                g.setColor(Color.RED);
                int y = 50 - (fm.getHeight() / 2) + fm.getAscent();
                g.translate(50, y);
                g.drawString("I'm half way", 0, 0);
                // You must reset the translation if you want to reuse the graphics OR
                // you didn't create a copy...
                g.translate(-50, -y);
    
                y = 350 - (fm.getHeight() / 2) + fm.getAscent();
                g.translate(300, y);
                g.drawString("I'm half way", 0, 0);
                // You must reset the translation if you want to reuse the graphics OR
                // you didn't create a copy...
                g.translate(-300, -y);
    
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a computer science teacher trying to create a little gradebook for myself using
Basically I am on school computer trying to use gdb. But there are way
Well, I'm a beginner, it's my year as a computer science major. I'm trying
i'm trying to get computer's state in my LAN... thought about using QTcpSocket but
I'm trying to create a computer reservation system, where user chooses a computer and
I'm trying to install the android SDK on my computer, but a bunch of
I've taken an introductory course in Computer Science, but a short while back I
I'm trying to prepare for a future in computer science, so I started with
I'm a first year computer science student. I am trying to teach myself hash
I'm trying to write an algorithm for a number theory/computer science merged class that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.