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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:55:31+00:00 2026-05-27T06:55:31+00:00

I am trying to paint a cube on a JFrame. Sounds simple, but lags

  • 0

I am trying to paint a cube on a JFrame.

Sounds simple, but lags a lot. The 7th and 8th lines usually flash pretty bad.

here is the code:

http://pastebin.com/ncDasST6

if someone can give me a hint or two on how to stop this lag from occurring, that would be great :D.

Originally was for Applet, but i wanted it to execute through a .jar file.

Also, any way to add an Applet to a JFrame?

I tried doing: add(new Rotational()); //name of JApplet it is based off of.

Thanks, Fire

  • 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-05-27T06:55:32+00:00Added an answer on May 27, 2026 at 6:55 am

    Does this variant work to your expectation? There are a number of changes which I did not bother to document (as I was ‘just playing’ with the code). Do a diff. to reveal the extent and nature of the changes.

    It shows no lag or rendering artifacts here at 700×700.

    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.border.EmptyBorder;
    
    public class Square extends JPanel implements MouseListener,
            MouseMotionListener {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        JFrame f = new JFrame("Cube Rotational");
                        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        Square square = new Square();
                        square.setBorder(new EmptyBorder(5,5,5,5));
                        f.setContentPane(square);
                        f.pack();
                        f.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public Square() {
            init();
            setPreferredSize(new Dimension(700,700));
        }
    
        class Point3D {
            public int x, y, z;
    
            public Point3D(int X, int Y, int Z) {
                x = X;
                y = Y;
                z = Z;
            }
        }
    
        class Edge {
            public int a, b;
    
            public Edge(int A, int B) {
                a = A;
                b = B;
            }
        }
    
        static int width, height;
        static int mx, my;
    
        static int azimuth = 45, elevation = 45;
    
        static Point3D[] vertices;
        static Edge[] edges;
    
        public void init() {
    
            width = 500;
            height = 500;
    
            vertices = new Point3D[8];
            vertices[0] = new Point3D(-1, -1, -1);
            vertices[1] = new Point3D(-1, -1, 1);
            vertices[2] = new Point3D(-1, 1, -1);
            vertices[3] = new Point3D(-1, 1, 1);
            vertices[4] = new Point3D(1, 1, -1);
            vertices[5] = new Point3D(1, 1, 1);
            vertices[6] = new Point3D(1, -1, -1);
            vertices[7] = new Point3D(1, -1, 1);
            edges = new Edge[12];
            edges[0] = new Edge(0, 1);
            edges[1] = new Edge(0, 2);
            edges[2] = new Edge(0, 6);
            edges[3] = new Edge(1, 3);
            edges[4] = new Edge(1, 7);
            edges[5] = new Edge(2, 3);
            edges[6] = new Edge(2, 4);
            edges[7] = new Edge(3, 5);
            edges[8] = new Edge(4, 5);
            edges[9] = new Edge(4, 6);
            edges[10] = new Edge(5, 7);
            edges[11] = new Edge(6, 7);
    
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addMouseListener(this);
            addMouseMotionListener(this);
            setVisible(true);
        }
    
        void drawWireframe(Graphics g) {
            double theta = Math.PI * azimuth / 180.0;
            double phi = Math.PI * elevation / 180.0;
            float cosT = (float) Math.cos(theta);
            float sinT = (float) Math.sin(theta);
            float cosP = (float) Math.cos(phi);
            float sinP = (float) Math.sin(phi);
            float cosTcosP = cosT * cosP;
            float cosTsinP = cosT * sinP;
            float sinTcosP = sinT * cosP;
            float sinTsinP = sinT * sinP;
            Point[] points;
            points = new Point[vertices.length];
            float scaleFactor = (getWidth() + getHeight()) / 8;
            float near = (float) 6;
            float nearToObj = 1.5f;
            for (int j = 0; j < vertices.length; ++j) {
                int x0 = vertices[j].x;
                int y0 = vertices[j].y;
                int z0 = vertices[j].z;
                float x1 = cosT * x0 + sinT * z0;
                float y1 = -sinTsinP * x0 + cosP * y0 + cosTsinP * z0;
                float z1 = cosTcosP * z0 - sinTcosP * x0 - sinP * y0;
                x1 = x1 * near / (z1 + near + nearToObj);
                y1 = y1 * near / (z1 + near + nearToObj);
                points[j] = new Point(
                        (int) (getWidth() / 2 + scaleFactor * x1 + 0.5),
                        (int) (getHeight() / 2 - scaleFactor * y1 + 0.5));
            }
            g.setColor(Color.black);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.white);
            for (int j = 0; j < edges.length; ++j) {
                int x1 = points[edges[j].a].x;
                int x2 = points[edges[j].b].x;
                int y1 = points[edges[j].a].y;
                int y2 = points[edges[j].b].y;
                ((Graphics2D) g).setStroke(new BasicStroke(5));
                g.drawLine(x1, y1, x2, y2);
            }
        }
    
        public void mouseEntered(MouseEvent e) {
        }
    
        public void mouseExited(MouseEvent e) {
        }
    
        public void mouseClicked(MouseEvent e) {
        }
    
        public void mousePressed(MouseEvent e) {
            mx = e.getX();
            my = e.getY();
            e.consume();
        }
    
        public void mouseReleased(MouseEvent e) {
        }
    
        public void mouseMoved(MouseEvent e) {
        }
    
        public void mouseDragged(MouseEvent e) {
            int new_mx = e.getX();
            int new_my = e.getY();
            azimuth -= new_mx - mx;
            azimuth %= 360;
            elevation += new_my - my;
            elevation %= 360;
            repaint();
            mx = new_mx;
            my = new_my;
    
            repaint();
            e.consume();
        }
    
        @Override
        public void paintComponent(Graphics g) {
            drawWireframe(g);
        }
    }
    

    Originally was for Applet, but i wanted it to execute through a .jar file.

    Good idea converting an applet to something more sensible, but note that an applet can (and usually should) be packed into a Jar.

    Also, any way to add an Applet to a JFrame?

    This is possible, relatively easy with this code (barring mixing Swing (JFrame) & AWT (Applet) components), but not the best way to go. It is better to create a hybrid like (for example) the subway applet/application.

    By moving the custom rendering from the frame to a JPanel, the code has been partially transformed into a hybrid, since the panel can be added to a frame or applet (or window or dialog, or another panel or..).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to paint a simple bar chart via C# but I've never experimented
I'm trying to write a simple paint applet with Java, but I'm having trouble
I'm trying to capture the screen and then paint the image to a JFrame
I'm trying to understand how I can paint simple graphics in x86 protected mode
I'm trying to paint a rectangle on my application in a red shade but
I'm building a simple chess game and am stuck on trying to paint drawings
I'm trying to paint some single rows in a listview but I don't know
I'm trying to make a paint in java, with classes and hierarchy. But my
I'm trying to create a simple game, but I can't find a certain memory
I'm trying to make simple paint application, based on Apple's GLPaint. To draw a

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.