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 7728895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:49:58+00:00 2026-06-01T05:49:58+00:00

public class TerrisView extends JFrame { public Canvas canvas; public TerrisView(String title) { super(title);

  • 0
public class TerrisView extends JFrame {
    public Canvas canvas;

    public TerrisView(String title) {
        super(title);
        canvas = new Canvas();
        canvas.setSize(300, 400);
        canvas.setBackground(Color.WHITE);
        // setSize(300, 400);
        this.add(canvas, BorderLayout.CENTER);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        paint();

    }

    public void paint() {

        // this.createBufferStrategy(2);
        // Graphics gp=getBufferStrategy().getDrawGraphics();
        // gp.setColor(Color.RED);
        // gp.fillRect(100, 100, 50, 50);
        // getBufferStrategy().show();
        Graphics gp = canvas.getGraphics();
        gp.setColor(Color.BLACK);
        gp.fillRect(0, 0, 10, 10);

    }

}

Why does it fail to draw the Rect on the canvas? What is wrong with the code?

  • 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-01T05:50:00+00:00Added an answer on June 1, 2026 at 5:50 am
    • Don’t mix Swing with AWT components without good reason.
    • In this case:
      1. Extend JComponent instead of Canvas
      2. Override paintComponent(Graphics) instead of paint(Graphics)

    Before I formatted that code, I failed to notice that paint() was not an override, and this classic..

    Graphics gp = canvas.getGraphics();
    

    Don’t do that with components. Use the Graphics object that is passed to the methods mentioned in point 2, and paint when told to do so.


    This answer is already accepted, but I could not resist reviewing and cleaning up the source to make an SSCCE, as well as add a few more tweaks. See comments in code for further tips.

    import java.awt.*;
    import javax.swing.*;
    
    public class TerrisView {
    
        public JComponent canvas;
    
        public TerrisView(String title) {
            // Don't extend frame, just use one
            JFrame f = new JFrame(title);
            canvas = new JComponent() {
                @Override // check this is a real method
                public void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    // paint the BG - automatic for a JPanel
                    g.setColor(getBackground());
                    g.fillRect(0,0,getWidth(),getHeight());
    
                    g.setColor(Color.BLACK);
                    // make it dynamic, changing with the size
                    int pad = 10;
                    g.fillRect(pad, pad, getWidth()-(2*pad), getHeight()-(2*pad));
                }
            };
            // layout managers are more likely to respect the preferred size
            canvas.setPreferredSize(new Dimension(300, 400));
            canvas.setBackground(Color.ORANGE);
            f.add(canvas, BorderLayout.CENTER);
            f.pack();
            // nice tweak
            f.setMinimumSize(f.getSize());
            // see http://stackoverflow.com/a/7143398/418556
            f.setLocationByPlatform(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            // start/alter Swing GUIs on the EDT
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new TerrisView("Terris View");
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
public class FacebookConnect extends Activity { Facebook facebook = new Facebook(); String FILENAME =
public class InterruptedInput { public static void main(String[] args) { InputThread th=new InputThread(); //worker
public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*
public class BobDatabase extends SQLiteOpenHelper{ private static final String DATABASE_NAME = bob.db; private static
public class Base{ protected String str; public static final Base ERROR = new Base(error);
public class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ JFileChooser chooser= new JFileChooser(); String
public class HomeActivity extends Activity{ // public ArrayList<User> users1 = new ArrayList<User>(); @Override public
public class Buddy { public string[] Sayings; } Buddy one = new Buddy(); one.Sayings
public class PackageTabActivity extends ListActivity{ HashMap<String,Object> hm ; ArrayList<HashMap<String,Object>> applistwithicon ; private static final

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.