paintcomponent

Drawing an object using getGraphics() without extending JFrame

回眸只為那壹抹淺笑 提交于 2019-12-27 10:40:07
问题 How can I draw an object without a class (which extends JFrame )? I found getGraphics method but it doesnt draw the object. import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(600, 400); JPanel panel = new JPanel(); frame.add(panel); Graphics g = panel.getGraphics(); g.setColor(Color.BLUE); g.fillRect(0, 0, 100, 100); } }

I am looking for a good example of adding an image to an applet

老子叫甜甜 提交于 2019-12-26 02:36:09
问题 I am trying to add an image to my applet. I have googled this but I have not found a decent example that I understand. Does anyone know where I can find a good example of adding an image to and applet? I got this online but when I run the applet it doesn't display my image. public class Lab5 extends JApplet { //Lab5(){} Image img; public void init() { img = getImage(getDocumentBase(), "img\flag0.gif"); } public void paintComponent(Graphics g) { g.drawImage(img, 50, 50, this); } } Below is my

Breakout Game Multiple Key Handling

孤人 提交于 2019-12-25 06:02:14
问题 i want my rectangle to stop when the user press both left and right key. I researched about multiple key handlings but couldnt manage to find anything. package View; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GamePanel extends JPanel { private final int WIDTH = 600, HEIGHT = 500; // PROPERTIES private Timer timer; int x = 0; int y = 475; int velX = 0; // only left or right // CONSTRUCTOR public GamePanel() { setSize(WIDTH, HEIGHT); setBackground(new Color

Breakout Game Multiple Key Handling

↘锁芯ラ 提交于 2019-12-25 06:01:13
问题 i want my rectangle to stop when the user press both left and right key. I researched about multiple key handlings but couldnt manage to find anything. package View; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GamePanel extends JPanel { private final int WIDTH = 600, HEIGHT = 500; // PROPERTIES private Timer timer; int x = 0; int y = 475; int velX = 0; // only left or right // CONSTRUCTOR public GamePanel() { setSize(WIDTH, HEIGHT); setBackground(new Color

How to add multiple classes to a single JFrame?

北战南征 提交于 2019-12-25 04:06:15
问题 So I'm trying to add multiple classes to my JFrame 'frame' using a JPanel 'panel' but it doesn't seem to have any effect. Here's my main class: import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; public class Frame { public static void main (String[] args) { JPanel panel = new JPanel(); panel.setBackground (Color.WHITE); panel.add (new Player()); // Class with paintComponent method. panel.add (new Terrain()); // Class with paintComponent method. JFrame frame = new

How to add multiple classes to a single JFrame?

筅森魡賤 提交于 2019-12-25 04:06:04
问题 So I'm trying to add multiple classes to my JFrame 'frame' using a JPanel 'panel' but it doesn't seem to have any effect. Here's my main class: import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; public class Frame { public static void main (String[] args) { JPanel panel = new JPanel(); panel.setBackground (Color.WHITE); panel.add (new Player()); // Class with paintComponent method. panel.add (new Terrain()); // Class with paintComponent method. JFrame frame = new

How to move a Rectangle with arrow keys?

不问归期 提交于 2019-12-25 02:57:07
问题 I have a frame which as a rectangle in it. I want to know how can I move the rectangle in if I clicked the arrow keys. I searched, and find few examples but nothing worked (weird, as it should be a simple thing to do) Here is my Rectangle class: public class PlayerOne implements KeyListener { int x,y; public PlayerOne(JPanel panel){ this.x = panel.getWidth()/2; this.y = panel.getHeight()/2; } public void paint(Graphics g){ g.setColor(Color.RED); g.fillRect(125, 480, 60, 10); } @Override

Setting arc position in Java using Mouse

旧街凉风 提交于 2019-12-25 02:46:44
问题 I am writing a 2D program. On my paintComponent I created an arc. public class Board extends Panel{ protected void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D graphics2d = (Graphics2D)g; int x = MouseInfo.getPointerInfo().getLocation().x;//set mouses current position int y = MouseInfo.getPointerInfo().getLocation().y; graphics2d.setStroke(wideStroke); graphics2d.draw(new Arc2D.Double(200, 200, 100, 100, ?, 180, Arc2D.OPEN)); } } In my main I am using a Thread to update the

Repaint doesn't work and the value of my private fields won't change?

不想你离开。 提交于 2019-12-25 02:31:08
问题 I am making a graphical interface in Netbeans where you can put a series of numbers (example: 7 8 5 4 10 13) in the textfield "punten" and when you press the button "ververs" a graphical linechart of all the numbers should appear (in my panel). I made a class "Gui" that extends JFrame with the Textfield, the button and a panel in it. I also made a class "Grafiek" that extends JPanel and that is linked with the panel in my "Gui". The problems that I experience are: the repaint(); command won't

Java Graphics Drawing Custom Circular

放肆的年华 提交于 2019-12-24 23:32:45
问题 I want to draw a shape at center that starts from the center of the panel and keeps expanding out in circular shape like in the picture link below : https://www.dropbox.com/s/lnxi1be8uogn2zx/20140320_124537.jpg but i am having difficulty with the center shape , its like it doesnt show up at all on the panel. Here is the class i wrote for the shapes package graphics.test; import java.awt.Graphics; import javax.swing.JPanel; public class DrawPanel extends JPanel { public void paintComponent