jpanel

Is there a better way to set an initial position for a JPanel graphic?)

被刻印的时光 ゝ 提交于 2019-12-01 23:37:25
In Chapter 15 of Liang's Intro to Java Programming (7th ed.) , he introduces a program to make a (2-D) ball on a JPanel and enlarge it upon clicking enlarge/shrink buttons. I've modified the program so that it also 1) enlarges/shrinks the ball if the user clicks/option+clicks, 2) allows you to pick the color of the ball by pressing a button, and 3) allows you to move the circle by dragging it with your mouse. The last modification is what was giving me trouble for a while, because I wanted to center the ball at the beginning, but then allow the user to move the ball around with the mouse. The

How can I replace one of two JPanels with another JPanel in Java?

邮差的信 提交于 2019-12-01 23:09:56
I designed an interface for the welcome screen with one JFrame included two JPanels (JPanel1 on right and JPanel2 on left). The buttons on the left is to switch the Panels in JPanel1. I want to press on a button to replace JPanel1 content with another JPanel but I don`t know how. Please help. Here is a very simple example of something that should approximate your description. On the left, we have a hug button to toggle the content of the right panel. On the right, you have a panel with a given border and a label. When you press the button, the content on the right is swapped with the other

Drawing the Quadratic formula with sliders on a JPanel

三世轮回 提交于 2019-12-01 22:51:39
So, I'm trying to make a program where you can input the quadratic formula (ax^2+bx+c) via sliders. Then it draws a graph as you adjust for A, B, and C. Issues: I want the stuff I wrote in super paint and the sliders to be in one place. The sliders are in place when I run it. There's space with the correct background where I want my graph in the panel but no actual graph. Here's my driver class: import java.awt.*; import javax.swing.*; public class quadraticslider { public static void main (String[] args) { JFrame frame = new JFrame ("Quadratic Slider"); frame.setDefaultCloseOperation (JFrame

JPanel not showing background image

本小妞迷上赌 提交于 2019-12-01 22:50:26
It is very simple program and i have tried my best but the JPanel doesn't come up with a background image. I just want a simple background image on my panel. Here is my code: import javax.swing.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.*; import java.awt.Graphics; class PanelEx extends JPanel { BufferedImage img; PanelEx() { try { img = ImageIO.read(new File("C:/Users/Pictures/s_4261.jpg")); } catch(Exception ex) { ex.printStackTrace(); } } public void printComponent(Graphics g) { g.drawImage(img,0,0,null); } } class JPanelEx2 extends JFrame { PanelEx

Scrolling limitation with JScrollPane and JViewport maximum sizes smaller than contents

Deadly 提交于 2019-12-01 22:33:30
I have a JFrame containing a JScrollPane containing a JPanel . The JPanel contains a bunch of JTextArea s. I'm loading a lot of text into them (about 8k-10k characters). The layout works fine, though the scrolling is a bit laggy. The real issue is that it seems JPanel , JScrollPane and JViewport have a hard 32767 size limit, so when any JTextArea grows higher than that, it can't be scrolled any further to show the last 1/3 of the text. Below you can see a minimal example for the problem. I used the NetBeans JFrame designer so it might be a bit lengthy but the only thing I have changed from the

Swing component setSize()/setBounds() issue

萝らか妹 提交于 2019-12-01 22:06:51
问题 Contents Overview Example Code Screenshots of Problem 1. Overview of problem So I'm writing a GUI for a complicated program I'm developing, and I get tired of trying to get components to scale correctly when the window is resized. At first I was using several layouts inside the jframe, and each jpanel to try and place the components correctly and scale them appropriately. Naturally, I got fed up with them, and I started trying to scale and set the x,y positions of the components dynamically

Scaling and zoom

爱⌒轻易说出口 提交于 2019-12-01 22:03:13
I need to implement zoom for a JDesktopPane contained in a JScrollPane . I have had prior success zooming by overriding the paintComponent(...) method and calling scale(double,double) . This is not working properly: the JInternalFrame 's and JPanel 's scale as intended, but the MouseListener 's for the JLabel 's and such register at the pre-scaled locations. What can I do? Thank you for reading. trashgod ScaledPanel shows how to scale mouse coordinates using explicit transformation methods: scaleX , scaleY , unScaleX and unScaleY . Alternatively, you can use an inverse transformation, as shown

jmathplot doesn't work in swing application

给你一囗甜甜゛ 提交于 2019-12-01 21:57:53
I'm trying to use the jmathplot library in a swing application. Problem is, it doesn't seem to work when I add it to a JPanel as follows: // create your PlotPanel (you can use it as a JPanel) double[] x = new double[] { 0, 1, 2, 3, 4, 5 }; double[] y = new double[] { 10, 11, 12, 14, 15, 16 }; // create your PlotPanel (you can use it as a JPanel) Plot2DPanel graph = new Plot2DPanel(); graph.setBounds(0, 0, 782, 272); // add a line plot to the PlotPanel graph.addLinePlot("my plot", x, y); JPanel panel = new JPanel(); panel.setBounds(10, 333, 782, 272); tab_analysis.add(panel); panel.setLayout

MouseEvent on JPanel - wrong coordinate

我只是一个虾纸丫 提交于 2019-12-01 21:45:07
问题 I have written the following micro-paintbrush program in Java: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; class AuxClass1 extends JFrame

Do I actually call the paintComponent method I make when creating a rectangle in Java?

橙三吉。 提交于 2019-12-01 21:39:37
This is my current RectangleComponent class and I add it to a panel in my main JFrame but it never appears. I thought it wasn't drawing so I decided to call the paintComponent method in the Rectangle's constructor, and after sorting through 4-5 nullPointerExceptions, nothing has changed. I've read multiple guides on how to draw rectangles and I have seen multiple code examples, but I can never get the panels to work with more than one JComponent. If you could, please take a brief look at my code and see if you can devise a solution. Thank you for your time. Also listed is the Frame I call the