jpanel

Why does this while loop prevent paint method from working correctly?

大城市里の小女人 提交于 2021-02-17 03:39:10
问题 Here's my code: package javaapplication2; import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class JavaApplication2 extends JPanel { public static void main(String[] args) { JFrame frame = new JFrame("Simple Sketching Program"); frame.getContentPane().add(new JavaApplication2()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setVisible(true); } @Override public void paint(Graphics g) { g.setColor

JScrollPane and JPanel

拈花ヽ惹草 提交于 2021-02-16 13:44:45
问题 So, I'm making a kind of text editor, and I need a JScrollPane for vertical navigation. But I can't get it to work. I have read every freaking tutorial on first ten pages of google results, and I can't get it to work. Lets say I have JFrame (size 1000x800). I want to put a JPanel (1000x2000) in it so that it horizontally alignes with the JFrame. I want to stick a simple scroll bar to the right side of the JPanel so I can get to the rest of it. I have reduced sizes, I have added JPanel to

Java Snake Game: Apple showing while snake is invisible

北战南征 提交于 2021-02-11 14:51:04
问题 I am following a the following video to design a snake game: https://www.youtube.com/watch?v=91a7ceECNTc I am following it step by step, but when I run it, the snake does not show on my screen, just the apple. I think I have something wrong when implementing public void paint(Graphics g); Can someone help me? This is the code my Main class import javax.swing.JFrame; public class Main { public static void main(String[] args) { JFrame frame = new JFrame (); GamePanel panel = new GamePanel();

Java Snake Game: Apple showing while snake is invisible

余生长醉 提交于 2021-02-11 14:50:29
问题 I am following a the following video to design a snake game: https://www.youtube.com/watch?v=91a7ceECNTc I am following it step by step, but when I run it, the snake does not show on my screen, just the apple. I think I have something wrong when implementing public void paint(Graphics g); Can someone help me? This is the code my Main class import javax.swing.JFrame; public class Main { public static void main(String[] args) { JFrame frame = new JFrame (); GamePanel panel = new GamePanel();

Why repaint() doesn't work when i want repaint my painting?

拟墨画扇 提交于 2021-02-11 13:38:20
问题 This is my first java animation and I want my flag to start using repaint (), but the method does nothing and I do not know why. It's about the moveFlag method, it should put the flag in motion reminiscent of a flag blowing in the wind. The application simply displays the flag but it does not move it. public class Testing extends JPanel implements ActionListener { private int x,y, xinc, yinc; public void paintComponent(Graphics g){ xinc = 10; yinc = 10; x = 101; y = 151; Graphics2D gimg =

How to control rendering quality using JAVA

偶尔善良 提交于 2021-02-11 12:40:59
问题 I have simple drawing canvas like this: public class DrawingCanvas extends JPanel private BufferedImage image; @Override public void paintComponent(Graphics g) { super.paintComponent(g); image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D) image.getGraphics(); // draw some items.... g2.dispose(); g.drawImage(image, 0, 0, null); } } I want to ask you, if I can set some render quality? For example Antialiasing.. I want use BEST

Can you add JButtons inside a JPanel

拟墨画扇 提交于 2021-02-11 06:16:06
问题 I'm making a Battleship game using one board. The picture below shows how the board GUI should look. My current code sets up a board without the 'menu-layer'. (see picture) I tried doing this using Swing GUI designer in Intellij and got the following form (see picture), but can't find a way to insert these buttons inside the panel. Currently, I have got the following code. package BattleshipGUI; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class BoardFrame extends

JButton showing up in the wrong spot after repaint is called

不问归期 提交于 2021-02-10 20:21:33
问题 I am trying to add a couple of JButton objects to my GUI program in Java and it is adding them to the JPanel (which is the main file of the program) but it is appearing in the incorrect spot. It is showing op at point [0, 0] and the action that is linked to it is happening at the correct spot. The rest of the elements on the panel are Images so the repaint method is called very often. private void init() { setLayout(null); setBackground(new Color(r, g, b)); addMouseListener(this);

Java on Eclipse won't show the JPanel even when I add it to JFrame

安稳与你 提交于 2021-02-10 18:16:06
问题 These are the files. I have set JFrame to be visible, and have added JPanel to it, but still, the code only shows the window without anything in it. import java.util.List; import java.util.ArrayList; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import java.util.Collections; public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(350, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("My Empty Window");

Moving jLabel to a different place in the jPanel (Pacman like game)

强颜欢笑 提交于 2021-02-10 09:34:07
问题 I'm making a game like pacman and so far I am just starting with the grid. I got the grid started but I need to figure out how to move something to a different place in the grid so that when the user clicks or my ghosts move, it will display on the screen. How do I make it move? I have tried a bunch of different ways but none worked for me. import java.awt.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class GUI { public static void main(String[] args) { final JFrame f