jpanel

Clicking on a JPanel to draw shapes

…衆ロ難τιáo~ 提交于 2019-12-20 03:30:19
问题 I have a JFrame containing 3 JPanels; Options, menu, canvas. In options there are a number of JButtons representing shapes. The aim is to click on the JButton of a shape e.g. rectangle, then click anywhere on the canvas and the shape will be drawn there. For some reason, the shape does not always get drawn, it is only drawn when I click somewhere in the top left area of the canvas. Also the shape seems to randomly change size depending on where I click. Here are some of my code snippets, it's

JPanel not showing background image

为君一笑 提交于 2019-12-20 03:20:02
问题 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

Window frame covering graphics content

不羁岁月 提交于 2019-12-20 02:26:07
问题 this is my first post here and I have a question that seems really nooby, but this has been troubling me for the past hour or so. I'm making a simple JFrame with a JPanel in it, but the Windows 7 border frame appears to be blocking my view of parts of the panel. For instance, if I draw a little square at coordinate 0,0, it will not appear and I suspect it's behind the window frame. I've tried messing around with pack, setsize, setpreferred size, setresizable, and different layouts, but I can

jmathplot doesn't work in swing application

若如初见. 提交于 2019-12-20 01:45:34
问题 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);

How to repaint a jpanel every x seconds?

浪子不回头ぞ 提交于 2019-12-19 20:48:08
问题 i would like to know how to repaint and update the background of a JPanel every x seconds...This is my code: package view; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class GamePanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; private final JLabel

Dynamically growing JPanel with BoxLayout (on a null layout)

独自空忆成欢 提交于 2019-12-19 12:07:21
问题 I have a JPanel with a vertical BoxLayout on top of a JPanel with a null layout. I would like the JPanel with the BoxLayout to grow as the components are being added. See this code: public static void main (String[] args) { JFrame f = new JFrame(); f.setSize(500,500); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel total = new JPanel(); total.setLayout(null); total.setSize(f.getWidth(),f.getHeight()); total.setBackground(Color.green); JPanel box = new JPanel(); box.setLocation(100

Making a single component full screen

随声附和 提交于 2019-12-19 11:28:04
问题 I'm trying to make a JPanel go full screen when you click a button, and back again when you press escape. I've managed to get the window to go full screen, but because of the whole thing about adding components removing them from other containers, I end up with a blank JPanel. I chose to make a separate JFrame to render full screen, the class of which is as follows (note that this is an inner class, so myPanel refers to a panel that already exists in MyJFrame): public class FullScreen extends

Lag spike when moving player

时光毁灭记忆、已成空白 提交于 2019-12-19 10:59:10
问题 The player is a panel, and it is getting removed, its position changed, and then re-added to another panel (which is what contains this method) which is drawn to the main frame. There are also a lot of other small panels containing a grass sprite being drawn to the primary panel as terrain tiles. I think the problem is that when I call revalidate() , it revalidates all those little panels as well. How can I solve this? EDIT: I should mention that I am using RelativeLayout to position the

Component on JPanel not showing when setLayout(null)

人盡茶涼 提交于 2019-12-19 10:59:08
问题 Someone can tell why the combobox is not showing ? I have a Controller: public class TestController extends JPanel { TestView cgView; public TestController() { setLayout(null); cgView=new TestView(); add(cgView); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame fr = new JFrame("testt"); fr.setSize(1200,1000); fr.setResizable(false); TestController cgc=new TestController(); fr.setBackground(Color.white); fr.setVisible(true); fr

Java - Custom Shape Panels?

China☆狼群 提交于 2019-12-19 10:51:57
问题 I am working on an application that involves the user requiring to hover over several moving dots on the screen in order to launch specific popups. At the moment, i am listening for mouseMoved events on the JPanel onto which the dots are rendered, and then launching the required pop ups whenever the cursor is within a specific distance of a dot. When i have hundreds of dots - this probably becomes quite expensive. Wouldnt the ideal solution be to represent my 'dots' as small components and