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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:11:19+00:00 2026-05-27T16:11:19+00:00

The code below plots some simple x-y data, but it has two problems that

  • 0

The code below plots some simple x-y data, but it has two problems that I do not know how to fix.

First, it plots negative values for some of the data points, which means lines extending southward below the x-axis. Since the data points are selected at random, you may have to resize the frame a bit in order to view new random numbers to be plotted in a way that shows this bug. All data values will be positive, so I want all deflections to project northward above the blue bottom marker line, and I need to make sure that no deflections extend southward below the blue bottom marker line.

Second, the y-axis label takes up too much real estate on the screen. It needs to be rotated -90 degrees. However, all the examples I have seen for this involve rotating the entire panel using a graphics2d object. I do not want to rotate the entire panel. Instead, I just want to rotate the text of the y-axis label.

Can anyone show me how to change the code below to fix these two specific problems?

The code is in the following two files:

GUI.java

import java.awt.*;
import javax.swing.*;

class GUI{

GUI() { 
    // Create a new JFrame container. 
    JFrame jfrm = new JFrame("X-Y Plot"); 
    // Specify FlowLayout for the layout manager. 
    jfrm.getContentPane().setLayout(new FlowLayout()); 
    int frameHeight = 400;
    int frameWidth = 300;
    // Give the frame an initial size. 
    jfrm.setSize(frameWidth, frameHeight);

    // Terminate the program when the user closes the application. 
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create a text-based label.
    JVertLabel myVLabel = new JVertLabel("y-axis label");
    int width = myVLabel.WIDTH;

    PaintPanel myPP = new PaintPanel(frameWidth-width-50-20,frameHeight-70);
    jfrm.add(myPP);

    jfrm.add(myVLabel);// Add the label to the frame.

    // Display the frame. 
    jfrm.setVisible(true); 
} 

public static void main(String args[]) { 
    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater(new Runnable() {public void run(){new GUI();}});
}

    public class JVertLabel extends JComponent {

        private String text;

        public JVertLabel(String s) {
            text = s;
        }//constructor

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.rotate(Math.toRadians(-90));
            g2d.drawString(text, 0, 0);
        }
    }
}

PaintPanel.java

import java.awt.*;
import javax.swing.*;   
import java.util.*;

class PaintPanel extends JPanel {

    Insets ins; // holds the panel's insets 
    Random rand; // used to generate random numbers 

    PaintPanel(int w, int h) {
        setOpaque(true);// Ensure that panel is opaque.
        setPreferredSize(new Dimension(w, h));// Set preferred dimension as specfied. 
        rand = new Random();
    }

    protected void paintComponent(Graphics g) {// Override paintComponent() method.
        super.paintComponent(g);// Always call superclass method first.
        int height = getHeight();// Get height of component.
        int width = getWidth();// Get width of component.
        ins = getInsets();// Get the insets.
        // Get dimensions of text
        Graphics2D g2d = (Graphics2D) g;
        Font font = new Font("Serif", Font.PLAIN, 12);
        FontMetrics fontMetrics = g2d.getFontMetrics();
        String xString = ("x-axis label");
        int xStrWidth = fontMetrics.stringWidth(xString);
        int xStrHeight = fontMetrics.getHeight();
        String yString = "y-axis-label";
        int yStrWidth = fontMetrics.stringWidth(yString);
        int yStrHeight = fontMetrics.getHeight();
        int leftStartPlotWindow = ins.left + 5 + yStrWidth;
        int hPad = 3;

        // Fill panel by plotting random data in a bar graph. 
        for (int i = leftStartPlotWindow + hPad; i <= width - leftStartPlotWindow - hPad + yStrWidth + 1; i += 4) {
            int h = Math.abs(rand.nextInt(height - ins.bottom));//Get rand# betw/0 and max height of drawing area.
            // If generated value w/in or too close to border, change it to just outside border.
            if (h <= ins.top) {
                h = ins.top + 1;
            }
            g.drawLine(i, Math.abs(height - ins.bottom - xStrHeight - 5), i, h);// Draw a line that represents data.
        }
        g.setColor(Color.blue);
        g.drawRect(leftStartPlotWindow, ins.bottom + 2, width - leftStartPlotWindow - ins.right - hPad, height - xStrHeight - 6);
        g.setColor(Color.red);
        g.drawRect(ins.left, ins.bottom, width - ins.left - 1, height - ins.bottom - 1);
        g.drawString(xString, (width / 2) - (xStrWidth / 2), height - ins.bottom - 6);
        g.drawString(yString, ins.left, height / 2);
    }
}
  • 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-27T16:11:20+00:00Added an answer on May 27, 2026 at 4:11 pm

    All data values will be positive, so I want all deflections to project northward above the blue bottom marker line, and I need to make sure that no deflections extend southward below the blue bottom marker line.

    You need to calculate the random height so that all values fit into the space available. So the calculation would be something like:

    int randomHeight = panelHeight - offset.top - offset.bottom - heightForTheXAxisText;
    

    Then you don’t have to worry about negative values or the top of the line extending outside the bounds of the panel.

    all the examples I have seen for this involve rotating the entire panel using a graphics2d object. I do not want to rotate the entire panel. Instead, I just want to rotate the text of the y-axis label.

    Set the rotation of the Graphics object, the draw the text, then restore the rotation of the Graphics object back to 0.

    Or, you create create a new Graphcis object from the current Graphics object, then apply the rotation, draw the text and then dispose of the temporaray Graphics object.

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

Sidebar

Related Questions

The below GUI simply lists some data in a listbox, plots it, and gives
I have some data that is constrained below a 1:1 line. I would to
The code below works fine but, in the textbox the decimal value has this
I have a plot (sample code pasted below) that I am trying to add
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
The code below shows a sample that I've used recently to explain the different
The code below works. But if I comment out the line Dim objRequest As
The code below gives an error: Property 'Int32 Key' is not defined for type
We all know that Mathematica is great, but it also often lacks critical functionality.

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.