jpanel

Clicking on a JPanel to draw shapes

百般思念 提交于 2019-12-02 01:43:01
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 probably a small error but I just can't seem to find it. Shape: public class Shape extends JPanel {

Add transparent JPanel upon AWT Component to paint on

假装没事ソ 提交于 2019-12-02 01:17:30
I've got a Problem: In my Java application I've got an AWT Component (cannot change that one) that streams and shows an avi-file. Now I want to draw upon this movie and thought about putting a transparent JPanel above it and draw on that one. This does not work since I either see the avi-stream or the drawn lines but not both. I read somewhere that AWT does not support transparency of components - but the panel is a JPanel which is able to do so. Can someone please help me with this one - thanks in advance. The Mixing Light and Heavyweight Components article explains how this is handled only

Adding oval shape to JPanel

安稳与你 提交于 2019-12-02 00:56:40
Here's my simple code. I don't really know how to add a drawn oval to a JPanel . I did some paintings before, but I have never used the constructor so I don't have an idea. public class Buffer extends JPanel{ public JFrame frame; public JPanel panel; public Buffer(){ frame=new JFrame(); panel=new JPanel(); panel.setSize(500,500); panel.setBackground(Color.red); frame.setSize(500,500); frame.setVisible(true); frame.add(panel); } public void paintComponent(Graphics g){ super.paintComponents(g); g.fillOval(20,20,20,20); } public static void main(String args[]){ new Buffer(); } } The basic

Scaling and zoom

南笙酒味 提交于 2019-12-02 00:50:17
问题 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. 回答1: ScaledPanel shows how to scale mouse coordinates using explicit transformation methods:

When creating a BufferedImage from a JPanel (w/o a JFrame), can I also use a layout manager?

僤鯓⒐⒋嵵緔 提交于 2019-12-02 00:49:57
I am trying to create a BufferedImage from a JPanel, without using a JFrame. Yesterday, I was finally able to get the image to appear with help from this community (see below), but now I'm having some layout trouble. All of the components on the BufferedImage begin drawing at 0,0, instead of following the layout manager. Does anyone know of a solution for this problem? Yesterday's question: Can I create a BufferedImage from a JPanel without rendering in a JFrame? In the code below, the two labels will overwrite each other in the top left corner of the image. import java.awt.Color; import java

Switching between JPanels

折月煮酒 提交于 2019-12-02 00:40:36
问题 I'm trying to make a game. There's several different screens in the game such as a main menu, and the actual game screen. Each of these is a separate jpanel extension. I have added each of them to my JFrame which is a class called Game. In my game class I have the following methods public void addPanel( JPanel p ) { panels.add( p ); // An array of all the different panels I need this.getContentPane().add( p ); } public void switchToPanel( JPanel p ) { for ( JPanel somePanel : panels ) {

JPanel with background image, with other panels overlayed

醉酒当歌 提交于 2019-12-02 00:10:28
问题 I want to have a JPanel which uses an image as a background, with this I want to add new panels to this panels so that they sit on top of this background image. I have tried the following: Image background; public Table(){ super(); ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTable.png")); background = ii.getImage(); setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (background != null)

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

孤人 提交于 2019-12-02 00:04:11
问题 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

Alignment issue in GridBagLayout

廉价感情. 提交于 2019-12-01 23:57:48
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestForm extends JFrame { private JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel,bfPercentageLabel; private JTextField heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt; private JPanel centerPanel; private JPanel southPanel; private JLabel endTargetWeightLabel; private JLabel endTargetWeightResultLabel; private JLabel fatMustLoseLabel; private JLabel fatMustLoseResultLabel; public TestForm() { //Declaring instance variables heightLabel = new JLabel("Height:

Scrolling limitation with JScrollPane and JViewport maximum sizes smaller than contents

别来无恙 提交于 2019-12-01 23:42:51
问题 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