Add extended rectangle class to jpanel - java

I have a class, SheetGood, which extends Rectangle. At the moment I place these SheetGoods onscreen using absolute positions based off of the users resolution, but I'd like to let a layoutmanager take over this aspect.
To do so I'd like to add a SheetGood object to a JPanel, but can't as SheetGood does not extend JComponent.
Any ideas as to how I can get around this?
//edit//
Will I run into issues if I force my program to run at a certain size and remove resizing options?
Ie, a fixed size of 1280x1024 so I can continue placing SheetGoods how I have been and not have to worry about the other controls clipping them when their layout manager moves them around.

To use absolute positioning, dont use a layout manager. You should set layout to null.
I suggest that: extends JPanel as rectangle and set a background color, and set bounds to the positions you want to place.
static class MyRectangle extends JPanel {
int x,
y,
width,
height;
Color bg;
public MyRectangle(int x, int y, int width, int height, Color bg) {
super();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.bg = bg;
setBounds(x, y, width, height);
setBackground(bg);
}
}
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Test rectangle");
MyRectangle rect1 = new MyRectangle(10, 10, 90, 90, Color.red),
rect2 = new MyRectangle(110, 110, 90, 90, Color.yellow);
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setLayout(null); //to make things absolute positioning
contentPane.add(rect1);
contentPane.add(rect2);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

Related

JPanel paintComponent not working properly

I learned that if you override
protected void paintComponent(Graphics g)
You can get an image painted as the background of a class that extends javax.swing.JPanel.
In my code I have 2 instances of the same Class extending JPanel with almost exactly the same code just with a different position and background image in a second JPanel and while one gets the background the other one does not. Here is my code:
public class CardPanel extends JPanel {
private int x, y, width, height;
private BufferedImage background;
public CardPanel(int x, int y, int width, int height, BufferedImage background) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.background = background;
createCardPanel();
}
private void createCardPanel() {
setPreferredSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));
setMinimumSize(new Dimension(width, height));
setFocusable(false);
setOpaque(true);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, x, y, null);
}
}
And how I use it:
pCardPanel = new CardPanel();
dCardPanel = new CardPanel();
Declaring the CardPanels
private void createCardPanels(String imgPath) {
BufferedImage background = ImageLoader.loadImage(imgPath);
pCardPanel = new CardPanel(0, (height - Card.CARD_HEIGHT), width, Card.CARD_HEIGHT, background.getSubimage(0, (height - Card.CARD_HEIGHT), width, Card.CARD_HEIGHT));
dCardPanel = new CardPanel(0, 0, width, Card.CARD_HEIGHT, background.getSubimage(0, 0, width, Card.CARD_HEIGHT));
this.add(pCardPanel, BorderLayout.SOUTH);
this.add(dCardPanel, BorderLayout.NORTH);
}
Method for creating and adding the CardPanels
createCardPanels("/textures/background.png");
Using the method
public void addCardImage(BufferedImage img, boolean playerCard) {
JLabel imgLabel = new JLabel();
ImageIcon icon;
icon = new ImageIcon(img);
imgLabel.setIcon(icon);
cardImages.add(imgLabel);
if (playerCard)
pCardPanel.add(imgLabel);
else
dCardPanel.add(imgLabel);
display.pack();
}
This last method is called for adding Card Images to te panel, this part works. Now to my problem:
this is how it looks. (there are some other flaws like the card position but this will be a later issue I can fix myself)
As you can see, the panel on the bottom (pCardPanel) has no background image. Any ideas why it might be this way? Thanks in advance
You can get an image painted as the background of a class that extends javax.swing.JPanel
A background generally implies that the image fills the entire panel and the size of the panel is the same as the size of the image. Therefore when you paint the image the code should be:
g.drawImage(background, 0, 0, null);
So the image is always painted at the top left of the panel.
When the panel is added to the frame, the layout manager will set the location of the panel.
just with a different position
pCardPanel = new CardPanel(0, (height - Card.CARD_HEIGHT),
I would guess the problem is the "y" value is outside the size of the panel, so you don't see the image.
That is your preferred size does not account for the fact that you are attempting to paint the image at some location other than (0, 0) in which case the preferred size should be something like:
setPreferredSize(new Dimension(x + width, y + height));
However, you don't want to do that, since each component should be independent of other components. It should not know or care that you are trying to position two panels above/below one another. It should just worry about painting its own image and let the layout manager worry about setting the location of each panel.
So what you really want to do is just paint the image at (0, 0) and let the layout manager determine the location of the panel.
You are already using the BorderLayout. So it is the job of the layout manager to set the location of the component in the "SOUTH" to some non-zero "y" value.

Aren't Graphics/JPanel dimensions measured in pixels?

When I draw [drawRect(x, y, width, height)] a rectangle on an JPanel inside a JFrame, that has a width of e.g. 500, it is actually wider than 500 Pixels on my Screen. How is this measured?
Whilst messing around with a drawing Rectangles on an JPanel and the size of the JFrame around this, i recognized, that 500 "width" are different things, when it comes to JFrame and JPanel.
A JFrame that is created with a width of 1920 Pixels is exactly 1920 Pixels wide, means, as wide as my screen (1920x1080).
If i draw a rectangle with a width of 1920 on a JPanel, that is inside the JFrame, it extends my screen by exactly 385 Pixels. Respectively: a drawn rectangle as wide as my screen needs a width of 1535.
import javax.swing.*;
import java.awt.*
public class Main {
public static void main(String[] args){
JFrame window = new JFrame();
window.setSize(1920,1080); //Window as wide as the screen
window.add(new Canvas());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
public class Canvas extends JPanel {
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(0, 0, 1920, 500); //Paints a rectangle on the JPanel
}
}
The window that opens is exactly as wide as my screen, but the rectangle inside extends it.
If I change the width of the rectangle to 1535 [drawRect(0, 0, 1535, 500)], it is as wide as the JFrame/screen. Why is that?
Edit: Since the Windows 10 Frame has no decorations at the side, just the standard menu-bar on top, I don't think this is the problem (as far as I understand decorations).
The short answer: Yes they are.
The explanation: Let us look deeper!
Running Java Swing on MacOS (tested with Metal LAF), the JFrame has insets of zero for left and right. This is similar to rendering for certain themes on Windows 10. I have included code below; the gap between the content pane and the panel's fill rectangle should remain 8 pixels. When the program is running, resize it, and check for yourself. Feel free to comment if this is not the behaviour you experience.
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
final String name;
name = javax.swing.plaf.metal.MetalLookAndFeel.class.getName();
try {
UIManager.setLookAndFeel(name);
}
catch (Throwable e) {
e.printStackTrace();
}
createAndShowWindow();
});
}
private static void createAndShowWindow() {
final int width = 1920;
final int height = 800;
final int padding = 8;
JFrame window = new JFrame();
window.setTitle("Hello World.");
window.setPreferredSize(new Dimension(width, height));
window.setSize(width, height); //Window as wide as the screen
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Canvas canvas = new Canvas(padding);
window.add(canvas, BorderLayout.CENTER);
window.pack();
window.setVisible(true);
System.out.println("w: " + window.getSize());
System.out.println("c: " + window.getContentPane().getSize());
System.out.println("p: " + canvas.getSize());
System.out.println("i: " + window.getInsets());
}
public static class Canvas extends JPanel {
private final int padding;
public Canvas(int padding) {
this.padding = padding;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(180, 120, 16));
//Paints a rectangle on the JPanel
int x = padding;
int y = padding;
int w = getWidth() - 2 * padding;
int h = getHeight() - 2 * padding;
g.fillRect(x, y, w, h);
}
}

Java can't focus on JTextField inside JPanel inside JPanel as viewport in JScrollPane

I have to admit, maybe the question seems strange how is written, but I'll explain here:
I have a JScrollPane, in which i correctly add a JPanel, let'sa say that for now JScrollPane variable is "JSP" and JPanel is "JP"
In my JP i have an ArrayList of JPanel (let's call it AJP) which i can control visibility from outside the JSP, i can control this visibility by clicking different buttons, every button is "linked" with a number so if i click button1, the first AJP element get visibility set to true, and all the others set to false.
Every of these AJP elements has a different elements inside, so for example, AJP at first position has two JTextFields and 1 JButton, AJP at position 2 has 1 JTextField only.
The fact is that seems that i can't click a JButton or edit a JTextField, like the mouse can't focus them, I'll post here some code
This is the class which contains the JP and it's an extension of JScrollPane
private int x, y, width, height;
private JPanel internalPanel = new JPanel();
private ArrayList<KPanel> kPanels = new ArrayList<KPanel>();
JViewport viewport = new JViewport();
public KScrollPanel(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.internalPanel.setBorder(new LineBorder(Color.black, 1));
this.internalPanel.setSize(new Dimension(this.width - 10, this.height - 10));
this.internalPanel.setPreferredSize(new Dimension(this.width - 10, this.height - 10));
this.setSize(new Dimension(this.internalPanel.getWidth() + 10, this.internalPanel.getHeight() + 10));
this.setPreferredSize(this.internalPanel.getSize());
this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
viewport.add(this.internalPanel);
}
This other is the class which pratically is composed the ArrayList in the class KScrollPanel i just posted, as you can see, the method initialize adds dinamically the elements (KButton and KTextFields are two classes extendind JButton and JTextField)
This class of course is an extension of JPanel
private int x, y, width, height;
KTextField textfield_nodeName, textfield_relationshipName;
KButton button_saveNode, button_saveRelationship;
public KPanel(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
initGUI();
}
public KPanel() {
}
public void initGUI() {
this.setBounds(this.x, this.y, this.width, this.height);
this.setBorder(new LineBorder(Color.gray, 1));
}
public void initialize(String type) {
switch(type) {
case "Node":
textfield_nodeName = new KTextField(5, 5, 200, 30);
this.add(textfield_nodeName);
button_saveNode = new KButton(5, 35, 200, 30, "Save Node");
this.add(button_saveNode);
break;
case "Relationship":
textfield_relationshipName = new KTextField(5, 5, 250, 30);
this.add(textfield_relationshipName);
break;
}
this.revalidate();
this.repaint();
}
Why are you extending JScrollPane?
Why are you creating a JViewport?
Why are you createing an internal JPanel to add to the viewport?
You should not be attempting to play with the sizes of any of your components. The layout manager will determine the size of each component.
The component you want to add to the scrollpane should be created separately from the scrollpanel. You can then set the Border of this component when you create it.
JViewport viewport = new JViewport();
...
viewport.add(this.internalPanel);
So it looks to me like you create an internal panel and add it to the viewport but you never add the viewport to the scroll pane, so there are no components to display.
So my suggestion is to get rid of the custom JScrollPane class then is no need to extend it because you are not adding any functionality to the class. Just use a JScrollPane the way it was designed to be used:
JPanel internalPanel = new JPanel();
internalPanel.setBorder(...);
internalPanel.add(...);
JScrollPane scrollPane = new JScrollPane( internalPanel );
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

how to get the image of a Swing widget? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Java Swing : Obtain Image of JFrame
I am working on a little drag-and-drop Java GUI builder. It works so far, but the widgets I'm dragging and dropping are just rectangles I'm dynamically drawing on a canvas.
If I have a rectangle that represents a widget like a JButton, is there a way for me to create a JButton, set the size and get the image of that JButton if it was drawn on the screen? Then I could paint the image to the screen instead of just my boring rectangle.
For example, I'm currently doing this to draw a (red) rectangle:
public void paint(Graphics graphics) {
int x = 100;
int y = 100;
int height = 100;
int width = 150;
graphics.setColor(Color.red);
graphics.drawRect(x, y, height, width);
}
How can I do something like:
public void paint(Graphics graphics) {
int x = 100;
int y = 100;
int height = 100;
int width = 150;
JButton btn = new JButton();
btn.setLabel("btn1");
btn.setHeight(height); // or minHeight, or maxHeight, or preferredHeight, or whatever; swing is tricky ;)
btn.setWidth(width);
Image image = // get the image of what the button will look like on screen at size of 'height' and 'width'
drawImage(image, x, y, imageObserver);
}
Basically, you'll paint your component to an image, and then paint that image wherever you want. In this case it's okay to call paint directly because you're not painting to the screen (but to a memory location).
If you wanted to optimize your code more than I've done here, you can save the image, and just repaint it in a different location whenever it's moved (instead of calculating the image from the button every time the screen repaints).
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class MainPanel extends Box{
public MainPanel(){
super(BoxLayout.Y_AXIS);
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
// Create image to paint button to
BufferedImage buttonImage = new BufferedImage(100, 150, BufferedImage.TYPE_INT_ARGB);
final Graphics g2d = buttonImage.getGraphics();
// Create button and paint it to your image
JButton button = new JButton("Click Me");
button.setSize(button.getPreferredSize());
button.paint(g2d);
// Draw image in desired location
g.drawImage(buttonImage, 100, 100, null);
}
public static void main(String[] args){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPanel());
frame.pack();
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

centre an image in the middle of a panel

I am coming to grips with Graphics in Java and created a circle on a JPanel.
How would a center the circle in the JPanel?
package exerciseninetwo;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class ExerciseNineTwo extends JFrame
{
public ExerciseNineTwo()
{
super("My Frame");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new CanvasPanel());
setVisible(true);
}
public static void main(String[] args)
{
new ExerciseNineTwo();
}
}
class CanvasPanel extends JPanel
{
CanvasPanel()
{
setSize(120, 120);
//setBackground(Color.cyan);
}
protected void paintComponent(Graphics g)
{
Graphics2D comp = (Graphics2D)g;
Ellipse2D circle = new Ellipse2D.Float(200, 200, 200, 200);
comp.draw(circle);
comp.setColor(Color.cyan);
comp.fillRect(0,0,500,500);
comp.setClip(circle);
comp.setColor(Color.magenta);
comp.fillRect(0,0,500,500);
}
}
Just draw it in the middle of your panel.
float x = getWidth()/2 - ELLIPSE_WIDTH/2;
float y = getHeight()/2 - ELLIPSE_HEIGHT/2;
Ellipse2D circle = new Ellipse2D.Float(x, y, ELLIPSE_WIDTH, ELLIPSE_HEIGHT);
Use getWidth()/getHeight() of the panel.
int x=(getWidth()-ovalWidth)/2;
int y=(getHeight()-ovalHeight)/2;
Check that panel width is bigger than oval width, and the same with height.
Take the panel object and query the X and Y size parameters(, or width and height). Divide each by 2 will give you the center of the frame. Create a circle using the result as the X and Y coordinates.
like
float x = (width-width of oval) /2;
float y = (height-height of oval) /2;
now set the x and y in the constructor of eclipse
You may easily get the size of your panel and place the circle accordingly:
Dimension size = getSize();
Ellipse2D circle = new Ellipse2D.Float(
(size.width - 200) / 2, // -200 due to the width/height of the circle
(size.height - 200) / 2,
200, 200);

Categories